Skip to content

Commit

Permalink
fix(date-picker): stabilize defaultDate behavior (#13186)
Browse files Browse the repository at this point in the history
* fix(date-picker): stabiliz defaultDate behavior

* Ignore time in date objects when comparing for equality to prevent overrides
* clear input values on rerender when value prop is set to prevent overrides
* Add test story (to be deleted)

* fix(date-picker): re-add simple storybook

* chore(date-picker): remove test story

---------

Co-authored-by: Alison Joseph <[email protected]>
  • Loading branch information
francinelucca and alisonjoseph authored Feb 24, 2023
1 parent 0ab5fc1 commit 2ca3000
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/react/src/components/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ const DatePicker = React.forwardRef(function DatePicker(
calendar.destroy();
}

// prevent a duplicate date selection when a default value is set
if (value) {
if (startInputField?.current) {
startInputField.current.value = '';
}
if (endInputField?.current) {
// eslint-disable-next-line react-hooks/exhaustive-deps
endInputField.current.value = '';
}
}

if (start) {
start.removeEventListener('keydown', handleArrowDown);
start.removeEventListener('change', handleOnChange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export default (config) => (fp) => {
if (inputTo === target && fp.selectedDates[1]) {
// Using getTime() enables the ability to more readily compare the date currently
// selected in the calendar and the date currently in the value of the input
const selectedToDate = new Date(fp.selectedDates[1]).getTime();
const currentValueToDate = new Date(inputTo.value).getTime();
const selectedToDate = new Date(fp.selectedDates[1]).setHours(0, 0, 0, 0);
const currentValueToDate = new Date(inputTo.value).setHours(0, 0, 0, 0);

// The date should only be set if both dates are valid dates, and they don't match.
// When they don't match, this indiciates that the date selected in the calendar is stale,
Expand Down

0 comments on commit 2ca3000

Please sign in to comment.