@carbon/ibm-products
Version:
Carbon for IBM Products
72 lines (65 loc) • 1.69 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
// Copyright IBM Corp. 2022, 2022
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
const usePath = function () {
let itemsLabel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
const [path, setPath] = React.useState([{
id: 'base_of_path',
title: itemsLabel
}]);
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
};
};
exports.default = usePath;