@hitachivantara/uikit-react-core
Version:
Core React components for the NEXT Design System.
148 lines (147 loc) • 4.17 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const utils = require("@mui/material/utils");
function _interopNamespace(e) {
if (e && e.__esModule) return e;
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespace(React);
function binaryFindElement(array, element) {
let start = 0;
let end = array.length - 1;
while (start <= end) {
const middle = Math.floor((start + end) / 2);
if (array[middle].element === element) {
return middle;
}
if (
// eslint-disable-next-line no-bitwise
array[middle].element.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_PRECEDING
) {
end = middle - 1;
} else {
start = middle + 1;
}
}
return start;
}
const DescendantContext = React__namespace.createContext({
level: 0
});
function usePrevious(value) {
const ref = React__namespace.useRef(null);
React__namespace.useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
const noop = () => {
};
function useDescendant(descendant) {
const [, forceUpdate] = React__namespace.useState();
const {
registerDescendant = noop,
unregisterDescendant = noop,
descendants = [],
parentId = null,
level = 0
} = React__namespace.useContext(DescendantContext);
const index = descendants.findIndex(
(item) => item.element === descendant.element
);
const previousDescendants = usePrevious(descendants);
const someDescendantsHaveChanged = descendants.some(
(newDescendant, position) => {
return previousDescendants?.[position] && previousDescendants[position].element !== newDescendant.element;
}
);
utils.unstable_useEnhancedEffect(() => {
if (descendant.element) {
registerDescendant({
...descendant,
index
});
return () => {
unregisterDescendant(descendant.element);
};
}
forceUpdate({});
return void 0;
}, [
registerDescendant,
unregisterDescendant,
index,
someDescendantsHaveChanged,
descendant
]);
return { parentId, index, level };
}
const DescendantProvider = (props) => {
const { children, id, level } = props;
const [items, set] = React__namespace.useState([]);
const registerDescendant = React__namespace.useCallback(
({ element, ...other }) => {
set((oldItems) => {
if (oldItems.length === 0) {
return [
{
...other,
element,
index: 0
}
];
}
const index = binaryFindElement(oldItems, element);
let newItems;
if (oldItems[index] && oldItems[index].element === element) {
newItems = oldItems;
} else {
const newItem = {
...other,
element,
index
};
newItems = oldItems.slice();
newItems.splice(index, 0, newItem);
}
newItems.forEach((item, position) => {
item.index = position;
});
return newItems;
});
},
[]
);
const unregisterDescendant = React__namespace.useCallback((element) => {
set((oldItems) => oldItems.filter((item) => element !== item.element));
}, []);
const value = React__namespace.useMemo(
() => ({
descendants: items,
registerDescendant,
unregisterDescendant,
parentId: id,
level
}),
[items, registerDescendant, unregisterDescendant, id, level]
);
return /* @__PURE__ */ jsxRuntime.jsx(DescendantContext.Provider, { value, children });
};
exports.DescendantContext = DescendantContext;
exports.DescendantProvider = DescendantProvider;
exports.useDescendant = useDescendant;