UNPKG

@hitachivantara/uikit-react-core

Version:
20 lines (19 loc) 717 B
import { useEffect, useState } from "react"; //#region src/Header/Navigation/useSelectionPath.ts var getSelectionPath = (data, selectedId, selection = [], idx = -1, parent = []) => { data?.forEach((item, i) => { const hasData = item.data?.length; if (item.id === selectedId) selection.push(...idx > -1 ? [parent[idx].id] : [], item.id); if (hasData) getSelectionPath(item.data, selectedId, selection, i, data); }); return selection; }; var useSelectionPath = (data, selectedId) => { const [selectionPath, setSelectionPath] = useState([]); useEffect(() => { setSelectionPath(getSelectionPath(data, selectedId)); }, [data, selectedId]); return selectionPath; }; //#endregion export { useSelectionPath };