Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 6b0d30b

Browse files
authored
[DatePicker] Don't run onChange if same date selected (#1967)
1 parent 54faaad commit 6b0d30b

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

docs/prop-types.json

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/DateRangePicker/DateRangePickerDay.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const PureDateRangeDay = ({
141141
{...other}
142142
day={day}
143143
selected={selected}
144+
allowSameDateSelection={true}
144145
inCurrentMonth={inCurrentMonth}
145146
data-mui-test="DateRangeDay"
146147
className={clsx(

lib/src/__tests__/DatePickerTestingLib.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,23 @@ describe('<DatePicker />', () => {
7878

7979
expect(screen.queryByRole('dialog')).toBeInTheDocument();
8080
});
81+
82+
it('does not call onChange if same date selected', async () => {
83+
const onChangeMock = jest.fn();
84+
85+
render(
86+
<DesktopDatePicker
87+
TransitionComponent={FakeTransitionComponent}
88+
value={utilsToUse.date('2018-01-01T00:00:00.000Z')}
89+
onChange={onChangeMock}
90+
renderInput={props => <TextField {...props} />}
91+
/>
92+
);
93+
94+
fireEvent.click(screen.getByLabelText('Choose date, selected date is Jan 1, 2018'));
95+
await waitFor(() => screen.getByRole('dialog'));
96+
97+
fireEvent.click(screen.getByLabelText('Jan 1, 2018'));
98+
expect(onChangeMock).not.toHaveBeenCalled();
99+
});
81100
});

lib/src/_shared/hooks/usePickerState.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export function usePickerState<TInput, TDateValue>(
104104
selectionState: PickerSelectionState = 'partial'
105105
) => {
106106
setPickerDate(newDate);
107-
108107
if (selectionState === 'partial') {
109108
acceptDate(newDate, false);
110109
}

lib/src/views/Calendar/Day.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,18 @@ export interface DayProps extends ExtendMui<ButtonBaseProps> {
124124
* @default false
125125
*/
126126
disableHighlightToday?: boolean;
127+
/**
128+
* Allow selecting the same date (fire onChange even if same date is selected).
129+
* @default false
130+
*/
131+
allowSameDateSelection?: boolean;
127132
onDayFocus: (day: unknown) => void;
128133
onDaySelect: (day: unknown, isFinish: PickerSelectionState) => void;
129134
}
130135

131136
const PureDay: React.FC<DayProps> = ({
132137
allowKeyboardControl,
138+
allowSameDateSelection = false,
133139
className,
134140
day,
135141
disabled,
@@ -180,6 +186,8 @@ const PureDay: React.FC<DayProps> = ({
180186
};
181187

182188
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
189+
if (!allowSameDateSelection && selected) return;
190+
183191
if (!disabled) {
184192
onDaySelect(day, 'finish');
185193
}

0 commit comments

Comments
 (0)