Menu.js
12.8 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var React = _interopRequireWildcard(require("react"));
var _miniStore = require("mini-store");
var _omit = _interopRequireDefault(require("omit.js"));
var _SubPopupMenu = _interopRequireWildcard(require("./SubPopupMenu"));
var _util = require("./util");
var _legacyUtil = require("./utils/legacyUtil");
var Menu = /*#__PURE__*/function (_React$Component) {
(0, _inherits2.default)(Menu, _React$Component);
var _super = (0, _createSuper2.default)(Menu);
function Menu(props) {
var _this;
(0, _classCallCheck2.default)(this, Menu);
_this = _super.call(this, props);
_this.onSelect = function (selectInfo) {
var _assertThisInitialize = (0, _assertThisInitialized2.default)(_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((0, _objectSpread2.default)((0, _objectSpread2.default)({}, selectInfo), {}, {
selectedKeys: _selectedKeys
}));
}
};
_this.onClick = function (e) {
var mode = _this.getRealMenuMode();
var _assertThisInitialize2 = (0, _assertThisInitialized2.default)(_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 = (0, _assertThisInitialized2.default)(_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 = (0, _assertThisInitialized2.default)(_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((0, _objectSpread2.default)((0, _objectSpread2.default)({}, 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 = (0, _miniStore.create)({
selectedKeys: selectedKeys,
openKeys: openKeys,
activeKey: {
'0-menu-': (0, _SubPopupMenu.getActiveKey)(props, props.activeKey)
}
});
_this.state = {
switchingModeFromInline: false,
prevProps: props,
inlineOpenKeys: [],
store: _this.store
};
return _this;
}
(0, _createClass2.default)(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 = (0, _objectSpread2.default)({}, (0, _omit.default)(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 = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
mode: mode,
onClick: this.onClick,
onOpenChange: this.onOpenChange,
onDeselect: this.onDeselect,
onSelect: this.onSelect,
onMouseEnter: this.onMouseEnter,
onTransitionEnd: this.onTransitionEnd,
parentMenu: this,
motion: (0, _legacyUtil.getMotion)(this.props, this.state, mode)
});
delete props.openAnimation;
delete props.openTransitionName;
return React.createElement(_miniStore.Provider, {
store: this.store
}, React.createElement(_SubPopupMenu.default, 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: _util.noop,
onSelect: _util.noop,
onOpenChange: _util.noop,
onDeselect: _util.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")
};
var _default = Menu;
exports.default = _default;