Skip to content

Commit

Permalink
Merge e6cb3c4 into bb87837
Browse files Browse the repository at this point in the history
  • Loading branch information
KenAJoh authored Mar 20, 2024
2 parents bb87837 + e6cb3c4 commit 7303fe2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-ducks-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Switch: Fikset edgecase der checked Switch var visuelt unchecked
29 changes: 15 additions & 14 deletions @navikt/core/react/src/expansion-card/ExpansionCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cl from "clsx";
import React, { forwardRef, useRef, useState } from "react";
import React, { forwardRef, useRef } from "react";
import { useControllableState } from "../util/hooks";
import { OverridableComponent } from "../util/types";
import ExpansionCardContent, {
ExpansionCardContentProps,
Expand Down Expand Up @@ -116,24 +117,24 @@ export const ExpansionCard = forwardRef<HTMLDivElement, ExpansionCardProps>(
},
ref,
) => {
const [_open, _setOpen] = useState(defaultOpen);

const shouldFade = useRef<boolean>(!(Boolean(open) || defaultOpen));

const handleOpen = () => {
if (open === undefined) {
const newOpen = !_open;
_setOpen(newOpen);
onToggle?.(newOpen);
} else {
onToggle?.(!open);
}
shouldFade.current = true;
};
const [_open, _setOpen] = useControllableState({
value: open,
onChange: (newValue) => {
onToggle?.(newValue);
shouldFade.current = true;
},
defaultValue: defaultOpen,
});

return (
<ExpansionCardContext.Provider
value={{ open: open ?? _open, toggleOpen: handleOpen, size }}
value={{
open: open ?? _open,
toggleOpen: () => _setOpen((x) => !x),
size,
}}
>
<section
{...rest}
Expand Down
4 changes: 3 additions & 1 deletion @navikt/core/react/src/form/switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ export const Switch = forwardRef<HTMLInputElement, SwitchProps>(
...rest
} = props;

const [checked, setChecked] = useState(
const [_checked, setChecked] = useState(
defaultChecked ?? checkedProp ?? false,
);

useEffect(() => {
checkedProp !== undefined && setChecked(checkedProp);
}, [checkedProp]);

const checked = checkedProp ?? _checked;

return (
<div
className={cl(
Expand Down

0 comments on commit 7303fe2

Please sign in to comment.