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

fix: avoid setting dateInView when value stays the same #103

Merged
merged 2 commits into from
Nov 28, 2023
Merged
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
15 changes: 14 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 @@ -361,12 +371,15 @@ export function DatePicker(props: DatePickerProps) {
format(new Date(Date.UTC(2021, 7, ((weekDay + firstDayOfWeek) % 7) + 1)));
}, [locale, firstDayOfWeek]);

const comparisonValue = getValueForComparison(value);
const comparisonDefaultDateInView = getValueForComparison(defaultDateInView);

useEffect(
() =>
setDateInView(
value === null ? defaultDateInView ?? mountTime : value instanceof Date ? value : value.max,
),
[value, defaultDateInView, mountTime],
[comparisonValue, comparisonDefaultDateInView, mountTime],
Copy link
Owner

Choose a reason for hiding this comment

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

Statt dafür Variable zu definieren, die den ganzen restlichen Scope "verschmutzen", finde ich es besser, das entweder inline zu machen.

);

useEffect(() => {
Expand Down