diff --git a/src/lib/components/Accordion/Accordion.tsx b/src/lib/components/Accordion/Accordion.tsx index 8a11fe5c8..b30bf5325 100644 --- a/src/lib/components/Accordion/Accordion.tsx +++ b/src/lib/components/Accordion/Accordion.tsx @@ -44,10 +44,19 @@ const AccordionComponent: FC = ({ ...props }) => { const [isOpen, setOpen] = useState(collapseAll ? -1 : 0); + + const toggleOpen = (i: number) => { + if (isOpen === i) { + setOpen(-1); + } else { + setOpen(i); + } + }; + const panels = useMemo( () => Children.map(children, (child, i) => - cloneElement(child, { alwaysOpen, arrowIcon, flush, isOpen: isOpen === i, setOpen: () => setOpen(i) }), + cloneElement(child, { alwaysOpen, arrowIcon, flush, isOpen: isOpen === i, setOpen: () => toggleOpen(i) }), ), [alwaysOpen, arrowIcon, children, flush, isOpen], );