-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…te-range-picker TAN-2973 Replace old date range picker
- Loading branch information
Showing
40 changed files
with
853 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
front/app/components/admin/DatePickers/DatePhasePicker/Calendar/utils/generateModifiers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
front/app/components/admin/DatePickers/DatePhasePicker/Calendar/utils/getStartEndMonth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
front/app/components/admin/DatePickers/DatePhasePicker/Calendar/utils/getUpdatedRange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
front/app/components/admin/DatePickers/DatePhasePicker/Calendar/utils/rangesValid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
front/app/components/admin/DatePickers/DatePhasePicker/Calendar/utils/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
front/app/components/admin/DatePickers/DatePhasePicker/DatePhasePicker.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
front/app/components/admin/DatePickers/DatePhasePicker/isSelectedRangeOpenEnded.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
front/app/components/admin/DatePickers/DatePhasePicker/patchDisabledRanges.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
front/app/components/admin/DatePickers/DatePhasePicker/typings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
front/app/components/admin/DatePickers/DateRangePicker/Calendar/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import React from 'react'; | ||
|
||
import { Box, colors, Button } from '@citizenlab/cl2-component-library'; | ||
import { DayPicker, PropsBase } from 'react-day-picker'; | ||
import 'react-day-picker/style.css'; | ||
import styled from 'styled-components'; | ||
|
||
import useLocale from 'hooks/useLocale'; | ||
|
||
import { useIntl } from 'utils/cl-intl'; | ||
|
||
import { getEndMonth } from '../../_shared/getStartEndMonth'; | ||
import { getLocale } from '../../_shared/locales'; | ||
import { CalendarProps } from '../typings'; | ||
|
||
import messages from './messages'; | ||
import { getNextSelectionMode } from './utils/getNextSelectionMode'; | ||
import { getUpdatedRange } from './utils/getUpdatedRange'; | ||
|
||
const DayPickerStyles = styled.div` | ||
.rdp-root { | ||
--rdp-accent-color: ${colors.teal700}; | ||
--rdp-accent-background-color: ${colors.teal100}; | ||
} | ||
.rdp-selected > button.rdp-day_button { | ||
font-size: 14px; | ||
font-weight: normal; | ||
} | ||
`; | ||
|
||
const Calendar = ({ | ||
selectedRange, | ||
startMonth: _startMonth, | ||
endMonth: _endMonth, | ||
defaultMonth, | ||
disabled, | ||
selectionMode, | ||
numberOfMonths = 2, | ||
onUpdateRange, | ||
onUpdateSelectionMode, | ||
}: CalendarProps) => { | ||
const locale = useLocale(); | ||
const { formatMessage } = useIntl(); | ||
|
||
const startMonth = _startMonth ?? new Date(1900, 0); | ||
const endMonth = getEndMonth({ | ||
endMonth: _endMonth, | ||
selectedDate: selectedRange.to, | ||
}); | ||
|
||
const handleDayClick: PropsBase['onDayClick'] = (day: Date) => { | ||
if (!selectionMode) return; // Should not be possible in practice | ||
|
||
const nextRange = getUpdatedRange({ | ||
selectedRange, | ||
selectionMode, | ||
clickedDate: day, | ||
}); | ||
|
||
onUpdateRange(nextRange); | ||
|
||
const nextSelectionMode = getNextSelectionMode({ | ||
selectionMode, | ||
selectedRange: nextRange, | ||
}); | ||
|
||
onUpdateSelectionMode(nextSelectionMode); | ||
}; | ||
|
||
return ( | ||
<DayPickerStyles> | ||
<DayPicker | ||
mode="range" | ||
numberOfMonths={numberOfMonths} | ||
captionLayout="dropdown" | ||
locale={getLocale(locale)} | ||
startMonth={startMonth} | ||
endMonth={endMonth} | ||
defaultMonth={defaultMonth} | ||
selected={{ from: selectedRange.from, to: selectedRange.to }} | ||
disabled={disabled} | ||
onDayClick={handleDayClick} | ||
// This NOOP is necessary because otherwise the | ||
// DayPicker will rely on its internal state to manage the selected | ||
// range rather than being controlled by our state. | ||
onSelect={NOOP} | ||
footer={ | ||
<Box mt="12px" w="100%" display="flex"> | ||
{selectedRange.from && ( | ||
<Button | ||
buttonStyle="text" | ||
size="s" | ||
w="auto" | ||
pl="0px" | ||
ml="8px" | ||
onClick={() => { | ||
onUpdateRange({ from: undefined, to: selectedRange.to }); | ||
}} | ||
> | ||
{formatMessage(messages.clearStartDate)} | ||
</Button> | ||
)} | ||
{selectedRange.to && ( | ||
<Button | ||
buttonStyle="text" | ||
size="s" | ||
w="auto" | ||
pl="0px" | ||
ml="8px" | ||
onClick={() => { | ||
onUpdateRange({ from: selectedRange.from, to: undefined }); | ||
}} | ||
> | ||
{formatMessage(messages.clearEndDate)} | ||
</Button> | ||
)} | ||
</Box> | ||
} | ||
/> | ||
</DayPickerStyles> | ||
); | ||
}; | ||
|
||
const NOOP = () => {}; | ||
|
||
export default Calendar; |
12 changes: 12 additions & 0 deletions
12
front/app/components/admin/DatePickers/DateRangePicker/Calendar/messages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineMessages } from 'react-intl'; | ||
|
||
export default defineMessages({ | ||
clearStartDate: { | ||
id: 'app.components.admin.DatePickers.DateRangePicker.Calendar.clearStartDate', | ||
defaultMessage: 'Clear start date', | ||
}, | ||
clearEndDate: { | ||
id: 'app.components.admin.DatePickers.DateRangePicker.Calendar.clearEndDate', | ||
defaultMessage: 'Clear end date', | ||
}, | ||
}); |
23 changes: 23 additions & 0 deletions
23
...t/app/components/admin/DatePickers/DateRangePicker/Calendar/utils/getNextSelectionMode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DateRange } from 'components/admin/DatePickers/_shared/typings'; | ||
|
||
import { SelectionMode } from '../../typings'; | ||
|
||
interface GetNextSelectionModeParams { | ||
selectedRange: Partial<DateRange>; | ||
selectionMode: SelectionMode; | ||
} | ||
|
||
export const getNextSelectionMode = ({ | ||
selectedRange, | ||
selectionMode, | ||
}: GetNextSelectionModeParams) => { | ||
if (selectedRange.from && !selectedRange.to) { | ||
return 'to'; | ||
} | ||
|
||
if (!selectedRange.from && selectedRange.to) { | ||
return 'from'; | ||
} | ||
|
||
return selectionMode; | ||
}; |
Oops, something went wrong.