Skip to content

Commit 106b69b

Browse files
committed
1 parent 35557b5 commit 106b69b

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/Accordion.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
"use client";
22

3-
import React, { forwardRef, memo, useState, type ReactNode, type CSSProperties } from "react";
3+
import React, {
4+
forwardRef,
5+
memo,
6+
useState,
7+
useEffect,
8+
type ReactNode,
9+
type CSSProperties
10+
} from "react";
411
import { assert } from "tsafe/assert";
512
import type { Equals } from "tsafe";
613
import { fr } from "./fr";
@@ -24,15 +31,15 @@ export namespace AccordionProps {
2431

2532
export type Uncontrolled = Common & {
2633
defaultExpanded?: boolean;
27-
expanded?: undefined;
34+
expanded?: never;
2835
onExpandedChange?: (
2936
expanded: boolean,
3037
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
3138
) => void;
3239
};
3340

3441
export type Controlled = Common & {
35-
defaultExpanded?: undefined;
42+
defaultExpanded?: never;
3643
expanded: boolean;
3744
onExpandedChange: (
3845
expanded: boolean,
@@ -52,7 +59,7 @@ export const Accordion = memo(
5259
classes = {},
5360
style,
5461
children,
55-
expanded: expandedProp,
62+
expanded: expanded_props,
5663
defaultExpanded = false,
5764
onExpandedChange,
5865
...rest
@@ -67,14 +74,25 @@ export const Accordion = memo(
6774

6875
const collapseElementId = `${id}-collapse`;
6976

70-
const [expandedState, setExpandedState] = useState(defaultExpanded);
77+
const [isExpanded, setIsExpanded] = useState(expanded_props ?? defaultExpanded);
7178

72-
const value = expandedProp ? expandedProp : expandedState;
79+
useEffect(() => {
80+
if (expanded_props === undefined) {
81+
return;
82+
}
83+
84+
setIsExpanded(expanded_props);
85+
}, [expanded_props]);
7386

7487
const onExtendButtonClick = useConstCallback(
7588
(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
76-
setExpandedState(!value);
77-
onExpandedChange?.(!value, event);
89+
const isExpended_newValue = !isExpanded;
90+
91+
onExpandedChange?.(isExpended_newValue, event);
92+
93+
if (expanded_props === undefined) {
94+
setIsExpanded(isExpended_newValue);
95+
}
7896
}
7997
);
8098

@@ -88,7 +106,7 @@ export const Accordion = memo(
88106
<HtmlTitleTag className={cx(fr.cx("fr-accordion__title"), classes.title)}>
89107
<button
90108
className={fr.cx("fr-accordion__btn")}
91-
aria-expanded={value}
109+
aria-expanded={isExpanded}
92110
aria-controls={collapseElementId}
93111
onClick={onExtendButtonClick}
94112
type="button"

stories/Accordion.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function ControlledAccordion() {
3232
return (
3333
<Accordion
3434
label="Name of the Accordion"
35-
onChange={(value,) => setExpanded(!value)}
35+
onExpandedChange={(value,) => setExpanded(!value)}
3636
expanded={expanded}
3737
>
3838
Content of the Accordion

test/integration/vite/src/Home.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { useIsDark } from "@codegouvfr/react-dsfr/useIsDark";
1010
import { useState } from "react";
1111
import { Table } from "@codegouvfr/react-dsfr/Table";
1212

13+
import { Accordion } from "@codegouvfr/react-dsfr/Accordion";
14+
1315
export function Home() {
1416
const { isDark, setIsDark } = useIsDark();
1517
return (
@@ -66,6 +68,7 @@ export function Home() {
6668
</div>
6769
<Form />
6870
<TableExample />
71+
<ControlledAccordion />
6972
</>
7073
);
7174
}
@@ -202,4 +205,20 @@ function TableExample() {
202205
);
203206
}
204207

208+
function ControlledAccordion(){
209+
210+
const [ expanded, setExpanded ] = useState(false)
211+
212+
return (
213+
<Accordion
214+
label="Name of the Accordion"
215+
onExpandedChange={(value,) => setExpanded(!value)}
216+
expanded={expanded}
217+
>
218+
Content of the Accordion
219+
</Accordion>
220+
);
221+
222+
}
223+
205224

0 commit comments

Comments
 (0)