-
Notifications
You must be signed in to change notification settings - Fork 6
feat: day picker #970
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
base: develop
Are you sure you want to change the base?
feat: day picker #970
Changes from all commits
c9c425c
e860234
d21b708
f27592a
775e5db
fe74b50
9c0ad7d
888cfb7
16c0e05
14c0746
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| .date-time-picker { | ||
| --rdp-accent-color: var(--B500); | ||
| --rdp-accent-background-color: var(--N700); | ||
| --rdp-day_button-border-radius: 0; | ||
| --rdp-day_button-border: 2px solid transparent; | ||
| --rdp-selected-border: none; | ||
| --rdp-today-color: var(--rdp-accent-color); | ||
| --rdp-range_middle-background-color: var(--O500); | ||
| --rdp-range_middle-color: inherit; | ||
| --rdp-range_start-color: var(--N100); | ||
| --rdp-range_start-background: linear-gradient(var(--O200), transparent 50%, var(--rdp-range_middle-background-color) 50%); | ||
| --rdp-range_start-date-background-color: var(--rdp-accent-color); | ||
| --rdp-range_end-background: linear-gradient(var(--O200), var(--rdp-range_middle-background-color) 50%, transparent 50%); | ||
| --rdp-range_end-color: var(--N100); | ||
| --rdp-range_end-date-background-color: var(--rdp-accent-color); | ||
| --rdp-week_number-border-radius: 100%; | ||
| --rdp-week_number-border: 2px solid var(--R100); | ||
|
|
||
| .rdp-day { | ||
| border: 1px solid var(--border-primary); | ||
|
|
||
| &_button { | ||
| color: var(--N900); | ||
| font-size: 14px; | ||
| font-weight: 400; | ||
| line-height: 20px; | ||
| border-radius: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
|
|
||
| &:hover { | ||
| background: var(--bg-hover); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .rdp-day.rdp-hidden { | ||
| border: none; | ||
| } | ||
|
|
||
| .rdp-day.rdp-selected { | ||
| .rdp-day_button { | ||
| background: var(--B100); | ||
| color: var(--B500); | ||
| font-size: 14px; | ||
| font-weight: 600; | ||
| line-height: 20px; | ||
| border: none; | ||
| border-radius: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| } | ||
| } | ||
|
|
||
| .rdp-day.rdp-disabled { | ||
| .rdp-day_button { | ||
| opacity: 0.5; | ||
| cursor: not-allowed; | ||
| } | ||
| } | ||
|
|
||
| .rdp-caption_label { | ||
| color: var(--N900); | ||
| font-size: 16px; | ||
| font-weight: 600; | ||
| } | ||
|
|
||
| .rdp-month_grid { | ||
| margin-top: 12px; | ||
| } | ||
|
|
||
| .rdp-weekday { | ||
| color: var(--N700); | ||
| font-size: 12px; | ||
| font-weight: 600; | ||
| line-height: 20px; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,31 +14,43 @@ | |||||||
| * limitations under the License. | ||||||||
| */ | ||||||||
|
|
||||||||
| import { useEffect, useState } from 'react' | ||||||||
| import { SingleDatePicker } from 'react-dates' | ||||||||
| import CustomizableCalendarDay from 'react-dates/esm/components/CustomizableCalendarDay' | ||||||||
| import { useMemo, useRef } from 'react' | ||||||||
| import { DateRange, DayPicker, OnSelectHandler } from 'react-day-picker' | ||||||||
| import { SelectInstance } from 'react-select' | ||||||||
| import moment from 'moment' | ||||||||
| import dayjs from 'dayjs' | ||||||||
|
|
||||||||
| import { ReactComponent as CalendarIcon } from '@Icons/ic-calendar.svg' | ||||||||
| import { ReactComponent as ClockIcon } from '@Icons/ic-clock.svg' | ||||||||
| import { ReactComponent as ICWarning } from '@Icons/ic-warning.svg' | ||||||||
| import { DATE_TIME_FORMATS } from '@Common/Constants' | ||||||||
| import { ComponentSizeType } from '@Shared/constants' | ||||||||
| import { getUniqueId } from '@Shared/Helpers' | ||||||||
|
|
||||||||
| import 'react-dates/initialize' | ||||||||
|
|
||||||||
| import { DATE_TIME_FORMATS } from '../../../Common' | ||||||||
| import { useFocusTrapControl } from '../DTFocusTrap' | ||||||||
| import { SelectPicker } from '../SelectPicker' | ||||||||
| import { customDayStyles, DATE_PICKER_IDS, DATE_PICKER_PLACEHOLDER } from './constants' | ||||||||
| import { DateTimePickerProps } from './types' | ||||||||
| import { Icon } from '../Icon' | ||||||||
| import { Popover, usePopover } from '../Popover' | ||||||||
| import { SelectPicker, SelectPickerOptionType } from '../SelectPicker' | ||||||||
| import { DATE_PICKER_CUSTOM_COMPONENTS, DATE_PICKER_IDS, DATE_PICKER_PLACEHOLDER } from './constants' | ||||||||
| import { DateTimePickerProps, UpdateDateRangeType } from './types' | ||||||||
| import { DEFAULT_TIME_OPTIONS, getTimeValue, updateDate, updateTime } from './utils' | ||||||||
|
|
||||||||
| import './datePicker.scss' | ||||||||
| import 'react-dates/lib/css/_datepicker.css' | ||||||||
| import 'react-day-picker/style.css' | ||||||||
| import './DateTimePicker.scss' | ||||||||
|
|
||||||||
| const isDateUpdateRange = ( | ||||||||
| isRange: boolean, | ||||||||
| handler: DateTimePickerProps['onChange'], | ||||||||
| ): handler is UpdateDateRangeType => isRange | ||||||||
|
|
||||||||
| const getTodayDate = (): Date => { | ||||||||
| const today = dayjs() | ||||||||
| return today.toDate() | ||||||||
| } | ||||||||
|
|
||||||||
| const DateTimePicker = ({ | ||||||||
| date: dateObject = new Date(), | ||||||||
| date: dateObject = getTodayDate(), | ||||||||
| dateRange = { | ||||||||
| from: getTodayDate(), | ||||||||
| to: getTodayDate(), | ||||||||
| }, | ||||||||
| onChange, | ||||||||
| timePickerProps = {} as SelectInstance, | ||||||||
| disabled, | ||||||||
|
|
@@ -47,40 +59,170 @@ const DateTimePicker = ({ | |||||||
| required, | ||||||||
| hideTimeSelect = false, | ||||||||
| readOnly = false, | ||||||||
| isTodayBlocked = false, | ||||||||
| dataTestIdForTime = DATE_PICKER_IDS.TIME, | ||||||||
| dataTestidForDate = DATE_PICKER_IDS.DATE, | ||||||||
| openDirection = 'down', | ||||||||
| error = '', | ||||||||
| isRangePicker = false, | ||||||||
| isTodayBlocked = false, | ||||||||
| blockPreviousDates = true, | ||||||||
| isOutsideRange, | ||||||||
| rangeShortcutOptions, | ||||||||
| }: DateTimePickerProps) => { | ||||||||
| const calendarPopoverId = useRef<string>(getUniqueId()) | ||||||||
|
|
||||||||
| const { open, overlayProps, popoverProps, triggerProps, scrollableRef } = usePopover({ | ||||||||
| id: `date-time-picker-popover-${calendarPopoverId.current}`, | ||||||||
| alignment: 'end', | ||||||||
| variant: 'overlay', | ||||||||
| }) | ||||||||
|
|
||||||||
| const parsedPopoverProps = useMemo<typeof popoverProps>( | ||||||||
| () => ({ | ||||||||
| ...popoverProps, | ||||||||
| className: `${popoverProps.className} w-100 p-12 date-time-picker`, | ||||||||
| style: { | ||||||||
| ...popoverProps.style, | ||||||||
| maxWidth: 'none', | ||||||||
| }, | ||||||||
| }), | ||||||||
| [popoverProps], | ||||||||
| ) | ||||||||
|
|
||||||||
| const parsedOverlayProps = useMemo<typeof overlayProps>( | ||||||||
| () => ({ | ||||||||
| ...overlayProps, | ||||||||
| initialFocus: false, | ||||||||
| }), | ||||||||
| [overlayProps], | ||||||||
| ) | ||||||||
|
|
||||||||
| const time = getTimeValue(dateObject) | ||||||||
| const selectedTimeOption = DEFAULT_TIME_OPTIONS.find((option) => option.value === time) ?? DEFAULT_TIME_OPTIONS[0] | ||||||||
| const [isFocused, setFocused] = useState(false) | ||||||||
| const handleTimeChange = (option: SelectPickerOptionType<string>) => { | ||||||||
| if (isDateUpdateRange(isRangePicker, onChange)) { | ||||||||
| return | ||||||||
| } | ||||||||
| onChange(updateTime(dateObject, option.value).value) | ||||||||
| } | ||||||||
|
|
||||||||
| const { disableFocusTrap, resumeFocusTrap } = useFocusTrapControl() | ||||||||
| const handleDateRangeChange = (range: DateRange) => { | ||||||||
| if (isDateUpdateRange(isRangePicker, onChange)) { | ||||||||
| const fromDate = range.from ? range.from : new Date() | ||||||||
| const toDate = range.to ? range.to : undefined | ||||||||
|
|
||||||||
| const handleFocusChange = ({ focused }) => { | ||||||||
| setFocused(focused) | ||||||||
| onChange({ | ||||||||
| from: fromDate, | ||||||||
| to: toDate, | ||||||||
| }) | ||||||||
|
Comment on lines
+107
to
+115
|
||||||||
| } | ||||||||
| } | ||||||||
| const handleDateChange = (event) => { | ||||||||
| onChange(updateDate(dateObject, event?.toDate())) | ||||||||
|
|
||||||||
| const handleRangeSelect: OnSelectHandler<DateRange> = (range) => { | ||||||||
| handleDateRangeChange(range) | ||||||||
| } | ||||||||
|
|
||||||||
| const handleTimeChange = (option) => { | ||||||||
| onChange(updateTime(dateObject, option.value).value) | ||||||||
| const getRangeUpdateHandler = (range: DateRange) => () => { | ||||||||
| handleDateRangeChange(range) | ||||||||
| } | ||||||||
|
|
||||||||
| const today = moment() | ||||||||
| // Function to disable dates including today and all past dates | ||||||||
| const isDayBlocked = (day) => isTodayBlocked && !day.isAfter(today) | ||||||||
| const handleSingleDateSelect: OnSelectHandler<Date> = (date) => { | ||||||||
| if (!isDateUpdateRange(isRangePicker, onChange)) { | ||||||||
| const updatedDate = date ? updateDate(dateObject, date) : null | ||||||||
| onChange(updatedDate) | ||||||||
| } | ||||||||
| } | ||||||||
|
Comment on lines
+127
to
+132
|
||||||||
|
|
||||||||
| useEffect(() => { | ||||||||
| if (isFocused) { | ||||||||
| disableFocusTrap() | ||||||||
| return | ||||||||
| const getDisabledState = () => { | ||||||||
| if (readOnly) { | ||||||||
| return true | ||||||||
| } | ||||||||
| resumeFocusTrap() | ||||||||
| }, [isFocused]) | ||||||||
|
|
||||||||
| const today = getTodayDate() | ||||||||
| today.setHours(0, 0, 0, 0) | ||||||||
|
Comment on lines
+139
to
+140
|
||||||||
|
|
||||||||
| const isOutsideRangeFn = isOutsideRange || (() => false) | ||||||||
|
|
||||||||
| if (isTodayBlocked) { | ||||||||
| return (date: Date) => date <= today || isOutsideRangeFn(date) | ||||||||
| } | ||||||||
|
|
||||||||
| if (blockPreviousDates) { | ||||||||
| return (date: Date) => date < today || isOutsideRangeFn(date) | ||||||||
| } | ||||||||
|
|
||||||||
| return isOutsideRangeFn | ||||||||
| } | ||||||||
|
|
||||||||
| const renderDatePicker = () => { | ||||||||
| if (isRangePicker) { | ||||||||
| return ( | ||||||||
| <div className="flexbox"> | ||||||||
| {!!rangeShortcutOptions?.length && ( | ||||||||
| <div className="flexbox-col py-20 px-16 w-200"> | ||||||||
| {rangeShortcutOptions.map(({ label: optionLabel, value, onClick }) => ( | ||||||||
| <button | ||||||||
| type="button" | ||||||||
| key={optionLabel} | ||||||||
| className="bg__hover cn-9 dc__tab-focus dc__align-left dc__transparent p-8 br-4" | ||||||||
| onClick={onClick || getRangeUpdateHandler(value)} | ||||||||
|
||||||||
| onClick={onClick || getRangeUpdateHandler(value)} | |
| onClick={onClick || getRangeUpdateHandler(value)} | |
| aria-label={`Select range: ${optionLabel}`} |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] When dateRange.from is falsy in range picker mode, the fromDate will be an empty string, resulting in a label like - .... This could be visually awkward. Consider displaying a more user-friendly placeholder text like "Select start date - ..." when from is not set.
| const fromDate = dateRange.from ? dayjs(dateRange.from).format(DATE_TIME_FORMATS.DD_MMM_YYYY) : '' | |
| const fromDate = dateRange.from ? dayjs(dateRange.from).format(DATE_TIME_FORMATS.DD_MMM_YYYY) : 'Select start date' |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label element uses htmlFor={id} but the actual input element inside SelectPicker uses inputId="date-picker-input" (a hardcoded value). This means the label won't properly associate with the input when clicked. Either use the id prop for the inputId or ensure they match for proper accessibility.
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The disabled prop is not passed to the date picker trigger element (SelectPicker). When the DateTimePicker is disabled, only the time picker reflects this state (line 257), but the date picker input appears enabled. Consider passing isDisabled={disabled} to the trigger SelectPicker to ensure consistent disabled state across both inputs.
| size={ComponentSizeType.large} | |
| size={ComponentSizeType.large} | |
| isDisabled={disabled} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The default value for
dateRangeis set to{ from: getTodayDate(), to: getTodayDate() }, which means both start and end dates default to today. This could be confusing for range pickers where typically the end date might be undefined initially. Consider using{ from: getTodayDate(), to: undefined }or making the default value conditional based on whether it's a range picker.