UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

161 lines (160 loc) 5.3 kB
"use client"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import * as React from 'react'; import FileOutlined from "@ant-design/icons/es/icons/FileOutlined"; import FolderOpenOutlined from "@ant-design/icons/es/icons/FolderOpenOutlined"; import FolderOutlined from "@ant-design/icons/es/icons/FolderOutlined"; import { conductExpandParent } from "@rc-component/tree/es/util"; import { convertDataToEntities, convertTreeToData } from "@rc-component/tree/es/utils/treeUtil"; import { clsx } from 'clsx'; import { ConfigContext } from '../config-provider'; import Tree from './Tree'; import { calcRangeKeys, convertDirectoryKeysToNodes } from './utils/dictUtil'; function getIcon(props) { const { isLeaf, expanded } = props; if (isLeaf) { return /*#__PURE__*/React.createElement(FileOutlined, null); } return expanded ? /*#__PURE__*/React.createElement(FolderOpenOutlined, null) : /*#__PURE__*/React.createElement(FolderOutlined, null); } function getTreeData({ treeData, children }) { return treeData || convertTreeToData(children); } const DirectoryTree = ({ defaultExpandAll, defaultExpandParent, defaultExpandedKeys, ...props }, ref) => { // Shift click usage const lastSelectedKey = React.useRef(null); const cachedSelectedKeys = React.useRef(null); const getInitExpandedKeys = () => { const { keyEntities } = convertDataToEntities(getTreeData(props), { fieldNames: props.fieldNames }); let initExpandedKeys; // Expanded keys if (defaultExpandAll) { initExpandedKeys = Object.keys(keyEntities); } else if (defaultExpandParent) { initExpandedKeys = conductExpandParent(props.expandedKeys || defaultExpandedKeys || [], keyEntities); } else { initExpandedKeys = props.expandedKeys || defaultExpandedKeys || []; } return initExpandedKeys; }; const [selectedKeys, setSelectedKeys] = React.useState(props.selectedKeys || props.defaultSelectedKeys || []); const [expandedKeys, setExpandedKeys] = React.useState(() => getInitExpandedKeys()); React.useEffect(() => { if ('selectedKeys' in props) { setSelectedKeys(props.selectedKeys); } }, [props.selectedKeys]); React.useEffect(() => { if ('expandedKeys' in props) { setExpandedKeys(props.expandedKeys); } }, [props.expandedKeys]); const onExpand = (keys, info) => { if (!('expandedKeys' in props)) { setExpandedKeys(keys); } // Call origin function return props.onExpand?.(keys, info); }; const onSelect = (keys, event) => { const { multiple, fieldNames } = props; const { node, nativeEvent } = event; const { key = '' } = node; const treeData = getTreeData(props); // const newState: DirectoryTreeState = {}; // We need wrap this event since some value is not same const newEvent = { ...event, selected: true // Directory selected always true }; // Windows / Mac single pick const ctrlPick = nativeEvent?.ctrlKey || nativeEvent?.metaKey; const shiftPick = nativeEvent?.shiftKey; // Generate new selected keys let newSelectedKeys; if (multiple && ctrlPick) { // Control click newSelectedKeys = keys; lastSelectedKey.current = key; cachedSelectedKeys.current = newSelectedKeys; newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData, newSelectedKeys, fieldNames); } else if (multiple && shiftPick) { // Shift click newSelectedKeys = Array.from(new Set([].concat(_toConsumableArray(cachedSelectedKeys.current || []), _toConsumableArray(calcRangeKeys({ treeData, expandedKeys, startKey: key, endKey: lastSelectedKey.current, fieldNames }))))); newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData, newSelectedKeys, fieldNames); } else { // Single click newSelectedKeys = [key]; lastSelectedKey.current = key; cachedSelectedKeys.current = newSelectedKeys; newEvent.selectedNodes = convertDirectoryKeysToNodes(treeData, newSelectedKeys, fieldNames); } props.onSelect?.(newSelectedKeys, newEvent); if (!('selectedKeys' in props)) { setSelectedKeys(newSelectedKeys); } }; const { getPrefixCls, direction } = React.useContext(ConfigContext); const { prefixCls: customizePrefixCls, className, showIcon = true, expandAction = 'click', ...otherProps } = props; const prefixCls = getPrefixCls('tree', customizePrefixCls); const connectClassName = clsx(`${prefixCls}-directory`, { [`${prefixCls}-directory-rtl`]: direction === 'rtl' }, className); return /*#__PURE__*/React.createElement(Tree, { icon: getIcon, ref: ref, blockNode: true, ...otherProps, showIcon: showIcon, expandAction: expandAction, prefixCls: prefixCls, className: connectClassName, expandedKeys: expandedKeys, selectedKeys: selectedKeys, onSelect: onSelect, onExpand: onExpand }); }; const ForwardDirectoryTree = /*#__PURE__*/React.forwardRef(DirectoryTree); if (process.env.NODE_ENV !== 'production') { ForwardDirectoryTree.displayName = 'DirectoryTree'; } export default ForwardDirectoryTree;