-
Notifications
You must be signed in to change notification settings - Fork 12
feat: date range picker in Visit Registration replaces two date pickers #1603
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
Open
shivoomiess
wants to merge
11
commits into
develop
Choose a base branch
from
SWAP-5412-daterange-visitform
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
91edd66
feat: date range picker
shivoomiess d59c2ca
fix: use mui popover instead of rendering date-picker inside modal
shivoomiess 1eb5163
chore: apply stylistic and usability review suggestions to datepicker
shivoomiess 673fb0a
Merge branch 'develop' into SWAP-5412-daterange-visitform
shivoomiess f5fe5fb
Merge branch 'develop' into SWAP-5412-daterange-visitform
jekabs-karklins ca2e50f
Merge branch 'develop' into SWAP-5412-daterange-visitform
jekabs-karklins 1d55260
Merge branch 'develop' into SWAP-5412-daterange-visitform
jekabs-karklins d4d18e1
Merge branch 'develop' into SWAP-5412-daterange-visitform
yoganandaness 8f5e128
Merge branch 'develop' into SWAP-5412-daterange-visitform
shivoomiess 4b6f5d0
fix: add review suggestions
shivoomiess 41f5537
Merge branch 'develop' into SWAP-5412-daterange-visitform
yoganandaness File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
apps/frontend/src/components/common/FormikUIDayRangePicker.tsx
This file contains hidden or 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,167 @@ | ||
| import DateRangeIcon from '@mui/icons-material/DateRange'; | ||
| import { Box, Button, IconButton } from '@mui/material'; | ||
| import Popover from '@mui/material/Popover'; | ||
| import { styled } from '@mui/material/styles'; | ||
| import TextField from '@mui/material/TextField'; | ||
| import { DateTime } from 'luxon'; | ||
| import React, { useState } from 'react'; | ||
| import { DayPicker, DateRange, Matcher } from 'react-day-picker'; | ||
| import 'react-day-picker/dist/style.css'; | ||
|
|
||
| const StyledPickerWrapper = styled('div')(({ theme }) => ({ | ||
| padding: theme.spacing(2), | ||
| borderRadius: theme.shape.borderRadius, | ||
| '.rdp-range_start': { | ||
| background: `linear-gradient(90deg, transparent 50%, ${theme.palette.primary.main} 50%)`, | ||
| border: 'none', | ||
| }, | ||
| '.rdp-range_end': { | ||
| background: `linear-gradient(90deg, ${theme.palette.primary.main} 50%, transparent 50%)`, | ||
| border: 'none', | ||
| }, | ||
| '.rdp-range_start .rdp-day_button, .rdp-range_end .rdp-day_button': { | ||
| backgroundColor: theme.palette.primary.main, | ||
| border: 'none', | ||
| }, | ||
| '.rdp-range_middle .rdp-day_button': { | ||
| backgroundColor: theme.palette.primary.main, | ||
| borderColor: theme.palette.primary.main, | ||
| color: 'white', | ||
| }, | ||
| '.rdp-day': { | ||
| padding: 0, | ||
| width: '100%', | ||
| height: '100%', | ||
| }, | ||
| '.rdp-nav svg': { | ||
| fill: theme.palette.primary.dark, | ||
| }, | ||
| })); | ||
|
|
||
| export interface DayRange { | ||
| from?: DateTime; | ||
| to?: DateTime; | ||
| } | ||
|
|
||
| export interface DayRangePickerProps { | ||
| label?: string; | ||
| value: DayRange; | ||
| onChange: (range: DayRange) => void; | ||
| /** Luxon format token used to display the selected dates. */ | ||
| format?: string | null; | ||
| /** Earliest selectable date - days before it are disabled. */ | ||
| minDate?: DateTime; | ||
| disabled?: boolean; | ||
| required?: boolean; | ||
| error?: string; | ||
| helperText?: string; | ||
| id?: string; | ||
| } | ||
|
|
||
| const formatDate = (date: DateTime | undefined, format?: string | null) => { | ||
| if (!date) { | ||
| return ''; | ||
| } | ||
|
|
||
| return format | ||
| ? date.toFormat(format) | ||
| : date.toLocaleString(DateTime.DATE_SHORT); | ||
| }; | ||
|
|
||
| export default function DayRangePicker({ | ||
| label, | ||
| value, | ||
| onChange, | ||
| format, | ||
| minDate, | ||
| disabled, | ||
| required, | ||
| error, | ||
| helperText, | ||
| id, | ||
| }: DayRangePickerProps) { | ||
| const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null); | ||
| const open = Boolean(anchorEl); | ||
|
|
||
| const selected: DateRange = { | ||
| from: value?.from?.toJSDate(), | ||
| to: value?.to?.toJSDate(), | ||
| }; | ||
|
|
||
| const handleSelect = (range: DateRange | undefined) => { | ||
| onChange({ | ||
| from: range?.from ? DateTime.fromJSDate(range.from) : undefined, | ||
| to: range?.to ? DateTime.fromJSDate(range.to) : undefined, | ||
| }); | ||
| }; | ||
|
|
||
| const displayValue = value?.from | ||
| ? `${formatDate(value.from, format)} - ${formatDate(value.to, format)}` | ||
| : ''; | ||
|
|
||
| const disabledDays: Matcher | undefined = minDate | ||
| ? { before: minDate.toJSDate() } | ||
| : undefined; | ||
|
|
||
| return ( | ||
| <Box style={{ display: 'flex' }}> | ||
| <Box style={{ flex: 1 }}> | ||
| <TextField | ||
| onClick={(event) => setAnchorEl(event.currentTarget)} | ||
| data-cy={id} | ||
| label={label} | ||
| fullWidth | ||
| required={required} | ||
| disabled={disabled} | ||
| placeholder="DD/MM/YYYY - DD/MM/YYYY" | ||
| value={displayValue} | ||
| error={!!error} | ||
| helperText={error ?? helperText} | ||
| InputLabelProps={{ shrink: true }} | ||
| InputProps={{ readOnly: true }} | ||
|
shivoomiess marked this conversation as resolved.
|
||
| /> | ||
| </Box> | ||
| <IconButton | ||
| onClick={(event) => setAnchorEl(event.currentTarget)} | ||
| disabled={disabled} | ||
| data-cy={id ? `${id}-calendar-btn` : undefined} | ||
| aria-label="Open date range picker" | ||
| sx={{ marginLeft: 1, marginTop: 1 }} | ||
| > | ||
| <DateRangeIcon /> | ||
| </IconButton> | ||
| <Popover | ||
| open={open} | ||
| anchorEl={anchorEl} | ||
| onClose={() => setAnchorEl(null)} | ||
| anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }} | ||
|
shivoomiess marked this conversation as resolved.
|
||
| transformOrigin={{ vertical: 'top', horizontal: 'right' }} | ||
| > | ||
| <StyledPickerWrapper> | ||
| <DayPicker | ||
| mode="range" | ||
| selected={selected} | ||
| onSelect={handleSelect} | ||
| disabled={disabledDays} | ||
| /> | ||
| </StyledPickerWrapper> | ||
| <Box | ||
| style={{ | ||
| display: 'flex', | ||
| justifyContent: 'flex-end', | ||
| padding: '8px', | ||
| }} | ||
| > | ||
| <Button | ||
| onClick={() => setAnchorEl(null)} | ||
| variant="contained" | ||
| color="primary" | ||
| data-cy={id ? `${id}-done-btn` : undefined} | ||
| > | ||
| Done | ||
| </Button> | ||
| </Box> | ||
| </Popover> | ||
| </Box> | ||
| ); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.