Menu.js
12 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
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 { Provider, create } from 'mini-store';
import omit from 'omit.js';
import SubPopupMenu, { getActiveKey } from './SubPopupMenu';
import { noop } from './util';
import { getMotion } from './utils/legacyUtil';
var Menu = /*#__PURE__*/function (_React$Component) {
_inherits(Menu, _React$Component);
var _super = _createSuper(Menu);
function Menu(props) {
var _this;
_classCallCheck(this, Menu);
_this = _super.call(this, props);
_this.onSelect = function (selectInfo) {
var _assertThisInitialize = _assertThisInitialized(_this),
props = _assertThisInitialize.props;
if (props.selectable) {
// root menu
var _this$store$getState = _this.store.getState(),
_selectedKeys = _this$store$getState.selectedKeys;
var selectedKey = selectInfo.key;
if (props.multiple) {
_selectedKeys = _selectedKeys.concat([selectedKey]);
} else {
_selectedKeys = [selectedKey];
}
if (!('selectedKeys' in props)) {
_this.store.setState({
selectedKeys: _selectedKeys
});
}
props.onSelect(_objectSpread(_objectSpread({}, selectInfo), {}, {
selectedKeys: _selectedKeys
}));
}
};
_this.onClick = function (e) {
var mode = _this.getRealMenuMode();
var _assertThisInitialize2 = _assertThisInitialized(_this),
store = _assertThisInitialize2.store,
onOpenChange = _assertThisInitialize2.props.onOpenChange;
if (mode !== 'inline' && !('openKeys' in _this.props)) {
// closing vertical popup submenu after click it
store.setState({
openKeys: []
});
onOpenChange([]);
}
_this.props.onClick(e);
}; // onKeyDown needs to be exposed as a instance method
// e.g., in rc-select, we need to navigate menu item while
// current active item is rc-select input box rather than the menu itself
_this.onKeyDown = function (e, callback) {
_this.innerMenu.getWrappedInstance().onKeyDown(e, callback);
};
_this.onOpenChange = function (event) {
var _assertThisInitialize3 = _assertThisInitialized(_this),
props = _assertThisInitialize3.props;
var openKeys = _this.store.getState().openKeys.concat();
var changed = false;
var processSingle = function processSingle(e) {
var oneChanged = false;
if (e.open) {
oneChanged = openKeys.indexOf(e.key) === -1;
if (oneChanged) {
openKeys.push(e.key);
}
} else {
var index = openKeys.indexOf(e.key);
oneChanged = index !== -1;
if (oneChanged) {
openKeys.splice(index, 1);
}
}
changed = changed || oneChanged;
};
if (Array.isArray(event)) {
// batch change call
event.forEach(processSingle);
} else {
processSingle(event);
}
if (changed) {
if (!('openKeys' in _this.props)) {
_this.store.setState({
openKeys: openKeys
});
}
props.onOpenChange(openKeys);
}
};
_this.onDeselect = function (selectInfo) {
var _assertThisInitialize4 = _assertThisInitialized(_this),
props = _assertThisInitialize4.props;
if (props.selectable) {
var _selectedKeys2 = _this.store.getState().selectedKeys.concat();
var selectedKey = selectInfo.key;
var index = _selectedKeys2.indexOf(selectedKey);
if (index !== -1) {
_selectedKeys2.splice(index, 1);
}
if (!('selectedKeys' in props)) {
_this.store.setState({
selectedKeys: _selectedKeys2
});
}
props.onDeselect(_objectSpread(_objectSpread({}, selectInfo), {}, {
selectedKeys: _selectedKeys2
}));
}
}; // Restore vertical mode when menu is collapsed responsively when mounted
// https://github.com/ant-design/ant-design/issues/13104
// TODO: not a perfect solution,
// looking a new way to avoid setting switchingModeFromInline in this situation
_this.onMouseEnter = function (e) {
_this.restoreModeVerticalFromInline();
var onMouseEnter = _this.props.onMouseEnter;
if (onMouseEnter) {
onMouseEnter(e);
}
};
_this.onTransitionEnd = function (e) {
// when inlineCollapsed menu width animation finished
// https://github.com/ant-design/ant-design/issues/12864
var widthCollapsed = e.propertyName === 'width' && e.target === e.currentTarget; // Fix SVGElement e.target.className.indexOf is not a function
// https://github.com/ant-design/ant-design/issues/15699
var className = e.target.className; // SVGAnimatedString.animVal should be identical to SVGAnimatedString.baseVal,
// unless during an animation.
var classNameValue = Object.prototype.toString.call(className) === '[object SVGAnimatedString]' ? className.animVal : className; // Fix for <Menu style={{ width: '100%' }} />,
// the width transition won't trigger when menu is collapsed
// https://github.com/ant-design/ant-design-pro/issues/2783
var iconScaled = e.propertyName === 'font-size' && classNameValue.indexOf('anticon') >= 0;
if (widthCollapsed || iconScaled) {
_this.restoreModeVerticalFromInline();
}
};
_this.setInnerMenu = function (node) {
_this.innerMenu = node;
};
_this.isRootMenu = true;
var selectedKeys = props.defaultSelectedKeys;
var openKeys = props.defaultOpenKeys;
if ('selectedKeys' in props) {
selectedKeys = props.selectedKeys || [];
}
if ('openKeys' in props) {
openKeys = props.openKeys || [];
}
_this.store = create({
selectedKeys: selectedKeys,
openKeys: openKeys,
activeKey: {
'0-menu-': getActiveKey(props, props.activeKey)
}
});
_this.state = {
switchingModeFromInline: false,
prevProps: props,
inlineOpenKeys: [],
store: _this.store
};
return _this;
}
_createClass(Menu, [{
key: "componentDidMount",
value: function componentDidMount() {
this.updateMiniStore();
this.updateMenuDisplay();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props = this.props,
siderCollapsed = _this$props.siderCollapsed,
inlineCollapsed = _this$props.inlineCollapsed,
onOpenChange = _this$props.onOpenChange;
if (!prevProps.inlineCollapsed && inlineCollapsed || !prevProps.siderCollapsed && siderCollapsed) {
onOpenChange([]);
}
this.updateMiniStore();
this.updateMenuDisplay();
}
}, {
key: "updateMenuDisplay",
value: function updateMenuDisplay() {
var collapsedWidth = this.props.collapsedWidth,
store = this.store,
prevOpenKeys = this.prevOpenKeys; // https://github.com/ant-design/ant-design/issues/8587
var hideMenu = this.getInlineCollapsed() && (collapsedWidth === 0 || collapsedWidth === '0' || collapsedWidth === '0px');
if (hideMenu) {
this.prevOpenKeys = store.getState().openKeys.concat();
this.store.setState({
openKeys: []
});
} else if (prevOpenKeys) {
this.store.setState({
openKeys: prevOpenKeys
});
this.prevOpenKeys = null;
}
}
}, {
key: "getRealMenuMode",
value: function getRealMenuMode() {
var mode = this.props.mode;
var switchingModeFromInline = this.state.switchingModeFromInline;
var inlineCollapsed = this.getInlineCollapsed();
if (switchingModeFromInline && inlineCollapsed) {
return 'inline';
}
return inlineCollapsed ? 'vertical' : mode;
}
}, {
key: "getInlineCollapsed",
value: function getInlineCollapsed() {
var _this$props2 = this.props,
inlineCollapsed = _this$props2.inlineCollapsed,
siderCollapsed = _this$props2.siderCollapsed;
if (siderCollapsed !== undefined) {
return siderCollapsed;
}
return inlineCollapsed;
}
}, {
key: "restoreModeVerticalFromInline",
value: function restoreModeVerticalFromInline() {
var switchingModeFromInline = this.state.switchingModeFromInline;
if (switchingModeFromInline) {
this.setState({
switchingModeFromInline: false
});
}
}
}, {
key: "updateMiniStore",
value: function updateMiniStore() {
if ('selectedKeys' in this.props) {
this.store.setState({
selectedKeys: this.props.selectedKeys || []
});
}
if ('openKeys' in this.props) {
this.store.setState({
openKeys: this.props.openKeys || []
});
}
}
}, {
key: "render",
value: function render() {
var props = _objectSpread({}, omit(this.props, ['collapsedWidth', 'siderCollapsed', 'defaultMotions']));
var mode = this.getRealMenuMode();
props.className += " ".concat(props.prefixCls, "-root");
if (props.direction === 'rtl') {
props.className += " ".concat(props.prefixCls, "-rtl");
}
props = _objectSpread(_objectSpread({}, props), {}, {
mode: mode,
onClick: this.onClick,
onOpenChange: this.onOpenChange,
onDeselect: this.onDeselect,
onSelect: this.onSelect,
onMouseEnter: this.onMouseEnter,
onTransitionEnd: this.onTransitionEnd,
parentMenu: this,
motion: getMotion(this.props, this.state, mode)
});
delete props.openAnimation;
delete props.openTransitionName;
return React.createElement(Provider, {
store: this.store
}, React.createElement(SubPopupMenu, Object.assign({}, props, {
ref: this.setInnerMenu
}), this.props.children));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
var prevProps = prevState.prevProps,
store = prevState.store;
var prevStoreState = store.getState();
var newStoreState = {};
var newState = {
prevProps: nextProps
};
if (prevProps.mode === 'inline' && nextProps.mode !== 'inline') {
newState.switchingModeFromInline = true;
}
if ('openKeys' in nextProps) {
newStoreState.openKeys = nextProps.openKeys;
} else {
// [Legacy] Old code will return after `openKeys` changed.
// Not sure the reason, we should keep this logic still.
if (nextProps.inlineCollapsed && !prevProps.inlineCollapsed || nextProps.siderCollapsed && !prevProps.siderCollapsed) {
newState.switchingModeFromInline = true;
newState.inlineOpenKeys = prevStoreState.openKeys;
newStoreState.openKeys = [];
}
if (!nextProps.inlineCollapsed && prevProps.inlineCollapsed || !nextProps.siderCollapsed && prevProps.siderCollapsed) {
newStoreState.openKeys = prevState.inlineOpenKeys;
newState.inlineOpenKeys = [];
}
}
if (Object.keys(newStoreState).length) {
store.setState(newStoreState);
}
return newState;
}
}]);
return Menu;
}(React.Component);
Menu.defaultProps = {
selectable: true,
onClick: noop,
onSelect: noop,
onOpenChange: noop,
onDeselect: noop,
defaultSelectedKeys: [],
defaultOpenKeys: [],
subMenuOpenDelay: 0.1,
subMenuCloseDelay: 0.1,
triggerSubMenuAction: 'hover',
prefixCls: 'rc-menu',
className: '',
mode: 'vertical',
style: {},
builtinPlacements: {},
overflowedIndicator: React.createElement("span", null, "\xB7\xB7\xB7")
};
export default Menu;