Skip to content

Commit cf1f78f

Browse files
committed
Minor fixes on accordion
1 parent cba188c commit cf1f78f

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/Accordion.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { assert } from "tsafe";
33
import type { Equals } from "tsafe";
44
import { fr } from "./lib";
55
import { cx } from "./lib/tools/cx";
6-
import "@gouvfr/dsfr/dist/component/accordion/accordion.css";
76
import { symToStr } from "tsafe/symToStr";
87
import { useConstCallback } from "./lib/tools/powerhooks/useConstCallback";
8+
import "@gouvfr/dsfr/dist/component/accordion/accordion.css";
99

1010
export type AccordionProps = AccordionProps.Controlled | AccordionProps.Uncontrolled;
1111

12-
//TODO Controlled mode (callback onClick, expended etc ...)
1312
export namespace AccordionProps {
1413
export type Common = {
1514
className?: string;
@@ -38,7 +37,7 @@ export namespace AccordionProps {
3837
};
3938
}
4039

41-
/** @see <https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/accordeon> */
40+
/** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-accordion> */
4241
export const Accordion = memo(
4342
forwardRef<HTMLDivElement, AccordionProps>((props, ref) => {
4443
const {
@@ -55,7 +54,7 @@ export const Accordion = memo(
5554

5655
assert<Equals<keyof typeof rest, never>>();
5756

58-
const id = useId();
57+
const accordionId = `accordion-${useId()}`;
5958

6059
const [expandedState, setExpandedState] = useState(defaultExpanded);
6160

@@ -64,9 +63,7 @@ export const Accordion = memo(
6463
const onExtendButtonClick = useConstCallback(
6564
(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
6665
setExpandedState(!value);
67-
if (onExpandedChange) {
68-
onExpandedChange(!value, event);
69-
}
66+
onExpandedChange?.(!value, event);
7067
}
7168
);
7269

@@ -76,13 +73,13 @@ export const Accordion = memo(
7673
<button
7774
className={fr.cx("fr-accordion__btn")}
7875
aria-expanded={value}
79-
aria-controls={`accordion-${id}`}
76+
aria-controls={accordionId}
8077
onClick={onExtendButtonClick}
8178
>
8279
{label}
8380
</button>
8481
</HtmlTitleTag>
85-
<div className={cx(fr.cx("fr-collapse"), classes.collapse)} id={`accordion-${id}`}>
82+
<div className={cx(fr.cx("fr-collapse"), classes.collapse)} id={accordionId}>
8683
{content}
8784
</div>
8885
</section>

stories/Accordion.stories.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import { getStoryFactory, logCallbacks } from "./getStory";
33
import { sectionName } from "./sectionName";
44

55
const { meta, getStory } = getStoryFactory({
6-
"sectionName": sectionName,
6+
sectionName,
77
"wrappedComponent": { Accordion },
88
"description": `- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/accordeon)
99
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Accordion.tsx)
1010
1111
## Accordion group
1212
13-
If you want to use a group of accordion, you juste have to wrap your acordions in a div with a class \`fr-accordions-group\` as bellow :
13+
If you want to use a group of accordion, you just have to wrap your accordion in a div with a class \`fr-accordions-group\` as bellow :
1414
1515
\`\`\`tsx
16+
import { fr } from "@codegouvfr/react-dsfr";
17+
1618
<div className={fr.cx("fr-accordions-group")}>
1719
<Accordion label="Name of the Accordion 1" content="Content of the Accordion 1" />
1820
<Accordion label="Name of the Accordion 2" content="Content of the Accordion 2" />

0 commit comments

Comments
 (0)