DOMWrap.js
11.4 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
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 * as React from 'react';
import * as ReactDOM from 'react-dom';
import ResizeObserver from 'resize-observer-polyfill';
import SubMenu from './SubMenu';
import { getWidth, setStyle, menuAllProps } from './util';
var MENUITEM_OVERFLOWED_CLASSNAME = 'menuitem-overflowed';
var FLOAT_PRECISION_ADJUST = 0.5;
var DOMWrap = /*#__PURE__*/function (_React$Component) {
_inherits(DOMWrap, _React$Component);
var _super = _createSuper(DOMWrap);
function DOMWrap() {
var _this;
_classCallCheck(this, DOMWrap);
_this = _super.apply(this, arguments);
_this.resizeObserver = null;
_this.mutationObserver = null; // original scroll size of the list
_this.originalTotalWidth = 0; // copy of overflowed items
_this.overflowedItems = []; // cache item of the original items (so we can track the size and order)
_this.menuItemSizes = [];
_this.cancelFrameId = null;
_this.state = {
lastVisibleIndex: undefined
}; // get all valid menuItem nodes
_this.getMenuItemNodes = function () {
var prefixCls = _this.props.prefixCls;
var ul = ReactDOM.findDOMNode(_assertThisInitialized(_this));
if (!ul) {
return [];
} // filter out all overflowed indicator placeholder
return [].slice.call(ul.children).filter(function (node) {
return node.className.split(' ').indexOf("".concat(prefixCls, "-overflowed-submenu")) < 0;
});
};
_this.getOverflowedSubMenuItem = function (keyPrefix, overflowedItems, renderPlaceholder) {
var _this$props = _this.props,
overflowedIndicator = _this$props.overflowedIndicator,
level = _this$props.level,
mode = _this$props.mode,
prefixCls = _this$props.prefixCls,
theme = _this$props.theme;
if (level !== 1 || mode !== 'horizontal') {
return null;
} // put all the overflowed item inside a submenu
// with a title of overflow indicator ('...')
var copy = _this.props.children[0];
var _copy$props = copy.props,
throwAway = _copy$props.children,
title = _copy$props.title,
propStyle = _copy$props.style,
rest = _objectWithoutProperties(_copy$props, ["children", "title", "style"]);
var style = _objectSpread({}, propStyle);
var key = "".concat(keyPrefix, "-overflowed-indicator");
var eventKey = "".concat(keyPrefix, "-overflowed-indicator");
if (overflowedItems.length === 0 && renderPlaceholder !== true) {
style = _objectSpread(_objectSpread({}, style), {}, {
display: 'none'
});
} else if (renderPlaceholder) {
style = _objectSpread(_objectSpread({}, style), {}, {
visibility: 'hidden',
// prevent from taking normal dom space
position: 'absolute'
});
key = "".concat(key, "-placeholder");
eventKey = "".concat(eventKey, "-placeholder");
}
var popupClassName = theme ? "".concat(prefixCls, "-").concat(theme) : '';
var props = {};
menuAllProps.forEach(function (k) {
if (rest[k] !== undefined) {
props[k] = rest[k];
}
});
return React.createElement(SubMenu, Object.assign({
title: overflowedIndicator,
className: "".concat(prefixCls, "-overflowed-submenu"),
popupClassName: popupClassName
}, props, {
key: key,
eventKey: eventKey,
disabled: false,
style: style
}), overflowedItems);
}; // memorize rendered menuSize
_this.setChildrenWidthAndResize = function () {
if (_this.props.mode !== 'horizontal') {
return;
}
var ul = ReactDOM.findDOMNode(_assertThisInitialized(_this));
if (!ul) {
return;
}
var ulChildrenNodes = ul.children;
if (!ulChildrenNodes || ulChildrenNodes.length === 0) {
return;
}
var lastOverflowedIndicatorPlaceholder = ul.children[ulChildrenNodes.length - 1]; // need last overflowed indicator for calculating length;
setStyle(lastOverflowedIndicatorPlaceholder, 'display', 'inline-block');
var menuItemNodes = _this.getMenuItemNodes(); // reset display attribute for all hidden elements caused by overflow to calculate updated width
// and then reset to original state after width calculation
var overflowedItems = menuItemNodes.filter(function (c) {
return c.className.split(' ').indexOf(MENUITEM_OVERFLOWED_CLASSNAME) >= 0;
});
overflowedItems.forEach(function (c) {
setStyle(c, 'display', 'inline-block');
});
_this.menuItemSizes = menuItemNodes.map(function (c) {
return getWidth(c, true);
});
overflowedItems.forEach(function (c) {
setStyle(c, 'display', 'none');
});
_this.overflowedIndicatorWidth = getWidth(ul.children[ul.children.length - 1], true);
_this.originalTotalWidth = _this.menuItemSizes.reduce(function (acc, cur) {
return acc + cur;
}, 0);
_this.handleResize(); // prevent the overflowed indicator from taking space;
setStyle(lastOverflowedIndicatorPlaceholder, 'display', 'none');
};
_this.handleResize = function () {
if (_this.props.mode !== 'horizontal') {
return;
}
var ul = ReactDOM.findDOMNode(_assertThisInitialized(_this));
if (!ul) {
return;
}
var width = getWidth(ul);
_this.overflowedItems = [];
var currentSumWidth = 0; // index for last visible child in horizontal mode
var lastVisibleIndex; // float number comparison could be problematic
// e.g. 0.1 + 0.2 > 0.3 =====> true
// thus using FLOAT_PRECISION_ADJUST as buffer to help the situation
if (_this.originalTotalWidth > width + FLOAT_PRECISION_ADJUST) {
lastVisibleIndex = -1;
_this.menuItemSizes.forEach(function (liWidth) {
currentSumWidth += liWidth;
if (currentSumWidth + _this.overflowedIndicatorWidth <= width) {
lastVisibleIndex += 1;
}
});
}
_this.setState({
lastVisibleIndex: lastVisibleIndex
});
};
return _this;
}
_createClass(DOMWrap, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
this.setChildrenWidthAndResize();
if (this.props.level === 1 && this.props.mode === 'horizontal') {
var menuUl = ReactDOM.findDOMNode(this);
if (!menuUl) {
return;
}
this.resizeObserver = new ResizeObserver(function (entries) {
entries.forEach(function () {
var cancelFrameId = _this2.cancelFrameId;
cancelAnimationFrame(cancelFrameId);
_this2.cancelFrameId = requestAnimationFrame(_this2.setChildrenWidthAndResize);
});
});
[].slice.call(menuUl.children).concat(menuUl).forEach(function (el) {
_this2.resizeObserver.observe(el);
});
if (typeof MutationObserver !== 'undefined') {
this.mutationObserver = new MutationObserver(function () {
_this2.resizeObserver.disconnect();
[].slice.call(menuUl.children).concat(menuUl).forEach(function (el) {
_this2.resizeObserver.observe(el);
});
_this2.setChildrenWidthAndResize();
});
this.mutationObserver.observe(menuUl, {
attributes: false,
childList: true,
subTree: false
});
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
}
if (this.mutationObserver) {
this.mutationObserver.disconnect();
}
cancelAnimationFrame(this.cancelFrameId);
}
}, {
key: "renderChildren",
value: function renderChildren(children) {
var _this3 = this;
// need to take care of overflowed items in horizontal mode
var lastVisibleIndex = this.state.lastVisibleIndex;
return (children || []).reduce(function (acc, childNode, index) {
var item = childNode;
if (_this3.props.mode === 'horizontal') {
var overflowed = _this3.getOverflowedSubMenuItem(childNode.props.eventKey, []);
if (lastVisibleIndex !== undefined && _this3.props.className.indexOf("".concat(_this3.props.prefixCls, "-root")) !== -1) {
if (index > lastVisibleIndex) {
item = React.cloneElement(childNode, // 这里修改 eventKey 是为了防止隐藏状态下还会触发 openkeys 事件
{
style: {
display: 'none'
},
eventKey: "".concat(childNode.props.eventKey, "-hidden"),
/**
* Legacy code. Here `className` never used:
* https://github.com/react-component/menu/commit/4cd6b49fce9d116726f4ea00dda85325d6f26500#diff-e2fa48f75c2dd2318295cde428556a76R240
*/
className: "".concat(MENUITEM_OVERFLOWED_CLASSNAME)
});
}
if (index === lastVisibleIndex + 1) {
_this3.overflowedItems = children.slice(lastVisibleIndex + 1).map(function (c) {
return React.cloneElement(c, // children[index].key will become '.$key' in clone by default,
// we have to overwrite with the correct key explicitly
{
key: c.props.eventKey,
mode: 'vertical-left'
});
});
overflowed = _this3.getOverflowedSubMenuItem(childNode.props.eventKey, _this3.overflowedItems);
}
}
var ret = [].concat(_toConsumableArray(acc), [overflowed, item]);
if (index === children.length - 1) {
// need a placeholder for calculating overflowed indicator width
ret.push(_this3.getOverflowedSubMenuItem(childNode.props.eventKey, [], true));
}
return ret;
}
return [].concat(_toConsumableArray(acc), [item]);
}, []);
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
visible = _this$props2.visible,
prefixCls = _this$props2.prefixCls,
overflowedIndicator = _this$props2.overflowedIndicator,
mode = _this$props2.mode,
level = _this$props2.level,
tag = _this$props2.tag,
children = _this$props2.children,
theme = _this$props2.theme,
rest = _objectWithoutProperties(_this$props2, ["visible", "prefixCls", "overflowedIndicator", "mode", "level", "tag", "children", "theme"]);
var Tag = tag;
return React.createElement(Tag, Object.assign({}, rest), this.renderChildren(children));
}
}]);
return DOMWrap;
}(React.Component);
DOMWrap.defaultProps = {
tag: 'div',
className: ''
};
export default DOMWrap;