Created
April 26, 2019 10:35
-
-
Save cladley/052e89134dff03529c122670a0e2616a to your computer and use it in GitHub Desktop.
Accordion using react hooks
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
import React, {forwardRef, useRef, useImperativeHandle, useState, useCallback, useMemo} from 'react'; | |
const Accordion = (props) => { | |
let items = useRef(new Map()).current; | |
const handleOnToggle = (id) => { | |
items.forEach((value, key) => { | |
if (key != id) { | |
value.close(); | |
} | |
}); | |
}; | |
const registerNode = useCallback((node) => { | |
if (node) { | |
items.set(node.id, node); | |
} | |
}, []); | |
return ( | |
React.Children.map(props.children, child => { | |
return React.cloneElement(child, { | |
ref: registerNode, | |
onToggle: handleOnToggle | |
}); | |
}) | |
); | |
}; | |
const Item = forwardRef((props, ref) => { | |
const [isOpen, setIsOpen] = useState(false); | |
const id = useMemo(() => (String(Math.random())), []); | |
const toggle = () => { | |
setIsOpen(o => !o); | |
props.onToggle(id); | |
}; | |
useImperativeHandle(ref, () => { | |
return { | |
id: id, | |
close() { | |
setIsOpen(false); | |
} | |
}; | |
}); | |
return ( | |
<div> | |
<h5 onClick={toggle}>{props.title}</h5> | |
{isOpen && <div> | |
{props.children} | |
</div>} | |
</div> | |
); | |
}); | |
Accordion.Item = Item; | |
export default Accordion; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment