NodeList.js
9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getMinimumRangeTransitionRange = getMinimumRangeTransitionRange;
exports.default = exports.MotionEntity = exports.MOTION_KEY = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var React = _interopRequireWildcard(require("react"));
var _rcVirtualList = _interopRequireDefault(require("rc-virtual-list"));
var _MotionTreeNode = _interopRequireDefault(require("./MotionTreeNode"));
var _diffUtil = require("./utils/diffUtil");
var _treeUtil = require("./utils/treeUtil");
/**
* Handle virtual list of the TreeNodes.
*/
var HIDDEN_STYLE = {
width: 0,
height: 0,
display: 'flex',
overflow: 'hidden',
opacity: 0,
border: 0,
padding: 0,
margin: 0
};
var noop = function noop() {};
var MOTION_KEY = "RC_TREE_MOTION_".concat(Math.random());
exports.MOTION_KEY = MOTION_KEY;
var MotionNode = {
key: MOTION_KEY
};
var MotionEntity = {
key: MOTION_KEY,
level: 0,
index: 0,
pos: '0',
node: MotionNode
};
exports.MotionEntity = MotionEntity;
var MotionFlattenData = {
parent: null,
children: [],
pos: MotionEntity.pos,
data: MotionNode,
/** Hold empty list here since we do not use it */
isStart: [],
isEnd: []
};
/**
* We only need get visible content items to play the animation.
*/
function getMinimumRangeTransitionRange(list, virtual, height, itemHeight) {
if (virtual === false || !height) {
return list;
}
return list.slice(0, Math.ceil(height / itemHeight) + 1);
}
function itemKey(item) {
var key = item.data.key,
pos = item.pos;
return (0, _treeUtil.getKey)(key, pos);
}
function getAccessibilityPath(item) {
var path = String(item.data.key);
var current = item;
while (current.parent) {
current = current.parent;
path = "".concat(current.data.key, " > ").concat(path);
}
return path;
}
var RefNodeList = function RefNodeList(props, ref) {
var prefixCls = props.prefixCls,
data = props.data,
selectable = props.selectable,
checkable = props.checkable,
expandedKeys = props.expandedKeys,
selectedKeys = props.selectedKeys,
checkedKeys = props.checkedKeys,
loadedKeys = props.loadedKeys,
loadingKeys = props.loadingKeys,
halfCheckedKeys = props.halfCheckedKeys,
keyEntities = props.keyEntities,
disabled = props.disabled,
dragging = props.dragging,
dragOverNodeKey = props.dragOverNodeKey,
dropPosition = props.dropPosition,
motion = props.motion,
height = props.height,
itemHeight = props.itemHeight,
virtual = props.virtual,
focusable = props.focusable,
activeItem = props.activeItem,
focused = props.focused,
tabIndex = props.tabIndex,
onKeyDown = props.onKeyDown,
onFocus = props.onFocus,
onBlur = props.onBlur,
onActiveChange = props.onActiveChange,
onListChangeStart = props.onListChangeStart,
onListChangeEnd = props.onListChangeEnd,
domProps = (0, _objectWithoutProperties2.default)(props, ["prefixCls", "data", "selectable", "checkable", "expandedKeys", "selectedKeys", "checkedKeys", "loadedKeys", "loadingKeys", "halfCheckedKeys", "keyEntities", "disabled", "dragging", "dragOverNodeKey", "dropPosition", "motion", "height", "itemHeight", "virtual", "focusable", "activeItem", "focused", "tabIndex", "onKeyDown", "onFocus", "onBlur", "onActiveChange", "onListChangeStart", "onListChangeEnd"]); // =============================== Ref ================================
var listRef = React.useRef(null);
var indentMeasurerRef = React.useRef(null);
React.useImperativeHandle(ref, function () {
return {
scrollTo: function scrollTo(scroll) {
listRef.current.scrollTo(scroll);
},
getIndentWidth: function getIndentWidth() {
return indentMeasurerRef.current.offsetWidth;
}
};
}); // ============================== Motion ==============================
var _React$useState = React.useState(expandedKeys),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
prevExpandedKeys = _React$useState2[0],
setPrevExpandedKeys = _React$useState2[1];
var _React$useState3 = React.useState(data),
_React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
prevData = _React$useState4[0],
setPrevData = _React$useState4[1];
var _React$useState5 = React.useState(data),
_React$useState6 = (0, _slicedToArray2.default)(_React$useState5, 2),
transitionData = _React$useState6[0],
setTransitionData = _React$useState6[1];
var _React$useState7 = React.useState([]),
_React$useState8 = (0, _slicedToArray2.default)(_React$useState7, 2),
transitionRange = _React$useState8[0],
setTransitionRange = _React$useState8[1];
var _React$useState9 = React.useState(null),
_React$useState10 = (0, _slicedToArray2.default)(_React$useState9, 2),
motionType = _React$useState10[0],
setMotionType = _React$useState10[1];
function onMotionEnd() {
setPrevData(data);
setTransitionData(data);
setTransitionRange([]);
setMotionType(null);
onListChangeEnd();
} // Do animation if expanded keys changed
React.useEffect(function () {
setPrevExpandedKeys(expandedKeys);
var diffExpanded = (0, _diffUtil.findExpandedKeys)(prevExpandedKeys, expandedKeys);
if (diffExpanded.key !== null) {
if (diffExpanded.add) {
var keyIndex = prevData.findIndex(function (_ref) {
var key = _ref.data.key;
return key === diffExpanded.key;
});
var rangeNodes = getMinimumRangeTransitionRange((0, _diffUtil.getExpandRange)(prevData, data, diffExpanded.key), virtual, height, itemHeight);
var newTransitionData = prevData.slice();
newTransitionData.splice(keyIndex + 1, 0, MotionFlattenData);
setTransitionData(newTransitionData);
setTransitionRange(rangeNodes);
setMotionType('show');
} else {
var _keyIndex = data.findIndex(function (_ref2) {
var key = _ref2.data.key;
return key === diffExpanded.key;
});
var _rangeNodes = getMinimumRangeTransitionRange((0, _diffUtil.getExpandRange)(data, prevData, diffExpanded.key), virtual, height, itemHeight);
var _newTransitionData = data.slice();
_newTransitionData.splice(_keyIndex + 1, 0, MotionFlattenData);
setTransitionData(_newTransitionData);
setTransitionRange(_rangeNodes);
setMotionType('hide');
}
} else if (prevData !== data) {
// If whole data changed, we just refresh the list
setPrevData(data);
setTransitionData(data);
}
}, [expandedKeys, data]); // We should clean up motion if is changed by dragging
React.useEffect(function () {
if (!dragging) {
onMotionEnd();
}
}, [dragging]);
var mergedData = motion ? transitionData : data;
var treeNodeRequiredProps = {
expandedKeys: expandedKeys,
selectedKeys: selectedKeys,
loadedKeys: loadedKeys,
loadingKeys: loadingKeys,
checkedKeys: checkedKeys,
halfCheckedKeys: halfCheckedKeys,
dragOverNodeKey: dragOverNodeKey,
dropPosition: dropPosition,
keyEntities: keyEntities
};
return /*#__PURE__*/React.createElement(React.Fragment, null, focused && activeItem && /*#__PURE__*/React.createElement("span", {
style: HIDDEN_STYLE,
"aria-live": "assertive"
}, getAccessibilityPath(activeItem)), /*#__PURE__*/React.createElement("div", {
role: "tree"
}, /*#__PURE__*/React.createElement("input", {
style: HIDDEN_STYLE,
disabled: focusable === false || disabled,
tabIndex: focusable !== false ? tabIndex : null,
onKeyDown: onKeyDown,
onFocus: onFocus,
onBlur: onBlur,
value: "",
onChange: noop
})), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-treenode"),
"aria-hidden": true,
style: {
position: 'absolute',
pointerEvents: 'none',
visibility: 'hidden',
height: 0,
overflow: 'hidden'
}
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-indent")
}, /*#__PURE__*/React.createElement("div", {
ref: indentMeasurerRef,
className: "".concat(prefixCls, "-indent-unit")
}))), /*#__PURE__*/React.createElement(_rcVirtualList.default, Object.assign({}, domProps, {
data: mergedData,
itemKey: itemKey,
height: height,
fullHeight: false,
virtual: virtual,
itemHeight: itemHeight,
prefixCls: "".concat(prefixCls, "-list"),
ref: listRef
}), function (treeNode) {
var pos = treeNode.pos,
_treeNode$data = treeNode.data,
key = _treeNode$data.key,
restProps = (0, _objectWithoutProperties2.default)(_treeNode$data, ["key"]),
isStart = treeNode.isStart,
isEnd = treeNode.isEnd;
var mergedKey = (0, _treeUtil.getKey)(key, pos);
delete restProps.children;
var treeNodeProps = (0, _treeUtil.getTreeNodeProps)(mergedKey, treeNodeRequiredProps);
return /*#__PURE__*/React.createElement(_MotionTreeNode.default, Object.assign({}, restProps, treeNodeProps, {
active: !!activeItem && key === activeItem.data.key,
pos: pos,
data: treeNode.data,
isStart: isStart,
isEnd: isEnd,
motion: motion,
motionNodes: key === MOTION_KEY ? transitionRange : null,
motionType: motionType,
onMotionStart: onListChangeStart,
onMotionEnd: onMotionEnd,
treeNodeRequiredProps: treeNodeRequiredProps,
onMouseMove: function onMouseMove() {
onActiveChange(null);
}
}));
}));
};
var NodeList = /*#__PURE__*/React.forwardRef(RefNodeList);
NodeList.displayName = 'NodeList';
var _default = NodeList;
exports.default = _default;