From 32b7483c325e198159813f14128581ed783266c2 Mon Sep 17 00:00:00 2001 From: Dmitriy Kovalenko Date: Wed, 22 Jul 2020 18:29:14 +0300 Subject: [PATCH] [DateTimePicker] Allow same date selection (#2016) * [DateTimePicker] Allow same date selection * Override all props in the DateRangePickerDay --- .../DateRangePicker/DateRangePickerDay.tsx | 6 +++--- lib/src/DateTimePicker/DateTimePicker.tsx | 1 + .../DateTimePickerTestingLib.test.tsx | 18 +++++++++++++++++- lib/src/views/Calendar/Calendar.tsx | 19 ++++++++++++------- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/src/DateRangePicker/DateRangePickerDay.tsx b/lib/src/DateRangePicker/DateRangePickerDay.tsx index aab509979..8435ba4d5 100644 --- a/lib/src/DateRangePicker/DateRangePickerDay.tsx +++ b/lib/src/DateRangePicker/DateRangePickerDay.tsx @@ -136,12 +136,12 @@ export const PureDateRangeDay = ({ })} > ', () => { @@ -96,4 +96,20 @@ describe('', () => { fireEvent.click(screen.getByLabelText('open next view')); expect(screen.getByLabelText('open next view')).toBeDisabled(); }); + + it('allows to select the same day and move to the next view', () => { + const onChangeMock = jest.fn() + render( + } + value={utilsToUse.date('2018-01-01T00:00:00.000Z')} + /> + ); + + fireEvent.click(screen.getByLabelText("Jan 1, 2018")) + expect(onChangeMock).toHaveBeenCalled() + + expect(screen.getByLabelText(/Selected time/)).toBeInTheDocument() + }) }); diff --git a/lib/src/views/Calendar/Calendar.tsx b/lib/src/views/Calendar/Calendar.tsx index 52b1343e1..1f51e048e 100644 --- a/lib/src/views/Calendar/Calendar.tsx +++ b/lib/src/views/Calendar/Calendar.tsx @@ -13,7 +13,10 @@ import { useGlobalKeyDown, keycode } from '../../_shared/hooks/useKeyDown'; import { SlideTransition, SlideDirection, SlideTransitionProps } from './SlideTransition'; export interface ExportedCalendarProps - extends Pick { + extends Pick< + DayProps, + 'disableHighlightToday' | 'showDaysOutsideCurrentMonth' | 'allowSameDateSelection' + > { /** * Calendar onChange. */ @@ -55,7 +58,7 @@ export interface CalendarProps extends ExportedCalendarProps { } const muiComponentConfig = { name: 'MuiPickersCalendar' }; -export const useStyles = makeStyles((theme) => { +export const useStyles = makeStyles(theme => { const weeksContainerHeight = (DAY_SIZE + DAY_MARGIN * 4) * 6; return { root: { @@ -119,6 +122,7 @@ export const Calendar: React.FC = withDefaultProps( showDaysOutsideCurrentMonth, className, loading, + allowSameDateSelection, renderLoading = () => ..., TransitionProps, }) => { @@ -156,7 +160,7 @@ export const Calendar: React.FC = withDefaultProps( const currentMonthNumber = utils.getMonth(currentMonth); const selectedDates = (Array.isArray(date) ? date : [date]) .filter(Boolean) - .map((selectedDateItem) => utils.startOfDay(selectedDateItem)); + .map(selectedDateItem => utils.startOfDay(selectedDateItem)); return ( @@ -184,9 +188,9 @@ export const Calendar: React.FC = withDefaultProps( {...TransitionProps} >
- {utils.getWeekArray(currentMonth).map((week) => ( + {utils.getWeekArray(currentMonth).map(week => (
- {week.map((day) => { + {week.map(day => { const disabled = isDateDisabled(day); const isDayInCurrentMonth = utils.getMonth(day) === currentMonthNumber; @@ -196,14 +200,15 @@ export const Calendar: React.FC = withDefaultProps( role: 'cell', isAnimating: isMonthSwitchingAnimating, disabled: disabled, - allowKeyboardControl: allowKeyboardControl, + allowKeyboardControl, + allowSameDateSelection, focused: allowKeyboardControl && Boolean(focusedDay) && utils.isSameDay(day, focusedDay), today: utils.isSameDay(day, now), inCurrentMonth: isDayInCurrentMonth, - selected: selectedDates.some((selectedDate) => + selected: selectedDates.some(selectedDate => utils.isSameDay(selectedDate, day) ), disableHighlightToday,