Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a new prop for simple calendar selection #7055 #7135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/js/components/Calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ const Calendar = forwardRef(
showAdjacentDays = true,
size = 'medium',
timestamp: timestampProp,
simpleSelection = false,
...rest
},
ref,
Expand Down Expand Up @@ -497,6 +498,7 @@ const Calendar = forwardRef(

const handleRange = useCallback(
(selectedDate) => {
console.log(simpleSelection);
let result;
const priorRange = normalizeRange(value, activeDate);
// deselect when date clicked was the start/end of the range
Expand All @@ -507,9 +509,12 @@ const Calendar = forwardRef(
result = [[priorRange[0][0], undefined]];
setActiveDate('end');
}

// selecting start date
else if (activeDate === 'start') {
if (!priorRange) {
if (simpleSelection) {
result = [[selectedDate, undefined]];
} else if (!priorRange) {
result = [[selectedDate, undefined]];
} else if (!priorRange[0][1]) {
result = [[selectedDate, priorRange[0][1]]];
Expand All @@ -519,16 +524,25 @@ const Calendar = forwardRef(
result = [[selectedDate, undefined]];
}
setActiveDate('end');
}
// selecting end date
else if (!priorRange) {
result = [[undefined, selectedDate]];
setActiveDate('start');
} else if (selectedDate.getTime() < priorRange[0][0].getTime()) {
result = [[selectedDate, undefined]];
setActiveDate('end');
} else if (selectedDate.getTime() > priorRange[0][0].getTime()) {
result = [[priorRange[0][0], selectedDate]];
} else {
// selecting end date
// swap if start date is after end date
if (
simpleSelection &&
selectedDate.getTime() < priorRange?.[0]?.[0]?.getTime()
) {
result = [[selectedDate, priorRange[0][0]]];
} else if (simpleSelection) {
result = [[priorRange[0][0], selectedDate]];
} else if (!priorRange) {
result = [[undefined, selectedDate]];
setActiveDate('start');
} else if (selectedDate.getTime() < priorRange[0][0].getTime()) {
result = [[selectedDate, undefined]];
setActiveDate('end');
} else if (selectedDate.getTime() > priorRange[0][0].getTime()) {
result = [[priorRange[0][0], selectedDate]];
}
setActiveDate('start');
}

Expand Down
1 change: 1 addition & 0 deletions src/js/components/Calendar/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface CalendarProps {
reference?: string;
showAdjacentDays?: boolean | 'trim';
size?: 'small' | 'medium' | 'large' | string;
simpleSelection?: boolean;
}

export interface CalendarExtendedProps
Expand Down
16 changes: 16 additions & 0 deletions src/js/components/Calendar/stories/RangeSimple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

import { Box, Calendar } from 'grommet';

export const RangeSimple = () => (
// Uncomment <Grommet> lines when using outside of storybook
// <Grommet theme={...}>
<Box align="center" pad="large">
<Calendar date={[['2020-04-03', '2020-04-08']]} range simpleSelection />
</Box>
// </Grommet>
);

export default {
title: 'Visualizations/Calendar/RangeSimple',
};