UNPKG

orcs-design-system

Version:
92 lines (88 loc) 3.77 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /** * Utility functions for managing navigation items in SideNavV2 * * Provides functions for categorizing, filtering, and analyzing navigation items * based on their properties and current state. */ /** * Categorizes navigation items into different sections based on their properties * * Groups items into top-aligned, page-specific, and bottom-aligned sections * for proper layout and positioning in the navigation. * * @param {Array} items - Array of navigation items to categorize * @returns {Object} Object containing categorized items * @returns {Array} returns.topAlignedItems - Items for top section * @returns {Array} returns.pageSpecificItems - Items for page-specific section * @returns {Array} returns.bottomAlignedItems - Items for bottom section * @returns {Array} returns.allItems - All items with index added */ export const categorizeItems = items => { if (!Array.isArray(items)) { console.warn("categorizeItems: items parameter must be an array"); return { topAlignedItems: [], pageSpecificItems: [], bottomAlignedItems: [], allItems: [] }; } const allItems = items.map((item, index) => _objectSpread(_objectSpread({}, item), {}, { index })); const topAlignedItems = allItems.filter(item => !item.bottomAligned && !item.pageSpecific); const pageSpecificItems = allItems.filter(item => !item.bottomAligned && item.pageSpecific); const bottomAlignedItems = allItems.filter(item => item.bottomAligned); return { topAlignedItems, pageSpecificItems, bottomAlignedItems, allItems }; }; /** * Filters out hidden items from a list * * @param {Array} items - Array of navigation items to filter * @returns {Array} Filtered items without hidden ones */ export const filterVisibleItems = items => { if (!Array.isArray(items)) { console.warn("filterVisibleItems: items parameter must be an array"); return []; } return items.filter(item => !item.hide); }; /** * Finds the first item that should be expanded by default * * @param {Array} items - Array of navigation items * @returns {number} Index of the first item to expand by default, or -1 if none */ export const findFirstExpandedByDefault = items => { if (!Array.isArray(items)) { console.warn("findFirstExpandedByDefault: items parameter must be an array"); return -1; } return items.findIndex(item => item.isExpandedByDefault && !item.hide); }; /** * Determines if an item should be active based on its type and state * * @param {Object} item - Navigation item to check * @param {number} expandedItem - Currently expanded item index * @returns {boolean} Whether the item should be active */ export const isItemActive = (item, expandedItem) => { if (!item || typeof item !== "object") { console.warn("isItemActive: item parameter must be a valid object"); return false; } if (item.actionType === "link") { return Boolean(item.isActive); } return expandedItem === item.index; };