UNPKG

@carbon/ibm-products

Version:
56 lines (54 loc) 1.36 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ require("../../../_virtual/_rolldown/runtime.js"); let react = require("react"); //#region src/components/AddSelect/hooks/usePath.js const usePath = (itemsLabel = "") => { const [path, setPath] = (0, react.useState)([{ id: "base_of_path", title: itemsLabel }]); (0, react.useEffect)(() => { setPath([{ id: "base_of_path", title: itemsLabel }]); }, [itemsLabel]); const handler = (id, title, parentId) => { if (path.find((entry) => entry.id === id)) return; const pathEntry = { id, title, ...parentId && { parentId } }; if (path.find((entry) => entry.parentId === parentId)) { const parentIdx = path.findIndex((entry) => entry.id === parentId); const pathCopy = [...path]; pathCopy.length = parentIdx + 1; setPath([...pathCopy, pathEntry]); } else setPath([...path, pathEntry]); }; const pathClickHandler = (idx) => { const pathCopy = [...path]; pathCopy.length = idx + 1; setPath([...pathCopy]); }; const resetPath = () => { setPath([{ id: "base_of_path", title: itemsLabel }]); }; return { path, setPath: handler, pathOnclick: pathClickHandler, resetPath }; }; //#endregion exports.default = usePath;