react-cascading-menu
Version:
React-Cascading-Menu is a multi-selectable cascading menu component for React, providing horizontal navigation and global search for efficient option selection.
43 lines (40 loc) • 1.3 kB
text/typescript
import { ItemId, MenuGroupMap, SelectedItemType } from "../types";
export const checkSelections = (
menuObj: MenuGroupMap,
selectionObj: SelectedItemType,
topParentId: ItemId,
isActiveItem: boolean
): boolean => {
// check if the childIds are matching with the actual child options
let ids = [topParentId];
while (ids.length > 0) {
const topmostId = ids.pop();
// console.log("ids", ids, topmostId);
// the id is not present, which means there is an error
if (!topmostId || !selectionObj[topmostId]) {
// console.log("1");
return false;
}
const newIds = selectionObj[topmostId].childIds;
if (topmostId !== topParentId) {
// checks if all the childs are having correct parentId
const isCorrectParent = newIds?.some(
(e) => menuObj[e]?.parentId !== topmostId
);
if (isCorrectParent) {
// console.log("===2===");
return false;
}
}
// if the no of childs are more than 1
if (isActiveItem && newIds && newIds.length > 1) {
// console.log("3", isActiveItem, topmostId, newIds, selectionObj);
return false;
}
// add the newIds to the stack
if (newIds?.length) {
ids = [...ids, ...newIds];
}
}
return true;
};