Skip to content
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

[DatePicker] Don't run onChange if same date selected #1967

Merged
merged 7 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/DateRangePicker/DateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function makeRangePicker<TWrapper extends SomeWrapper>(name: string, Wrap
emptyValue: [null, null],
parseInput: parseRangeInputValue,
areValuesEqual: (utils, a, b) => utils.isEqual(a[0], b[0]) && utils.isEqual(a[1], b[1]),
areDaysEqual: (utils, a, b) => utils.isSameDay(a[0], b[0]) && utils.isSameDay(a[1], b[1]),
};

function RangePickerWithStateAndWrapper({
Expand Down
2 changes: 2 additions & 0 deletions lib/src/Picker/makePickerWithState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const valueManager: PickerStateValueManager<ParsableDate, MaterialUiPickersDate>
parseInput: parsePickerInputValue,
areValuesEqual: (utils: MuiPickersAdapter, a: MaterialUiPickersDate, b: MaterialUiPickersDate) =>
utils.isEqual(a, b),
areDaysEqual: (utils: MuiPickersAdapter, a: MaterialUiPickersDate, b: MaterialUiPickersDate) =>
utils.isSameDay(a, b),
};

export function makePickerWithStateAndWrapper<
Expand Down
12 changes: 9 additions & 3 deletions lib/src/_shared/hooks/usePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export interface PickerStateValueManager<TInput, TDateValue> {
valueLeft: TDateValue,
valueRight: TDateValue
) => boolean;
areDaysEqual: (
utils: MuiPickersAdapter,
valueLeft: TDateValue,
valueRight: TDateValue
) => boolean;
}

export type PickerSelectionState = 'partial' | 'shallow' | 'finish';
Expand Down Expand Up @@ -103,8 +108,9 @@ export function usePickerState<TInput, TDateValue>(
wrapperVariant: WrapperVariant,
selectionState: PickerSelectionState = 'partial'
) => {
setPickerDate(newDate);

if (!valueManager.areDaysEqual(utils, newDate, pickerDate)) {
setPickerDate(newDate);
}
Copy link
Member

@dmtrKovalenko dmtrKovalenko Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!valueManager.areDaysEqual(utils, newDate, pickerDate)) {
setPickerDate(newDate);
}
if (valueManager.areDaysEqual(utils, newDate, pickerDate)) {
return;
}
setPickerDate(newDate);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something but wouldn't this run only if you select the same date?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right but we still doesn't need to update the state.
Another question – what for TimePicker and DateTimePicker? I think overall we need to add this check to the Calendar component.

if (selectionState === 'partial') {
acceptDate(newDate, false);
}
Expand All @@ -117,7 +123,7 @@ export function usePickerState<TInput, TDateValue>(
// if selectionState === "shallow" do nothing (we already update picker state)
},
}),
[acceptDate, disableCloseOnSelect, isMobileKeyboardViewOpen, pickerDate]
[acceptDate, disableCloseOnSelect, isMobileKeyboardViewOpen, pickerDate, utils, valueManager]
);

const inputProps = React.useMemo(
Expand Down