UNPKG

@kenshooui/react-tree

Version:

React Tree is a straight forward component that allows a user to display and manage a hierarchical structure of items in a clear and comfortable way.

32 lines (26 loc) 682 B
import { useState } from "react"; const useItemCallbacks = ({ onSelect }) => { const [currentDepth, setCurrentDepth] = useState(0); const [parents, setParents] = useState([]); const onClick = (label, item, hasChild) => { if (hasChild) { setParents(parents.concat(label)); setCurrentDepth(currentDepth + 1); } else { onSelect(item); } }; const onBackClick = () => { setParents(parents.filter((parent, index) => index < parents.length - 1)); setCurrentDepth(currentDepth - 1); }; return { onClick, onBackClick, currentDepth, setCurrentDepth, parents, setParents }; }; export default useItemCallbacks;