refactor: refactor toggle logic

This commit is contained in:
jongomez 2023-09-29 10:49:18 +01:00 committed by Jon
parent e4f3567cec
commit eace6c6160
1 changed files with 10 additions and 12 deletions

View File

@ -69,14 +69,18 @@ export const Dropdown: React.FC<DropdownProps> & {
}, },
}) })
const handleToggle = (open: boolean) => {
if (isControlled) {
onToggle && onToggle(open)
} else {
setOpenState(open)
}
}
const onTrigger = () => { const onTrigger = () => {
if (disabled) return if (disabled) return
if (isControlled) { handleToggle(!open)
onToggle && onToggle(!open)
} else {
setOpenState(!open)
}
} }
useEffect(() => { useEffect(() => {
@ -161,13 +165,7 @@ export const Dropdown: React.FC<DropdownProps> & {
<DropdownMenu <DropdownMenu
handleRef={containerRef} handleRef={containerRef}
open={open} open={open}
onClose={() => { onClose={() => handleToggle(false)}
if (isControlled) {
onToggle && onToggle(false)
} else {
setOpenState(false)
}
}}
size={size} size={size}
genericFontFamily={props.genericFontFamily} genericFontFamily={props.genericFontFamily}
> >