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" ;
411import { assert } from "tsafe/assert" ;
512import type { Equals } from "tsafe" ;
613import { 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"
0 commit comments