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] DateWrapper now uses state instead of ref #3333

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/few-dragons-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Datepicker: Use of 'open'-prop set to 'true' on first render could lead to the dialog not opening.
9 changes: 5 additions & 4 deletions @navikt/core/react/src/date/datepicker/DatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cl from "clsx";
import { isWeekend } from "date-fns";
import React, { forwardRef, useRef, useState } from "react";
import React, { forwardRef, useState } from "react";
import { DateRange, DayPicker, isMatch } from "react-day-picker";
import { omit } from "../../util";
import { useId } from "../../util/hooks";
Expand Down Expand Up @@ -88,8 +88,9 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
const ariaId = useId(id);
const [open, setOpen] = useState(_open ?? false);

const wrapperRef = useRef<HTMLDivElement | null>(null);
const mergedRef = useMergeRefs(wrapperRef, ref);
/* We use state here to insure that anchor is defined if open is true on initial render */
const [wrapperRef, setWrapperRef] = useState<HTMLDivElement | null>(null);
KenAJoh marked this conversation as resolved.
Show resolved Hide resolved
const mergedRef = useMergeRefs(setWrapperRef, ref);

const [selectedDates, setSelectedDates] = React.useState<
Date | Date[] | DateRange | undefined
Expand Down Expand Up @@ -167,7 +168,7 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
{children}
<DateWrapper
open={_open ?? open}
anchor={wrapperRef.current}
anchor={wrapperRef}
onClose={() => onClose?.() ?? setOpen(false)}
locale={locale}
variant={mode}
Expand Down
9 changes: 5 additions & 4 deletions @navikt/core/react/src/date/monthpicker/MonthPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cl from "clsx";
import React, { forwardRef, useRef, useState } from "react";
import React, { forwardRef, useState } from "react";
import { DayPickerProvider } from "react-day-picker";
import { useId } from "../../util/hooks";
import { useMergeRefs } from "../../util/hooks/useMergeRefs";
Expand Down Expand Up @@ -81,8 +81,9 @@ export const MonthPicker = forwardRef<HTMLDivElement, MonthPickerProps>(
const ariaId = useId(id);
const [open, setOpen] = useState(_open ?? false);

const wrapperRef = useRef<HTMLDivElement | null>(null);
const mergedRef = useMergeRefs(wrapperRef, ref);
/* We use state here to insure that anchor is defined if open is true on initial render */
const [wrapperRef, setWrapperRef] = useState<HTMLDivElement | null>(null);
const mergedRef = useMergeRefs(setWrapperRef, ref);

const [selectedMonth, setSelectedMonth] = useState<Date | undefined>(
defaultSelected,
Expand Down Expand Up @@ -118,7 +119,7 @@ export const MonthPicker = forwardRef<HTMLDivElement, MonthPickerProps>(
{children}
<DateWrapper
open={_open ?? open}
anchor={wrapperRef.current}
anchor={wrapperRef}
onClose={() => onClose?.() ?? setOpen(false)}
locale={locale}
variant="month"
Expand Down
Loading