import { useState, useRef, forwardRef, PropsWithChildren, ForwardedRef, useImperativeHandle } from 'react'; type Properties = { title: string; open: boolean; }; export default forwardRef(function Collapsible( props: PropsWithChildren, outerRef: ForwardedRef | undefined ) { const [opened, setOpened] = useState(props.open); const [previouslyOpened, setPreviouslyOpened] = useState(props.open); const innerRef = useRef(null); useImperativeHandle(outerRef, () => innerRef.current!, []); if (props.open !== previouslyOpened) { setOpened(props.open); setPreviouslyOpened(props.open); } const handleClick = () => { setOpened(!opened); if (innerRef && innerRef.current) { innerRef.current.classList.toggle('hidden'); } }; return (
{props.title} {opened ? '-' : '+'}
{props.children}
); });