@hitachivantara/uikit-react-core
Version:
Core React components for the NEXT Design System.
23 lines (22 loc) • 737 B
JavaScript
import { useState, useEffect } from "react";
const getSelectionPath = (data, selectedId, selection = [], idx = -1, parent = []) => {
data?.forEach((item, i) => {
const hasData = item.data?.length;
const isSelected = item.id === selectedId;
if (isSelected)
selection.push(...idx > -1 ? [parent[idx].id] : [], item.id);
if (hasData) getSelectionPath(item.data, selectedId, selection, i, data);
});
return selection;
};
const useSelectionPath = (data, selectedId) => {
const [selectionPath, setSelectionPath] = useState([]);
useEffect(() => {
const path = getSelectionPath(data, selectedId);
setSelectionPath(path);
}, [data, selectedId]);
return selectionPath;
};
export {
useSelectionPath
};