Skip to content

Commit

Permalink
[Datepicker] DateWrapper now uses state instead of ref (#3333)
Browse files Browse the repository at this point in the history
* bug: DateWrapper now uses state instead of ref to update after first render

* changeset update

* Added comments based on change
  • Loading branch information
KenAJoh authored Nov 8, 2024
1 parent c449c38 commit 702aecf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
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);
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

0 comments on commit 702aecf

Please sign in to comment.