UNPKG

rsuite

Version:

A suite of react components

77 lines (75 loc) 2.56 kB
'use client'; import { useCallback } from 'react'; import intersection from 'lodash/intersection'; import isUndefined from 'lodash/isUndefined'; import { getNodeParentKeys } from "../utils/getNodeParentKeys.js"; import { isSearching } from "../utils/isSearching.js"; import { UNSAFE_flattenTree } from "../utils/flattenTree.js"; import { getNodeCheckState } from "../../CheckTree/utils.js"; import { useItemDataKeys } from "../../internals/Tree/TreeProvider.js"; /** * Determines whether a node should be shown based on the expanded state of its parent nodes. */ function shouldShowNodeByParentExpanded(expandItemValues = [], parentKeys = []) { const intersectionKeys = intersection(expandItemValues, parentKeys); if (intersectionKeys.length === parentKeys.length) { return true; } return false; } function useVirtualizedTreeData(nodes, data, options) { const { childrenKey, valueKey } = useItemDataKeys(); /** * Formats the virtualized tree data. */ return useCallback(() => { const { cascade, searchKeyword, expandItemValues, disabledItemValues = [] } = options; return UNSAFE_flattenTree(data, childrenKey, node => { let formatted = {}; const curNode = nodes === null || nodes === void 0 ? void 0 : nodes[node.refKey]; const parentKeys = getNodeParentKeys(nodes, curNode, valueKey); /** * When using virtualized, * if the parent node is collapsed, the child nodes should be hidden * avoid component height calculation errors */ let visible = curNode !== null && curNode !== void 0 && curNode.parent ? shouldShowNodeByParentExpanded(expandItemValues, parentKeys) : true; /** * when searching, every node default expand * the node's visible should follow the original state */ if (isSearching(searchKeyword)) { visible = node.visible; } if (curNode) { const checkState = !isUndefined(cascade) ? getNodeCheckState(curNode, { cascade, nodes, childrenKey, disabledItemValues, valueKey }) : undefined; formatted = { ...node, check: curNode.check, uncheckable: curNode.uncheckable, hasChildren: !!node[childrenKey], layer: curNode.layer, parent: curNode.parent, checkState, visible }; } return formatted; }); }, [childrenKey, data, nodes, options, valueKey]); } export default useVirtualizedTreeData;