Created
March 4, 2024 10:29
-
-
Save keyurparalkar/3c33b27c6ad2f0403226201145e3001c to your computer and use it in GitHub Desktop.
Customized details component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CustomDetails = (props: CustomDetailsProps) => { | |
const { icons, name, renderTree } = props; | |
const [isOpen, setIsOpen] = useState(false); | |
const hasIcons = icons && icons.length > 0; | |
const handleDrawerOpen = ( | |
e: React.SyntheticEvent<HTMLDetailsElement, ToggleEvent>, | |
) => { | |
// This stops event bubbling; | |
e.stopPropagation(); | |
setIsOpen(e.nativeEvent.newState === "open" ? true : false); | |
}; | |
return ( | |
<StyledDetails onToggle={handleDrawerOpen} hasIcons={hasIcons}> | |
<summary className={hasIcons ? "custom-icons" : undefined}> | |
{hasIcons && (isOpen ? icons[1] : icons[0])} | |
<span>{name}</span> | |
</summary> | |
{renderTree} | |
</StyledDetails> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment