Skip to content

Commit

Permalink
[DatePicker] Don't run onChange if same date selected (mui#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
todor-a authored Jul 15, 2020
1 parent 54faaad commit 6b0d30b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
23 changes: 19 additions & 4 deletions docs/prop-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/src/DateRangePicker/DateRangePickerDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const PureDateRangeDay = ({
{...other}
day={day}
selected={selected}
allowSameDateSelection={true}
inCurrentMonth={inCurrentMonth}
data-mui-test="DateRangeDay"
className={clsx(
Expand Down
19 changes: 19 additions & 0 deletions lib/src/__tests__/DatePickerTestingLib.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ describe('<DatePicker />', () => {

expect(screen.queryByRole('dialog')).toBeInTheDocument();
});

it('does not call onChange if same date selected', async () => {
const onChangeMock = jest.fn();

render(
<DesktopDatePicker
TransitionComponent={FakeTransitionComponent}
value={utilsToUse.date('2018-01-01T00:00:00.000Z')}
onChange={onChangeMock}
renderInput={props => <TextField {...props} />}
/>
);

fireEvent.click(screen.getByLabelText('Choose date, selected date is Jan 1, 2018'));
await waitFor(() => screen.getByRole('dialog'));

fireEvent.click(screen.getByLabelText('Jan 1, 2018'));
expect(onChangeMock).not.toHaveBeenCalled();
});
});
1 change: 0 additions & 1 deletion lib/src/_shared/hooks/usePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export function usePickerState<TInput, TDateValue>(
selectionState: PickerSelectionState = 'partial'
) => {
setPickerDate(newDate);

if (selectionState === 'partial') {
acceptDate(newDate, false);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/views/Calendar/Day.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,18 @@ export interface DayProps extends ExtendMui<ButtonBaseProps> {
* @default false
*/
disableHighlightToday?: boolean;
/**
* Allow selecting the same date (fire onChange even if same date is selected).
* @default false
*/
allowSameDateSelection?: boolean;
onDayFocus: (day: unknown) => void;
onDaySelect: (day: unknown, isFinish: PickerSelectionState) => void;
}

const PureDay: React.FC<DayProps> = ({
allowKeyboardControl,
allowSameDateSelection = false,
className,
day,
disabled,
Expand Down Expand Up @@ -180,6 +186,8 @@ const PureDay: React.FC<DayProps> = ({
};

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
if (!allowSameDateSelection && selected) return;

if (!disabled) {
onDaySelect(day, 'finish');
}
Expand Down

0 comments on commit 6b0d30b

Please sign in to comment.