From 388a3ea22f560f4ac46bb5694ee27efa613ca9de Mon Sep 17 00:00:00 2001 From: Flavien DELANGLE Date: Thu, 2 Jun 2022 09:39:37 +0200 Subject: [PATCH] [DateRangePicker] Fix usage of `maxDate` / `minDate` / `disableFuture` and `disablePast` (#5081) --- .../DateRangePickerViewDesktop.tsx | 4 ++ .../DesktopDateRangePicker.test.tsx | 65 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePickerViewDesktop.tsx b/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePickerViewDesktop.tsx index d1aea008f4db5..a7082d58ebb24 100644 --- a/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePickerViewDesktop.tsx +++ b/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePickerViewDesktop.tsx @@ -192,6 +192,10 @@ export function DateRangePickerViewDesktop(props: DesktopDateRangeCalenda {...other} + minDate={minDate} + maxDate={maxDate} + disablePast={disablePast} + disableFuture={disableFuture} key={index} selectedDays={parsedValue} onFocusedDayChange={doNothing} diff --git a/packages/x-date-pickers-pro/src/DesktopDateRangePicker/DesktopDateRangePicker.test.tsx b/packages/x-date-pickers-pro/src/DesktopDateRangePicker/DesktopDateRangePicker.test.tsx index 9ed24f6aac41c..0032283579aa2 100644 --- a/packages/x-date-pickers-pro/src/DesktopDateRangePicker/DesktopDateRangePicker.test.tsx +++ b/packages/x-date-pickers-pro/src/DesktopDateRangePicker/DesktopDateRangePicker.test.tsx @@ -27,7 +27,10 @@ const WrappedDesktopDateRangePicker = withPickerControls(DesktopDateRangePicker) }); describe('', () => { - const { render, clock } = createPickerRenderer({ clock: 'fake' }); + const { render, clock } = createPickerRenderer({ + clock: 'fake', + clockConfig: new Date('2018-01-10T00:00:00.000'), + }); describeConformance( ', () => { expect(onClose.callCount).to.equal(0); }); }); + + describe('disabled dates', () => { + it('should respect the disablePast prop', () => { + render(); + + openPicker({ type: 'date-range', variant: 'desktop', initialFocus: 'start' }); + + expect(screen.getByLabelText('Jan 8, 2018')).to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 9, 2018')).to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 10, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 11, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 11, 2018')).not.to.have.attribute('disabled'); + }); + + it('should respect the disableFuture prop', () => { + render(); + + openPicker({ type: 'date-range', variant: 'desktop', initialFocus: 'start' }); + + expect(screen.getByLabelText('Jan 8, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 9, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 10, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 11, 2018')).to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 12, 2018')).to.have.attribute('disabled'); + }); + + it('should respect the minDate prop', () => { + render( + , + ); + + openPicker({ type: 'date-range', variant: 'desktop', initialFocus: 'start' }); + + expect(screen.getByLabelText('Jan 13, 2018')).to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 14, 2018')).to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 15, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 16, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 17, 2018')).not.to.have.attribute('disabled'); + }); + + it('should respect the maxDate prop', () => { + render( + , + ); + + openPicker({ type: 'date-range', variant: 'desktop', initialFocus: 'start' }); + + expect(screen.getByLabelText('Jan 13, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 14, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 15, 2018')).not.to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 16, 2018')).to.have.attribute('disabled'); + expect(screen.getByLabelText('Jan 17, 2018')).to.have.attribute('disabled'); + }); + }); });