import _extends from "@babel/runtime/helpers/esm/extends"; import _typeof from "@babel/runtime/helpers/esm/typeof"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _createSuper from "@babel/runtime/helpers/esm/createSuper"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; // TODO: https://www.w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/treeview/treeview-2/treeview-2a.html // Fully accessibility support import classNames from 'classnames'; import KeyCode from "rc-util/es/KeyCode"; import pickAttrs from "rc-util/es/pickAttrs"; import warning from "rc-util/es/warning"; import * as React from 'react'; import { TreeContext } from "./contextTypes"; import DropIndicator from "./DropIndicator"; import NodeList, { MOTION_KEY, MotionEntity } from "./NodeList"; import TreeNode from "./TreeNode"; import { arrAdd, arrDel, calcDropPosition, calcSelectedKeys, conductExpandParent, getDragChildrenKeys, parseCheckedKeys, posToArr } from "./util"; import { conductCheck } from "./utils/conductUtil"; import getEntity from "./utils/keyUtil"; import { convertDataToEntities, convertNodePropsToEventData, convertTreeToData, fillFieldNames, flattenTreeData, getTreeNodeProps, warningWithoutKey } from "./utils/treeUtil"; var MAX_RETRY_TIMES = 10; var Tree = /*#__PURE__*/function (_React$Component) { _inherits(Tree, _React$Component); var _super = _createSuper(Tree); function Tree() { var _this; _classCallCheck(this, Tree); for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) { _args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(_args)); _defineProperty(_assertThisInitialized(_this), "destroyed", false); _defineProperty(_assertThisInitialized(_this), "delayedDragEnterLogic", void 0); _defineProperty(_assertThisInitialized(_this), "loadingRetryTimes", {}); _defineProperty(_assertThisInitialized(_this), "state", { keyEntities: {}, indent: null, selectedKeys: [], checkedKeys: [], halfCheckedKeys: [], loadedKeys: [], loadingKeys: [], expandedKeys: [], draggingNodeKey: null, dragChildrenKeys: [], // dropTargetKey is the key of abstract-drop-node // the abstract-drop-node is the real drop node when drag and drop // not the DOM drag over node dropTargetKey: null, dropPosition: null, // the drop position of abstract-drop-node, inside 0, top -1, bottom 1 dropContainerKey: null, // the container key of abstract-drop-node if dropPosition is -1 or 1 dropLevelOffset: null, // the drop level offset of abstract-drag-over-node dropTargetPos: null, // the pos of abstract-drop-node dropAllowed: true, // if drop to abstract-drop-node is allowed // the abstract-drag-over-node // if mouse is on the bottom of top dom node or no the top of the bottom dom node // abstract-drag-over-node is the top node dragOverNodeKey: null, treeData: [], flattenNodes: [], focused: false, activeKey: null, listChanging: false, prevProps: null, fieldNames: fillFieldNames() }); _defineProperty(_assertThisInitialized(_this), "dragStartMousePosition", null); _defineProperty(_assertThisInitialized(_this), "dragNodeProps", null); _defineProperty(_assertThisInitialized(_this), "currentMouseOverDroppableNodeKey", null); _defineProperty(_assertThisInitialized(_this), "listRef", /*#__PURE__*/React.createRef()); _defineProperty(_assertThisInitialized(_this), "onNodeDragStart", function (event, nodeProps) { var _this$state = _this.state, expandedKeys = _this$state.expandedKeys, keyEntities = _this$state.keyEntities; var onDragStart = _this.props.onDragStart; var eventKey = nodeProps.eventKey; _this.dragNodeProps = nodeProps; _this.dragStartMousePosition = { x: event.clientX, y: event.clientY }; var newExpandedKeys = arrDel(expandedKeys, eventKey); _this.setState({ draggingNodeKey: eventKey, dragChildrenKeys: getDragChildrenKeys(eventKey, keyEntities), indent: _this.listRef.current.getIndentWidth() }); _this.setExpandedKeys(newExpandedKeys); window.addEventListener('dragend', _this.onWindowDragEnd); onDragStart === null || onDragStart === void 0 || onDragStart({ event: event, node: convertNodePropsToEventData(nodeProps) }); }); /** * [Legacy] Select handler is smaller than node, * so that this will trigger when drag enter node or select handler. * This is a little tricky if customize css without padding. * Better for use mouse move event to refresh drag state. * But let's just keep it to avoid event trigger logic change. */ _defineProperty(_assertThisInitialized(_this), "onNodeDragEnter", function (event, nodeProps) { var _this$state2 = _this.state, expandedKeys = _this$state2.expandedKeys, keyEntities = _this$state2.keyEntities, dragChildrenKeys = _this$state2.dragChildrenKeys, flattenNodes = _this$state2.flattenNodes, indent = _this$state2.indent; var _this$props = _this.props, onDragEnter = _this$props.onDragEnter, onExpand = _this$props.onExpand, allowDrop = _this$props.allowDrop, direction = _this$props.direction; var pos = nodeProps.pos, eventKey = nodeProps.eventKey; // record the key of node which is latest entered, used in dragleave event. if (_this.currentMouseOverDroppableNodeKey !== eventKey) { _this.currentMouseOverDroppableNodeKey = eventKey; } if (!_this.dragNodeProps) { _this.resetDragState(); return; } var _calcDropPosition = calcDropPosition(event, _this.dragNodeProps, nodeProps, indent, _this.dragStartMousePosition, allowDrop, flattenNodes, keyEntities, expandedKeys, direction), dropPosition = _calcDropPosition.dropPosition, dropLevelOffset = _calcDropPosition.dropLevelOffset, dropTargetKey = _calcDropPosition.dropTargetKey, dropContainerKey = _calcDropPosition.dropContainerKey, dropTargetPos = _calcDropPosition.dropTargetPos, dropAllowed = _calcDropPosition.dropAllowed, dragOverNodeKey = _calcDropPosition.dragOverNodeKey; if ( // don't allow drop inside its children dragChildrenKeys.includes(dropTargetKey) || // don't allow drop when drop is not allowed caculated by calcDropPosition !dropAllowed) { _this.resetDragState(); return; } // Side effect for delay drag if (!_this.delayedDragEnterLogic) { _this.delayedDragEnterLogic = {}; } Object.keys(_this.delayedDragEnterLogic).forEach(function (key) { clearTimeout(_this.delayedDragEnterLogic[key]); }); if (_this.dragNodeProps.eventKey !== nodeProps.eventKey) { // hoist expand logic here // since if logic is on the bottom // it will be blocked by abstract dragover node check // => if you dragenter from top, you mouse will still be consider as in the top node event.persist(); _this.delayedDragEnterLogic[pos] = window.setTimeout(function () { if (_this.state.draggingNodeKey === null) { return; } var newExpandedKeys = _toConsumableArray(expandedKeys); var entity = getEntity(keyEntities, nodeProps.eventKey); if (entity && (entity.children || []).length) { newExpandedKeys = arrAdd(expandedKeys, nodeProps.eventKey); } if (!_this.props.hasOwnProperty('expandedKeys')) { _this.setExpandedKeys(newExpandedKeys); } onExpand === null || onExpand === void 0 || onExpand(newExpandedKeys, { node: convertNodePropsToEventData(nodeProps), expanded: true, nativeEvent: event.nativeEvent }); }, 800); } // Skip if drag node is self if (_this.dragNodeProps.eventKey === dropTargetKey && dropLevelOffset === 0) { _this.resetDragState(); return; } // Update drag over node and drag state _this.setState({ dragOverNodeKey: dragOverNodeKey, dropPosition: dropPosition, dropLevelOffset: dropLevelOffset, dropTargetKey: dropTargetKey, dropContainerKey: dropContainerKey, dropTargetPos: dropTargetPos, dropAllowed: dropAllowed }); onDragEnter === null || onDragEnter === void 0 || onDragEnter({ event: event, node: convertNodePropsToEventData(nodeProps), expandedKeys: expandedKeys }); }); _defineProperty(_assertThisInitialized(_this), "onNodeDragOver", function (event, nodeProps) { var _this$state3 = _this.state, dragChildrenKeys = _this$state3.dragChildrenKeys, flattenNodes = _this$state3.flattenNodes, keyEntities = _this$state3.keyEntities, expandedKeys = _this$state3.expandedKeys, indent = _this$state3.indent; var _this$props2 = _this.props, onDragOver = _this$props2.onDragOver, allowDrop = _this$props2.allowDrop, direction = _this$props2.direction; if (!_this.dragNodeProps) { return; } var _calcDropPosition2 = calcDropPosition(event, _this.dragNodeProps, nodeProps, indent, _this.dragStartMousePosition, allowDrop, flattenNodes, keyEntities, expandedKeys, direction), dropPosition = _calcDropPosition2.dropPosition, dropLevelOffset = _calcDropPosition2.dropLevelOffset, dropTargetKey = _calcDropPosition2.dropTargetKey, dropContainerKey = _calcDropPosition2.dropContainerKey, dropTargetPos = _calcDropPosition2.dropTargetPos, dropAllowed = _calcDropPosition2.dropAllowed, dragOverNodeKey = _calcDropPosition2.dragOverNodeKey; if (dragChildrenKeys.includes(dropTargetKey) || !dropAllowed) { // don't allow drop inside its children // don't allow drop when drop is not allowed calculated by calcDropPosition return; } // Update drag position if (_this.dragNodeProps.eventKey === dropTargetKey && dropLevelOffset === 0) { if (!(_this.state.dropPosition === null && _this.state.dropLevelOffset === null && _this.state.dropTargetKey === null && _this.state.dropContainerKey === null && _this.state.dropTargetPos === null && _this.state.dropAllowed === false && _this.state.dragOverNodeKey === null)) { _this.resetDragState(); } } else if (!(dropPosition === _this.state.dropPosition && dropLevelOffset === _this.state.dropLevelOffset && dropTargetKey === _this.state.dropTargetKey && dropContainerKey === _this.state.dropContainerKey && dropTargetPos === _this.state.dropTargetPos && dropAllowed === _this.state.dropAllowed && dragOverNodeKey === _this.state.dragOverNodeKey)) { _this.setState({ dropPosition: dropPosition, dropLevelOffset: dropLevelOffset, dropTargetKey: dropTargetKey, dropContainerKey: dropContainerKey, dropTargetPos: dropTargetPos, dropAllowed: dropAllowed, dragOverNodeKey: dragOverNodeKey }); } onDragOver === null || onDragOver === void 0 || onDragOver({ event: event, node: convertNodePropsToEventData(nodeProps) }); }); _defineProperty(_assertThisInitialized(_this), "onNodeDragLeave", function (event, nodeProps) { // if it is outside the droppable area // currentMouseOverDroppableNodeKey will be updated in dragenter event when into another droppable receiver. if (_this.currentMouseOverDroppableNodeKey === nodeProps.eventKey && !event.currentTarget.contains(event.relatedTarget)) { _this.resetDragState(); _this.currentMouseOverDroppableNodeKey = null; } var onDragLeave = _this.props.onDragLeave; onDragLeave === null || onDragLeave === void 0 || onDragLeave({ event: event, node: convertNodePropsToEventData(nodeProps) }); }); // since stopPropagation() is called in treeNode // if onWindowDrag is called, whice means state is keeped, drag state should be cleared _defineProperty(_assertThisInitialized(_this), "onWindowDragEnd", function (event) { _this.onNodeDragEnd(event, null, true); window.removeEventListener('dragend', _this.onWindowDragEnd); }); // if onNodeDragEnd is called, onWindowDragEnd won't be called since stopPropagation() is called _defineProperty(_assertThisInitialized(_this), "onNodeDragEnd", function (event, nodeProps) { var onDragEnd = _this.props.onDragEnd; _this.setState({ dragOverNodeKey: null }); _this.cleanDragState(); onDragEnd === null || onDragEnd === void 0 || onDragEnd({ event: event, node: convertNodePropsToEventData(nodeProps) }); _this.dragNodeProps = null; window.removeEventListener('dragend', _this.onWindowDragEnd); }); _defineProperty(_assertThisInitialized(_this), "onNodeDrop", function (event, _) { var _this$getActiveItem; var outsideTree = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var _this$state4 = _this.state, dragChildrenKeys = _this$state4.dragChildrenKeys, dropPosition = _this$state4.dropPosition, dropTargetKey = _this$state4.dropTargetKey, dropTargetPos = _this$state4.dropTargetPos, dropAllowed = _this$state4.dropAllowed; if (!dropAllowed) { return; } var onDrop = _this.props.onDrop; _this.setState({ dragOverNodeKey: null }); _this.cleanDragState(); if (dropTargetKey === null) return; var abstractDropNodeProps = _objectSpread(_objectSpread({}, getTreeNodeProps(dropTargetKey, _this.getTreeNodeRequiredProps())), {}, { active: ((_this$getActiveItem = _this.getActiveItem()) === null || _this$getActiveItem === void 0 ? void 0 : _this$getActiveItem.key) === dropTargetKey, data: getEntity(_this.state.keyEntities, dropTargetKey).node }); var dropToChild = dragChildrenKeys.includes(dropTargetKey); warning(!dropToChild, "Can not drop to dragNode's children node. This is a bug of rc-tree. Please report an issue."); var posArr = posToArr(dropTargetPos); var dropResult = { event: event, node: convertNodePropsToEventData(abstractDropNodeProps), dragNode: _this.dragNodeProps ? convertNodePropsToEventData(_this.dragNodeProps) : null, dragNodesKeys: [_this.dragNodeProps.eventKey].concat(dragChildrenKeys), dropToGap: dropPosition !== 0, dropPosition: dropPosition + Number(posArr[posArr.length - 1]) }; if (!outsideTree) { onDrop === null || onDrop === void 0 || onDrop(dropResult); } _this.dragNodeProps = null; }); _defineProperty(_assertThisInitialized(_this), "cleanDragState", function () { var draggingNodeKey = _this.state.draggingNodeKey; if (draggingNodeKey !== null) { _this.setState({ draggingNodeKey: null, dropPosition: null, dropContainerKey: null, dropTargetKey: null, dropLevelOffset: null, dropAllowed: true, dragOverNodeKey: null }); } _this.dragStartMousePosition = null; _this.currentMouseOverDroppableNodeKey = null; }); _defineProperty(_assertThisInitialized(_this), "triggerExpandActionExpand", function (e, treeNode) { var _this$state5 = _this.state, expandedKeys = _this$state5.expandedKeys, flattenNodes = _this$state5.flattenNodes; var expanded = treeNode.expanded, key = treeNode.key, isLeaf = treeNode.isLeaf; if (isLeaf || e.shiftKey || e.metaKey || e.ctrlKey) { return; } var node = flattenNodes.filter(function (nodeItem) { return nodeItem.key === key; })[0]; var eventNode = convertNodePropsToEventData(_objectSpread(_objectSpread({}, getTreeNodeProps(key, _this.getTreeNodeRequiredProps())), {}, { data: node.data })); _this.setExpandedKeys(expanded ? arrDel(expandedKeys, key) : arrAdd(expandedKeys, key)); _this.onNodeExpand(e, eventNode); }); _defineProperty(_assertThisInitialized(_this), "onNodeClick", function (e, treeNode) { var _this$props3 = _this.props, onClick = _this$props3.onClick, expandAction = _this$props3.expandAction; if (expandAction === 'click') { _this.triggerExpandActionExpand(e, treeNode); } onClick === null || onClick === void 0 || onClick(e, treeNode); }); _defineProperty(_assertThisInitialized(_this), "onNodeDoubleClick", function (e, treeNode) { var _this$props4 = _this.props, onDoubleClick = _this$props4.onDoubleClick, expandAction = _this$props4.expandAction; if (expandAction === 'doubleClick') { _this.triggerExpandActionExpand(e, treeNode); } onDoubleClick === null || onDoubleClick === void 0 || onDoubleClick(e, treeNode); }); _defineProperty(_assertThisInitialized(_this), "onNodeSelect", function (e, treeNode) { var selectedKeys = _this.state.selectedKeys; var _this$state6 = _this.state, keyEntities = _this$state6.keyEntities, fieldNames = _this$state6.fieldNames; var _this$props5 = _this.props, onSelect = _this$props5.onSelect, multiple = _this$props5.multiple; var selected = treeNode.selected; var key = treeNode[fieldNames.key]; var targetSelected = !selected; // Update selected keys if (!targetSelected) { selectedKeys = arrDel(selectedKeys, key); } else if (!multiple) { selectedKeys = [key]; } else { selectedKeys = arrAdd(selectedKeys, key); } // [Legacy] Not found related usage in doc or upper libs var selectedNodes = selectedKeys.map(function (selectedKey) { var entity = getEntity(keyEntities, selectedKey); return entity ? entity.node : null; }).filter(Boolean); _this.setUncontrolledState({ selectedKeys: selectedKeys }); onSelect === null || onSelect === void 0 || onSelect(selectedKeys, { event: 'select', selected: targetSelected, node: treeNode, selectedNodes: selectedNodes, nativeEvent: e.nativeEvent }); }); _defineProperty(_assertThisInitialized(_this), "onNodeCheck", function (e, treeNode, checked) { var _this$state7 = _this.state, keyEntities = _this$state7.keyEntities, oriCheckedKeys = _this$state7.checkedKeys, oriHalfCheckedKeys = _this$state7.halfCheckedKeys; var _this$props6 = _this.props, checkStrictly = _this$props6.checkStrictly, onCheck = _this$props6.onCheck; var key = treeNode.key; // Prepare trigger arguments var checkedObj; var eventObj = { event: 'check', node: treeNode, checked: checked, nativeEvent: e.nativeEvent }; if (checkStrictly) { var checkedKeys = checked ? arrAdd(oriCheckedKeys, key) : arrDel(oriCheckedKeys, key); var halfCheckedKeys = arrDel(oriHalfCheckedKeys, key); checkedObj = { checked: checkedKeys, halfChecked: halfCheckedKeys }; eventObj.checkedNodes = checkedKeys.map(function (checkedKey) { return getEntity(keyEntities, checkedKey); }).filter(Boolean).map(function (entity) { return entity.node; }); _this.setUncontrolledState({ checkedKeys: checkedKeys }); } else { // Always fill first var _conductCheck = conductCheck([].concat(_toConsumableArray(oriCheckedKeys), [key]), true, keyEntities), _checkedKeys = _conductCheck.checkedKeys, _halfCheckedKeys = _conductCheck.halfCheckedKeys; // If remove, we do it again to correction if (!checked) { var keySet = new Set(_checkedKeys); keySet.delete(key); var _conductCheck2 = conductCheck(Array.from(keySet), { checked: false, halfCheckedKeys: _halfCheckedKeys }, keyEntities); _checkedKeys = _conductCheck2.checkedKeys; _halfCheckedKeys = _conductCheck2.halfCheckedKeys; } checkedObj = _checkedKeys; // [Legacy] This is used for `rc-tree-select` eventObj.checkedNodes = []; eventObj.checkedNodesPositions = []; eventObj.halfCheckedKeys = _halfCheckedKeys; _checkedKeys.forEach(function (checkedKey) { var entity = getEntity(keyEntities, checkedKey); if (!entity) return; var node = entity.node, pos = entity.pos; eventObj.checkedNodes.push(node); eventObj.checkedNodesPositions.push({ node: node, pos: pos }); }); _this.setUncontrolledState({ checkedKeys: _checkedKeys }, false, { halfCheckedKeys: _halfCheckedKeys }); } onCheck === null || onCheck === void 0 || onCheck(checkedObj, eventObj); }); _defineProperty(_assertThisInitialized(_this), "onNodeLoad", function (treeNode) { var _entity$children; var key = treeNode.key; var keyEntities = _this.state.keyEntities; // Skip if has children already var entity = getEntity(keyEntities, key); if (entity !== null && entity !== void 0 && (_entity$children = entity.children) !== null && _entity$children !== void 0 && _entity$children.length) { return; } var loadPromise = new Promise(function (resolve, reject) { // We need to get the latest state of loading/loaded keys _this.setState(function (_ref) { var _ref$loadedKeys = _ref.loadedKeys, loadedKeys = _ref$loadedKeys === void 0 ? [] : _ref$loadedKeys, _ref$loadingKeys = _ref.loadingKeys, loadingKeys = _ref$loadingKeys === void 0 ? [] : _ref$loadingKeys; var _this$props7 = _this.props, loadData = _this$props7.loadData, onLoad = _this$props7.onLoad; if (!loadData || loadedKeys.includes(key) || loadingKeys.includes(key)) { return null; } // Process load data var promise = loadData(treeNode); promise.then(function () { var currentLoadedKeys = _this.state.loadedKeys; var newLoadedKeys = arrAdd(currentLoadedKeys, key); // onLoad should trigger before internal setState to avoid `loadData` trigger twice. // https://github.com/ant-design/ant-design/issues/12464 onLoad === null || onLoad === void 0 || onLoad(newLoadedKeys, { event: 'load', node: treeNode }); _this.setUncontrolledState({ loadedKeys: newLoadedKeys }); _this.setState(function (prevState) { return { loadingKeys: arrDel(prevState.loadingKeys, key) }; }); resolve(); }).catch(function (e) { _this.setState(function (prevState) { return { loadingKeys: arrDel(prevState.loadingKeys, key) }; }); // If exceed max retry times, we give up retry _this.loadingRetryTimes[key] = (_this.loadingRetryTimes[key] || 0) + 1; if (_this.loadingRetryTimes[key] >= MAX_RETRY_TIMES) { var currentLoadedKeys = _this.state.loadedKeys; warning(false, 'Retry for `loadData` many times but still failed. No more retry.'); _this.setUncontrolledState({ loadedKeys: arrAdd(currentLoadedKeys, key) }); resolve(); } reject(e); }); return { loadingKeys: arrAdd(loadingKeys, key) }; }); }); // Not care warning if we ignore this loadPromise.catch(function () {}); return loadPromise; }); _defineProperty(_assertThisInitialized(_this), "onNodeMouseEnter", function (event, node) { var onMouseEnter = _this.props.onMouseEnter; onMouseEnter === null || onMouseEnter === void 0 || onMouseEnter({ event: event, node: node }); }); _defineProperty(_assertThisInitialized(_this), "onNodeMouseLeave", function (event, node) { var onMouseLeave = _this.props.onMouseLeave; onMouseLeave === null || onMouseLeave === void 0 || onMouseLeave({ event: event, node: node }); }); _defineProperty(_assertThisInitialized(_this), "onNodeContextMenu", function (event, node) { var onRightClick = _this.props.onRightClick; if (onRightClick) { event.preventDefault(); onRightClick({ event: event, node: node }); } }); _defineProperty(_assertThisInitialized(_this), "onFocus", function () { var onFocus = _this.props.onFocus; _this.setState({ focused: true }); for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } onFocus === null || onFocus === void 0 || onFocus.apply(void 0, args); }); _defineProperty(_assertThisInitialized(_this), "onBlur", function () { var onBlur = _this.props.onBlur; _this.setState({ focused: false }); _this.onActiveChange(null); for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } onBlur === null || onBlur === void 0 || onBlur.apply(void 0, args); }); _defineProperty(_assertThisInitialized(_this), "getTreeNodeRequiredProps", function () { var _this$state8 = _this.state, expandedKeys = _this$state8.expandedKeys, selectedKeys = _this$state8.selectedKeys, loadedKeys = _this$state8.loadedKeys, loadingKeys = _this$state8.loadingKeys, checkedKeys = _this$state8.checkedKeys, halfCheckedKeys = _this$state8.halfCheckedKeys, dragOverNodeKey = _this$state8.dragOverNodeKey, dropPosition = _this$state8.dropPosition, keyEntities = _this$state8.keyEntities; return { expandedKeys: expandedKeys || [], selectedKeys: selectedKeys || [], loadedKeys: loadedKeys || [], loadingKeys: loadingKeys || [], checkedKeys: checkedKeys || [], halfCheckedKeys: halfCheckedKeys || [], dragOverNodeKey: dragOverNodeKey, dropPosition: dropPosition, keyEntities: keyEntities }; }); // =========================== Expanded =========================== /** Set uncontrolled `expandedKeys`. This will also auto update `flattenNodes`. */ _defineProperty(_assertThisInitialized(_this), "setExpandedKeys", function (expandedKeys) { var _this$state9 = _this.state, treeData = _this$state9.treeData, fieldNames = _this$state9.fieldNames; var flattenNodes = flattenTreeData(treeData, expandedKeys, fieldNames); _this.setUncontrolledState({ expandedKeys: expandedKeys, flattenNodes: flattenNodes }, true); }); _defineProperty(_assertThisInitialized(_this), "onNodeExpand", function (e, treeNode) { var expandedKeys = _this.state.expandedKeys; var _this$state10 = _this.state, listChanging = _this$state10.listChanging, fieldNames = _this$state10.fieldNames; var _this$props8 = _this.props, onExpand = _this$props8.onExpand, loadData = _this$props8.loadData; var expanded = treeNode.expanded; var key = treeNode[fieldNames.key]; // Do nothing when motion is in progress if (listChanging) { return; } // Update selected keys var certain = expandedKeys.includes(key); var targetExpanded = !expanded; warning(expanded && certain || !expanded && !certain, 'Expand state not sync with index check'); expandedKeys = targetExpanded ? arrAdd(expandedKeys, key) : arrDel(expandedKeys, key); _this.setExpandedKeys(expandedKeys); onExpand === null || onExpand === void 0 || onExpand(expandedKeys, { node: treeNode, expanded: targetExpanded, nativeEvent: e.nativeEvent }); // Async Load data if (targetExpanded && loadData) { var loadPromise = _this.onNodeLoad(treeNode); if (loadPromise) { loadPromise.then(function () { // [Legacy] Refresh logic var newFlattenTreeData = flattenTreeData(_this.state.treeData, expandedKeys, fieldNames); _this.setUncontrolledState({ flattenNodes: newFlattenTreeData }); }).catch(function () { var currentExpandedKeys = _this.state.expandedKeys; var expandedKeysToRestore = arrDel(currentExpandedKeys, key); _this.setExpandedKeys(expandedKeysToRestore); }); } } }); _defineProperty(_assertThisInitialized(_this), "onListChangeStart", function () { _this.setUncontrolledState({ listChanging: true }); }); _defineProperty(_assertThisInitialized(_this), "onListChangeEnd", function () { setTimeout(function () { _this.setUncontrolledState({ listChanging: false }); }); }); // =========================== Keyboard =========================== _defineProperty(_assertThisInitialized(_this), "onActiveChange", function (newActiveKey) { var activeKey = _this.state.activeKey; var _this$props9 = _this.props, onActiveChange = _this$props9.onActiveChange, _this$props9$itemScro = _this$props9.itemScrollOffset, itemScrollOffset = _this$props9$itemScro === void 0 ? 0 : _this$props9$itemScro; if (activeKey === newActiveKey) { return; } _this.setState({ activeKey: newActiveKey }); if (newActiveKey !== null) { _this.scrollTo({ key: newActiveKey, offset: itemScrollOffset }); } onActiveChange === null || onActiveChange === void 0 || onActiveChange(newActiveKey); }); _defineProperty(_assertThisInitialized(_this), "getActiveItem", function () { var _this$state11 = _this.state, activeKey = _this$state11.activeKey, flattenNodes = _this$state11.flattenNodes; if (activeKey === null) { return null; } return flattenNodes.find(function (_ref2) { var key = _ref2.key; return key === activeKey; }) || null; }); _defineProperty(_assertThisInitialized(_this), "offsetActiveKey", function (offset) { var _this$state12 = _this.state, flattenNodes = _this$state12.flattenNodes, activeKey = _this$state12.activeKey; var index = flattenNodes.findIndex(function (_ref3) { var key = _ref3.key; return key === activeKey; }); // Align with index if (index === -1 && offset < 0) { index = flattenNodes.length; } index = (index + offset + flattenNodes.length) % flattenNodes.length; var item = flattenNodes[index]; if (item) { var _key4 = item.key; _this.onActiveChange(_key4); } else { _this.onActiveChange(null); } }); _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) { var _this$state13 = _this.state, activeKey = _this$state13.activeKey, expandedKeys = _this$state13.expandedKeys, checkedKeys = _this$state13.checkedKeys, fieldNames = _this$state13.fieldNames; var _this$props10 = _this.props, onKeyDown = _this$props10.onKeyDown, checkable = _this$props10.checkable, selectable = _this$props10.selectable; // >>>>>>>>>> Direction switch (event.which) { case KeyCode.UP: { _this.offsetActiveKey(-1); event.preventDefault(); break; } case KeyCode.DOWN: { _this.offsetActiveKey(1); event.preventDefault(); break; } } // >>>>>>>>>> Expand & Selection var activeItem = _this.getActiveItem(); if (activeItem && activeItem.data) { var treeNodeRequiredProps = _this.getTreeNodeRequiredProps(); var expandable = activeItem.data.isLeaf === false || !!(activeItem.data[fieldNames.children] || []).length; var eventNode = convertNodePropsToEventData(_objectSpread(_objectSpread({}, getTreeNodeProps(activeKey, treeNodeRequiredProps)), {}, { data: activeItem.data, active: true })); switch (event.which) { // >>> Expand case KeyCode.LEFT: { // Collapse if possible if (expandable && expandedKeys.includes(activeKey)) { _this.onNodeExpand({}, eventNode); } else if (activeItem.parent) { _this.onActiveChange(activeItem.parent.key); } event.preventDefault(); break; } case KeyCode.RIGHT: { // Expand if possible if (expandable && !expandedKeys.includes(activeKey)) { _this.onNodeExpand({}, eventNode); } else if (activeItem.children && activeItem.children.length) { _this.onActiveChange(activeItem.children[0].key); } event.preventDefault(); break; } // Selection case KeyCode.ENTER: case KeyCode.SPACE: { if (checkable && !eventNode.disabled && eventNode.checkable !== false && !eventNode.disableCheckbox) { _this.onNodeCheck({}, eventNode, !checkedKeys.includes(activeKey)); } else if (!checkable && selectable && !eventNode.disabled && eventNode.selectable !== false) { _this.onNodeSelect({}, eventNode); } break; } } } onKeyDown === null || onKeyDown === void 0 || onKeyDown(event); }); /** * Only update the value which is not in props */ _defineProperty(_assertThisInitialized(_this), "setUncontrolledState", function (state) { var atomic = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var forceState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; if (!_this.destroyed) { var needSync = false; var allPassed = true; var newState = {}; Object.keys(state).forEach(function (name) { if (_this.props.hasOwnProperty(name)) { allPassed = false; return; } needSync = true; newState[name] = state[name]; }); if (needSync && (!atomic || allPassed)) { _this.setState(_objectSpread(_objectSpread({}, newState), forceState)); } } }); _defineProperty(_assertThisInitialized(_this), "scrollTo", function (scroll) { _this.listRef.current.scrollTo(scroll); }); return _this; } _createClass(Tree, [{ key: "componentDidMount", value: function componentDidMount() { this.destroyed = false; this.onUpdated(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.onUpdated(); } }, { key: "onUpdated", value: function onUpdated() { var _this$props11 = this.props, activeKey = _this$props11.activeKey, _this$props11$itemScr = _this$props11.itemScrollOffset, itemScrollOffset = _this$props11$itemScr === void 0 ? 0 : _this$props11$itemScr; if (activeKey !== undefined && activeKey !== this.state.activeKey) { this.setState({ activeKey: activeKey }); if (activeKey !== null) { this.scrollTo({ key: activeKey, offset: itemScrollOffset }); } } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('dragend', this.onWindowDragEnd); this.destroyed = true; } }, { key: "resetDragState", value: function resetDragState() { this.setState({ dragOverNodeKey: null, dropPosition: null, dropLevelOffset: null, dropTargetKey: null, dropContainerKey: null, dropTargetPos: null, dropAllowed: false }); } }, { key: "render", value: function render() { var _this$state14 = this.state, focused = _this$state14.focused, flattenNodes = _this$state14.flattenNodes, keyEntities = _this$state14.keyEntities, draggingNodeKey = _this$state14.draggingNodeKey, activeKey = _this$state14.activeKey, dropLevelOffset = _this$state14.dropLevelOffset, dropContainerKey = _this$state14.dropContainerKey, dropTargetKey = _this$state14.dropTargetKey, dropPosition = _this$state14.dropPosition, dragOverNodeKey = _this$state14.dragOverNodeKey, indent = _this$state14.indent; var _this$props12 = this.props, prefixCls = _this$props12.prefixCls, className = _this$props12.className, style = _this$props12.style, showLine = _this$props12.showLine, focusable = _this$props12.focusable, _this$props12$tabInde = _this$props12.tabIndex, tabIndex = _this$props12$tabInde === void 0 ? 0 : _this$props12$tabInde, selectable = _this$props12.selectable, showIcon = _this$props12.showIcon, icon = _this$props12.icon, switcherIcon = _this$props12.switcherIcon, draggable = _this$props12.draggable, checkable = _this$props12.checkable, checkStrictly = _this$props12.checkStrictly, disabled = _this$props12.disabled, motion = _this$props12.motion, loadData = _this$props12.loadData, filterTreeNode = _this$props12.filterTreeNode, height = _this$props12.height, itemHeight = _this$props12.itemHeight, scrollWidth = _this$props12.scrollWidth, virtual = _this$props12.virtual, titleRender = _this$props12.titleRender, dropIndicatorRender = _this$props12.dropIndicatorRender, onContextMenu = _this$props12.onContextMenu, onScroll = _this$props12.onScroll, direction = _this$props12.direction, rootClassName = _this$props12.rootClassName, rootStyle = _this$props12.rootStyle; var domProps = pickAttrs(this.props, { aria: true, data: true }); // It's better move to hooks but we just simply keep here var draggableConfig; if (draggable) { if (_typeof(draggable) === 'object') { draggableConfig = draggable; } else if (typeof draggable === 'function') { draggableConfig = { nodeDraggable: draggable }; } else { draggableConfig = {}; } } var contextValue = { prefixCls: prefixCls, selectable: selectable, showIcon: showIcon, icon: icon, switcherIcon: switcherIcon, draggable: draggableConfig, draggingNodeKey: draggingNodeKey, checkable: checkable, checkStrictly: checkStrictly, disabled: disabled, keyEntities: keyEntities, dropLevelOffset: dropLevelOffset, dropContainerKey: dropContainerKey, dropTargetKey: dropTargetKey, dropPosition: dropPosition, dragOverNodeKey: dragOverNodeKey, indent: indent, direction: direction, dropIndicatorRender: dropIndicatorRender, loadData: loadData, filterTreeNode: filterTreeNode, titleRender: titleRender, onNodeClick: this.onNodeClick, onNodeDoubleClick: this.onNodeDoubleClick, onNodeExpand: this.onNodeExpand, onNodeSelect: this.onNodeSelect, onNodeCheck: this.onNodeCheck, onNodeLoad: this.onNodeLoad, onNodeMouseEnter: this.onNodeMouseEnter, onNodeMouseLeave: this.onNodeMouseLeave, onNodeContextMenu: this.onNodeContextMenu, onNodeDragStart: this.onNodeDragStart, onNodeDragEnter: this.onNodeDragEnter, onNodeDragOver: this.onNodeDragOver, onNodeDragLeave: this.onNodeDragLeave, onNodeDragEnd: this.onNodeDragEnd, onNodeDrop: this.onNodeDrop }; return /*#__PURE__*/React.createElement(TreeContext.Provider, { value: contextValue }, /*#__PURE__*/React.createElement("div", { className: classNames(prefixCls, className, rootClassName, _defineProperty(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-show-line"), showLine), "".concat(prefixCls, "-focused"), focused), "".concat(prefixCls, "-active-focused"), activeKey !== null)), style: rootStyle }, /*#__PURE__*/React.createElement(NodeList, _extends({ ref: this.listRef, prefixCls: prefixCls, style: style, data: flattenNodes, disabled: disabled, selectable: selectable, checkable: !!checkable, motion: motion, dragging: draggingNodeKey !== null, height: height, itemHeight: itemHeight, virtual: virtual, focusable: focusable, focused: focused, tabIndex: tabIndex, activeItem: this.getActiveItem(), onFocus: this.onFocus, onBlur: this.onBlur, onKeyDown: this.onKeyDown, onActiveChange: this.onActiveChange, onListChangeStart: this.onListChangeStart, onListChangeEnd: this.onListChangeEnd, onContextMenu: onContextMenu, onScroll: onScroll, scrollWidth: scrollWidth }, this.getTreeNodeRequiredProps(), domProps)))); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, prevState) { var prevProps = prevState.prevProps; var newState = { prevProps: props }; function needSync(name) { return !prevProps && props.hasOwnProperty(name) || prevProps && prevProps[name] !== props[name]; } // ================== Tree Node ================== var treeData; // fieldNames var fieldNames = prevState.fieldNames; if (needSync('fieldNames')) { fieldNames = fillFieldNames(props.fieldNames); newState.fieldNames = fieldNames; } // Check if `treeData` or `children` changed and save into the state. if (needSync('treeData')) { treeData = props.treeData; } else if (needSync('children')) { warning(false, '`children` of Tree is deprecated. Please use `treeData` instead.'); treeData = convertTreeToData(props.children); } // Save flatten nodes info and convert `treeData` into keyEntities if (treeData) { newState.treeData = treeData; var entitiesMap = convertDataToEntities(treeData, { fieldNames: fieldNames }); newState.keyEntities = _objectSpread(_defineProperty({}, MOTION_KEY, MotionEntity), entitiesMap.keyEntities); // Warning if treeNode not provide key if (process.env.NODE_ENV !== 'production') { warningWithoutKey(treeData, fieldNames); } } var keyEntities = newState.keyEntities || prevState.keyEntities; // ================ expandedKeys ================= if (needSync('expandedKeys') || prevProps && needSync('autoExpandParent')) { newState.expandedKeys = props.autoExpandParent || !prevProps && props.defaultExpandParent ? conductExpandParent(props.expandedKeys, keyEntities) : props.expandedKeys; } else if (!prevProps && props.defaultExpandAll) { var cloneKeyEntities = _objectSpread({}, keyEntities); delete cloneKeyEntities[MOTION_KEY]; // Only take the key who has the children to enhance the performance var nextExpandedKeys = []; Object.keys(cloneKeyEntities).forEach(function (key) { var entity = cloneKeyEntities[key]; if (entity.children && entity.children.length) { nextExpandedKeys.push(entity.key); } }); newState.expandedKeys = nextExpandedKeys; } else if (!prevProps && props.defaultExpandedKeys) { newState.expandedKeys = props.autoExpandParent || props.defaultExpandParent ? conductExpandParent(props.defaultExpandedKeys, keyEntities) : props.defaultExpandedKeys; } if (!newState.expandedKeys) { delete newState.expandedKeys; } // ================ flattenNodes ================= if (treeData || newState.expandedKeys) { var flattenNodes = flattenTreeData(treeData || prevState.treeData, newState.expandedKeys || prevState.expandedKeys, fieldNames); newState.flattenNodes = flattenNodes; } // ================ selectedKeys ================= if (props.selectable) { if (needSync('selectedKeys')) { newState.selectedKeys = calcSelectedKeys(props.selectedKeys, props); } else if (!prevProps && props.defaultSelectedKeys) { newState.selectedKeys = calcSelectedKeys(props.defaultSelectedKeys, props); } } // ================= checkedKeys ================= if (props.checkable) { var checkedKeyEntity; if (needSync('checkedKeys')) { checkedKeyEntity = parseCheckedKeys(props.checkedKeys) || {}; } else if (!prevProps && props.defaultCheckedKeys) { checkedKeyEntity = parseCheckedKeys(props.defaultCheckedKeys) || {}; } else if (treeData) { // If `treeData` changed, we also need check it checkedKeyEntity = parseCheckedKeys(props.checkedKeys) || { checkedKeys: prevState.checkedKeys, halfCheckedKeys: prevState.halfCheckedKeys }; } if (checkedKeyEntity) { var _checkedKeyEntity = checkedKeyEntity, _checkedKeyEntity$che = _checkedKeyEntity.checkedKeys, checkedKeys = _checkedKeyEntity$che === void 0 ? [] : _checkedKeyEntity$che, _checkedKeyEntity$hal = _checkedKeyEntity.halfCheckedKeys, halfCheckedKeys = _checkedKeyEntity$hal === void 0 ? [] : _checkedKeyEntity$hal; if (!props.checkStrictly) { var conductKeys = conductCheck(checkedKeys, true, keyEntities); checkedKeys = conductKeys.checkedKeys; halfCheckedKeys = conductKeys.halfCheckedKeys; } newState.checkedKeys = checkedKeys; newState.halfCheckedKeys = halfCheckedKeys; } } // ================= loadedKeys ================== if (needSync('loadedKeys')) { newState.loadedKeys = props.loadedKeys; } return newState; } }]); return Tree; }(React.Component); _defineProperty(Tree, "defaultProps", { prefixCls: 'rc-tree', showLine: false, showIcon: true, selectable: true, multiple: false, checkable: false, disabled: false, checkStrictly: false, draggable: false, defaultExpandParent: true, autoExpandParent: false, defaultExpandAll: false, defaultExpandedKeys: [], defaultCheckedKeys: [], defaultSelectedKeys: [], dropIndicatorRender: DropIndicator, allowDrop: function allowDrop() { return true; }, expandAction: false }); _defineProperty(Tree, "TreeNode", TreeNode); export default Tree;