From eff03f661c3ec7444f0aa4b29ce163dfe17013b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Bu=C4=9Fra=20Yi=C4=9Fiter?= Date: Thu, 29 Feb 2024 20:36:54 +0300 Subject: [PATCH] Refactor Collapsible component to handle open state more efficiently --- src/components/Collapsible/Collapsible.tsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/components/Collapsible/Collapsible.tsx b/src/components/Collapsible/Collapsible.tsx index b8e80060d..ac28dbc5d 100644 --- a/src/components/Collapsible/Collapsible.tsx +++ b/src/components/Collapsible/Collapsible.tsx @@ -27,12 +27,22 @@ export const Collapsible = (props: CollapsibleProps) => { label, ...rest } = props + const [expanded, toggleExpanded] = useState(defaultOpen) const isOpen = open ?? expanded - const handleOpenChange = (open: boolean) => { - toggleExpanded(open) - onOpenChange?.(open) + const handleSetExpanded = (isExpanded: boolean) => { + if (open !== undefined) { + return + } + + toggleExpanded(isExpanded) + } + + const handleOpenChange = (isOpen: boolean) => { + handleSetExpanded(isOpen) + + onOpenChange?.(isOpen) } return (