Skip to content

Commit

Permalink
fix: avoid setting dateInView when value stays the same (schummar#103)
Browse files Browse the repository at this point in the history
* fix: avoid setting dateInView when value stays the same

* inline comparison

---------

Co-authored-by: Michael Förg <[email protected]>
  • Loading branch information
fer0n and Michael Förg authored Nov 28, 2023
1 parent e8470b5 commit d070acf
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/components/datePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ export function dateClamp(date: Date, min?: Date, max?: Date) {
return date;
}

function getValueForComparison(value?: Date | DateRange | null) {
if (!value) {
return value;
}
if (value instanceof Date) {
return value.getTime();
}
return `${value.min?.getTime()}-${value.max?.getTime()}`;
}

export function DatePicker(props: DatePickerProps) {
const context = useContext(DatePickerContext);

Expand Down Expand Up @@ -366,7 +376,7 @@ export function DatePicker(props: DatePickerProps) {
setDateInView(
value === null ? defaultDateInView ?? mountTime : value instanceof Date ? value : value.max,
),
[value, defaultDateInView, mountTime],
[getValueForComparison(value), getValueForComparison(defaultDateInView), mountTime],
);

useEffect(() => {
Expand Down

0 comments on commit d070acf

Please sign in to comment.