SubMenu.js
21.9 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.SubMenu = void 0;
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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var React = _interopRequireWildcard(require("react"));
var ReactDOM = _interopRequireWildcard(require("react-dom"));
var _rcTrigger = _interopRequireDefault(require("rc-trigger"));
var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var _rcMotion = _interopRequireDefault(require("rc-motion"));
var _classnames = _interopRequireDefault(require("classnames"));
var _miniStore = require("mini-store");
var _SubPopupMenu = _interopRequireDefault(require("./SubPopupMenu"));
var _placements = require("./placements");
var _util = require("./util");
var guid = 0;
var popupPlacementMap = {
horizontal: 'bottomLeft',
vertical: 'rightTop',
'vertical-left': 'rightTop',
'vertical-right': 'leftTop'
};
var updateDefaultActiveFirst = function updateDefaultActiveFirst(store, eventKey, defaultActiveFirst) {
var menuId = (0, _util.getMenuIdFromSubMenuEventKey)(eventKey);
var state = store.getState();
store.setState({
defaultActiveFirst: (0, _objectSpread3.default)((0, _objectSpread3.default)({}, state.defaultActiveFirst), {}, (0, _defineProperty2.default)({}, menuId, defaultActiveFirst))
});
};
var SubMenu = /*#__PURE__*/function (_React$Component) {
(0, _inherits2.default)(SubMenu, _React$Component);
var _super = (0, _createSuper2.default)(SubMenu);
function SubMenu(props) {
var _this;
(0, _classCallCheck2.default)(this, SubMenu);
_this = _super.call(this, props);
_this.onDestroy = function (key) {
_this.props.onDestroy(key);
};
/**
* note:
* This legacy code that `onKeyDown` is called by parent instead of dom self.
* which need return code to check if this event is handled
*/
_this.onKeyDown = function (e) {
var keyCode = e.keyCode;
var menu = _this.menuInstance;
var store = _this.props.store;
var visible = _this.getVisible();
if (keyCode === _KeyCode.default.ENTER) {
_this.onTitleClick(e);
updateDefaultActiveFirst(store, _this.props.eventKey, true);
return true;
}
if (keyCode === _KeyCode.default.RIGHT) {
if (visible) {
menu.onKeyDown(e);
} else {
_this.triggerOpenChange(true); // need to update current menu's defaultActiveFirst value
updateDefaultActiveFirst(store, _this.props.eventKey, true);
}
return true;
}
if (keyCode === _KeyCode.default.LEFT) {
var handled;
if (visible) {
handled = menu.onKeyDown(e);
} else {
return undefined;
}
if (!handled) {
_this.triggerOpenChange(false);
handled = true;
}
return handled;
}
if (visible && (keyCode === _KeyCode.default.UP || keyCode === _KeyCode.default.DOWN)) {
return menu.onKeyDown(e);
}
return undefined;
};
_this.onOpenChange = function (e) {
_this.props.onOpenChange(e);
};
_this.onPopupVisibleChange = function (visible) {
_this.triggerOpenChange(visible, visible ? 'mouseenter' : 'mouseleave');
};
_this.onMouseEnter = function (e) {
var _this$props = _this.props,
key = _this$props.eventKey,
onMouseEnter = _this$props.onMouseEnter,
store = _this$props.store;
updateDefaultActiveFirst(store, _this.props.eventKey, false);
onMouseEnter({
key: key,
domEvent: e
});
};
_this.onMouseLeave = function (e) {
var _this$props2 = _this.props,
parentMenu = _this$props2.parentMenu,
eventKey = _this$props2.eventKey,
onMouseLeave = _this$props2.onMouseLeave;
parentMenu.subMenuInstance = (0, _assertThisInitialized2.default)(_this);
onMouseLeave({
key: eventKey,
domEvent: e
});
};
_this.onTitleMouseEnter = function (domEvent) {
var _this$props3 = _this.props,
key = _this$props3.eventKey,
onItemHover = _this$props3.onItemHover,
onTitleMouseEnter = _this$props3.onTitleMouseEnter;
onItemHover({
key: key,
hover: true
});
onTitleMouseEnter({
key: key,
domEvent: domEvent
});
};
_this.onTitleMouseLeave = function (e) {
var _this$props4 = _this.props,
parentMenu = _this$props4.parentMenu,
eventKey = _this$props4.eventKey,
onItemHover = _this$props4.onItemHover,
onTitleMouseLeave = _this$props4.onTitleMouseLeave;
parentMenu.subMenuInstance = (0, _assertThisInitialized2.default)(_this);
onItemHover({
key: eventKey,
hover: false
});
onTitleMouseLeave({
key: eventKey,
domEvent: e
});
};
_this.onTitleClick = function (e) {
var _assertThisInitialize = (0, _assertThisInitialized2.default)(_this),
props = _assertThisInitialize.props;
props.onTitleClick({
key: props.eventKey,
domEvent: e
});
if (props.triggerSubMenuAction === 'hover') {
return;
}
_this.triggerOpenChange(!_this.getVisible(), 'click');
updateDefaultActiveFirst(props.store, _this.props.eventKey, false);
};
_this.onSubMenuClick = function (info) {
// in the case of overflowed submenu
// onClick is not copied over
if (typeof _this.props.onClick === 'function') {
_this.props.onClick(_this.addKeyPath(info));
}
};
_this.onSelect = function (info) {
_this.props.onSelect(info);
};
_this.onDeselect = function (info) {
_this.props.onDeselect(info);
};
_this.getPrefixCls = function () {
return "".concat(_this.props.rootPrefixCls, "-submenu");
};
_this.getActiveClassName = function () {
return "".concat(_this.getPrefixCls(), "-active");
};
_this.getDisabledClassName = function () {
return "".concat(_this.getPrefixCls(), "-disabled");
};
_this.getSelectedClassName = function () {
return "".concat(_this.getPrefixCls(), "-selected");
};
_this.getOpenClassName = function () {
return "".concat(_this.props.rootPrefixCls, "-submenu-open");
};
_this.getVisible = function () {
return _this.state.isOpen;
};
_this.getMode = function () {
return _this.state.mode;
};
_this.saveMenuInstance = function (c) {
// children menu instance
_this.menuInstance = c;
};
_this.addKeyPath = function (info) {
return (0, _objectSpread3.default)((0, _objectSpread3.default)({}, info), {}, {
keyPath: (info.keyPath || []).concat(_this.props.eventKey)
});
};
_this.triggerOpenChange = function (open, type) {
var key = _this.props.eventKey;
var openChange = function openChange() {
_this.onOpenChange({
key: key,
item: (0, _assertThisInitialized2.default)(_this),
trigger: type,
open: open
});
};
if (type === 'mouseenter') {
// make sure mouseenter happen after other menu item's mouseleave
_this.mouseenterTimeout = setTimeout(function () {
openChange();
}, 0);
} else {
openChange();
}
};
_this.isChildrenSelected = function () {
var ret = {
find: false
};
(0, _util.loopMenuItemRecursively)(_this.props.children, _this.props.selectedKeys, ret);
return ret.find;
};
_this.isInlineMode = function () {
return _this.getMode() === 'inline';
};
_this.adjustWidth = function () {
/* istanbul ignore if */
if (!_this.subMenuTitle || !_this.menuInstance) {
return;
}
var popupMenu = ReactDOM.findDOMNode(_this.menuInstance);
if (popupMenu.offsetWidth >= _this.subMenuTitle.offsetWidth) {
return;
}
/* istanbul ignore next */
popupMenu.style.minWidth = "".concat(_this.subMenuTitle.offsetWidth, "px");
};
_this.saveSubMenuTitle = function (subMenuTitle) {
_this.subMenuTitle = subMenuTitle;
};
_this.getBaseProps = function () {
var _assertThisInitialize2 = (0, _assertThisInitialized2.default)(_this),
props = _assertThisInitialize2.props;
var mergedMode = _this.getMode();
return {
mode: mergedMode === 'horizontal' ? 'vertical' : mergedMode,
visible: _this.getVisible(),
level: props.level + 1,
inlineIndent: props.inlineIndent,
focusable: false,
onClick: _this.onSubMenuClick,
onSelect: _this.onSelect,
onDeselect: _this.onDeselect,
onDestroy: _this.onDestroy,
selectedKeys: props.selectedKeys,
eventKey: "".concat(props.eventKey, "-menu-"),
openKeys: props.openKeys,
motion: props.motion,
onOpenChange: _this.onOpenChange,
subMenuOpenDelay: props.subMenuOpenDelay,
parentMenu: (0, _assertThisInitialized2.default)(_this),
subMenuCloseDelay: props.subMenuCloseDelay,
forceSubMenuRender: props.forceSubMenuRender,
triggerSubMenuAction: props.triggerSubMenuAction,
builtinPlacements: props.builtinPlacements,
defaultActiveFirst: props.store.getState().defaultActiveFirst[(0, _util.getMenuIdFromSubMenuEventKey)(props.eventKey)],
multiple: props.multiple,
prefixCls: props.rootPrefixCls,
id: _this.internalMenuId,
manualRef: _this.saveMenuInstance,
itemIcon: props.itemIcon,
expandIcon: props.expandIcon,
direction: props.direction
};
};
_this.getMotion = function (mode, visible) {
var _assertThisInitialize3 = (0, _assertThisInitialized2.default)(_this),
haveRendered = _assertThisInitialize3.haveRendered;
var _this$props5 = _this.props,
motion = _this$props5.motion,
rootPrefixCls = _this$props5.rootPrefixCls; // don't show transition on first rendering (no animation for opened menu)
// show appear transition if it's not visible (not sure why)
// show appear transition if it's not inline mode
var mergedMotion = (0, _objectSpread3.default)((0, _objectSpread3.default)({}, motion), {}, {
leavedClassName: "".concat(rootPrefixCls, "-hidden"),
removeOnLeave: false,
motionAppear: haveRendered || !visible || mode !== 'inline'
});
return mergedMotion;
};
var store = props.store,
eventKey = props.eventKey;
var _store$getState = store.getState(),
defaultActiveFirst = _store$getState.defaultActiveFirst;
_this.isRootMenu = false;
var value = false;
if (defaultActiveFirst) {
value = defaultActiveFirst[eventKey];
}
updateDefaultActiveFirst(store, eventKey, value);
_this.state = {
mode: props.mode,
isOpen: props.isOpen
};
return _this;
}
(0, _createClass2.default)(SubMenu, [{
key: "componentDidMount",
value: function componentDidMount() {
this.componentDidUpdate();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
var _this2 = this;
var _this$props6 = this.props,
mode = _this$props6.mode,
parentMenu = _this$props6.parentMenu,
manualRef = _this$props6.manualRef,
isOpen = _this$props6.isOpen;
var updateState = function updateState() {
_this2.setState({
mode: mode,
isOpen: isOpen
});
}; // Delay sync when mode changed in case openKeys change not sync
var isOpenChanged = isOpen !== this.state.isOpen;
var isModeChanged = mode !== this.state.mode;
if (isModeChanged || isOpenChanged) {
_raf.default.cancel(this.updateStateRaf);
if (isModeChanged) {
this.updateStateRaf = (0, _raf.default)(updateState);
} else {
updateState();
}
} // invoke customized ref to expose component to mixin
if (manualRef) {
manualRef(this);
}
if (mode !== 'horizontal' || !(parentMenu === null || parentMenu === void 0 ? void 0 : parentMenu.isRootMenu) || !isOpen) {
return;
}
this.minWidthTimeout = setTimeout(function () {
return _this2.adjustWidth();
}, 0);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this$props7 = this.props,
onDestroy = _this$props7.onDestroy,
eventKey = _this$props7.eventKey;
if (onDestroy) {
onDestroy(eventKey);
}
/* istanbul ignore if */
if (this.minWidthTimeout) {
clearTimeout(this.minWidthTimeout);
}
/* istanbul ignore if */
if (this.mouseenterTimeout) {
clearTimeout(this.mouseenterTimeout);
}
_raf.default.cancel(this.updateStateRaf);
}
}, {
key: "renderPopupMenu",
value: function renderPopupMenu(className, style) {
var baseProps = this.getBaseProps();
/**
* zombiej: Why SubPopupMenu here?
* Seems whatever popup or inline mode both will render SubPopupMenu.
* It's controlled by Trigger for popup or not.
*/
return React.createElement(_SubPopupMenu.default, Object.assign({}, baseProps, {
id: this.internalMenuId,
className: className,
style: style
}), this.props.children);
}
}, {
key: "renderChildren",
value: function renderChildren() {
var _this3 = this;
var baseProps = this.getBaseProps();
var mode = baseProps.mode,
visible = baseProps.visible,
forceSubMenuRender = baseProps.forceSubMenuRender,
direction = baseProps.direction; // [Legacy] getMotion must be called before `haveRendered`
var mergedMotion = this.getMotion(mode, visible);
this.haveRendered = true;
this.haveOpened = this.haveOpened || visible || forceSubMenuRender; // never rendered not planning to, don't render
if (!this.haveOpened) {
return React.createElement("div", null);
}
var sharedClassName = (0, _classnames.default)("".concat(baseProps.prefixCls, "-sub"), (0, _defineProperty2.default)({}, "".concat(baseProps.prefixCls, "-rtl"), direction === 'rtl'));
if (!this.isInlineMode()) {
return this.renderPopupMenu(sharedClassName);
}
return React.createElement(_rcMotion.default, Object.assign({
visible: baseProps.visible
}, mergedMotion), function (_ref) {
var className = _ref.className,
style = _ref.style;
var mergedClassName = (0, _classnames.default)(sharedClassName, className);
return _this3.renderPopupMenu(mergedClassName, style);
});
}
}, {
key: "render",
value: function render() {
var _classNames2, _props$parentMenu, _classNames3;
var props = (0, _objectSpread3.default)({}, this.props);
var visible = this.getVisible();
var prefixCls = this.getPrefixCls();
var inline = this.isInlineMode();
var mergedMode = this.getMode();
var className = (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(mergedMode), (_classNames2 = {}, (0, _defineProperty2.default)(_classNames2, props.className, !!props.className), (0, _defineProperty2.default)(_classNames2, this.getOpenClassName(), visible), (0, _defineProperty2.default)(_classNames2, this.getActiveClassName(), props.active || visible && !inline), (0, _defineProperty2.default)(_classNames2, this.getDisabledClassName(), props.disabled), (0, _defineProperty2.default)(_classNames2, this.getSelectedClassName(), this.isChildrenSelected()), _classNames2));
if (!this.internalMenuId) {
if (props.eventKey) {
this.internalMenuId = "".concat(props.eventKey, "$Menu");
} else {
guid += 1;
this.internalMenuId = "$__$".concat(guid, "$Menu");
}
}
var mouseEvents = {};
var titleClickEvents = {};
var titleMouseEvents = {};
if (!props.disabled) {
mouseEvents = {
onMouseLeave: this.onMouseLeave,
onMouseEnter: this.onMouseEnter
}; // only works in title, not outer li
titleClickEvents = {
onClick: this.onTitleClick
};
titleMouseEvents = {
onMouseEnter: this.onTitleMouseEnter,
onMouseLeave: this.onTitleMouseLeave
};
}
var style = {};
var direction = props.direction;
var isRTL = direction === 'rtl';
if (inline) {
if (isRTL) {
style.paddingRight = props.inlineIndent * props.level;
} else {
style.paddingLeft = props.inlineIndent * props.level;
}
}
var ariaOwns = {}; // only set aria-owns when menu is open
// otherwise it would be an invalid aria-owns value
// since corresponding node cannot be found
if (this.getVisible()) {
ariaOwns = {
'aria-owns': this.internalMenuId
};
} // expand custom icon should NOT be displayed in menu with horizontal mode.
var icon = null;
if (mergedMode !== 'horizontal') {
icon = this.props.expandIcon; // ReactNode
if (typeof this.props.expandIcon === 'function') {
icon = React.createElement(this.props.expandIcon, (0, _objectSpread3.default)({}, this.props));
}
}
var title = React.createElement("div", Object.assign({
ref: this.saveSubMenuTitle,
style: style,
className: "".concat(prefixCls, "-title"),
role: "button"
}, titleMouseEvents, titleClickEvents, {
"aria-expanded": visible
}, ariaOwns, {
"aria-haspopup": "true",
title: typeof props.title === 'string' ? props.title : undefined
}), props.title, icon || React.createElement("i", {
className: "".concat(prefixCls, "-arrow")
}));
var children = this.renderChildren();
var getPopupContainer = ((_props$parentMenu = props.parentMenu) === null || _props$parentMenu === void 0 ? void 0 : _props$parentMenu.isRootMenu) ? props.parentMenu.props.getPopupContainer : function (triggerNode) {
return triggerNode.parentNode;
};
var popupPlacement = popupPlacementMap[mergedMode];
var popupAlign = props.popupOffset ? {
offset: props.popupOffset
} : {};
var popupClassName = (0, _classnames.default)((_classNames3 = {}, (0, _defineProperty2.default)(_classNames3, props.popupClassName, props.popupClassName && !inline), (0, _defineProperty2.default)(_classNames3, "".concat(prefixCls, "-rtl"), isRTL), _classNames3));
var disabled = props.disabled,
triggerSubMenuAction = props.triggerSubMenuAction,
subMenuOpenDelay = props.subMenuOpenDelay,
forceSubMenuRender = props.forceSubMenuRender,
subMenuCloseDelay = props.subMenuCloseDelay,
builtinPlacements = props.builtinPlacements;
_util.menuAllProps.forEach(function (key) {
return delete props[key];
}); // Set onClick to null, to ignore propagated onClick event
delete props.onClick;
var placement = isRTL ? Object.assign({}, _placements.placementsRtl, builtinPlacements) : Object.assign({}, _placements.placements, builtinPlacements);
delete props.direction; // [Legacy] It's a fast fix,
// but we should check if we can refactor this to make code more easy to understand
var baseProps = this.getBaseProps();
var mergedMotion = inline ? null : this.getMotion(baseProps.mode, baseProps.visible);
return React.createElement("li", Object.assign({}, props, mouseEvents, {
className: className,
role: "menuitem"
}), React.createElement(_rcTrigger.default, {
prefixCls: prefixCls,
popupClassName: (0, _classnames.default)("".concat(prefixCls, "-popup"), popupClassName),
getPopupContainer: getPopupContainer,
builtinPlacements: placement,
popupPlacement: popupPlacement,
popupVisible: inline ? false : visible,
popupAlign: popupAlign,
popup: inline ? null : children,
action: disabled || inline ? [] : [triggerSubMenuAction],
mouseEnterDelay: subMenuOpenDelay,
mouseLeaveDelay: subMenuCloseDelay,
onPopupVisibleChange: this.onPopupVisibleChange,
forceRender: forceSubMenuRender,
popupMotion: mergedMotion
}, title), inline ? children : null);
}
}]);
return SubMenu;
}(React.Component);
exports.SubMenu = SubMenu;
SubMenu.defaultProps = {
onMouseEnter: _util.noop,
onMouseLeave: _util.noop,
onTitleMouseEnter: _util.noop,
onTitleMouseLeave: _util.noop,
onTitleClick: _util.noop,
manualRef: _util.noop,
mode: 'vertical',
title: ''
};
var connected = (0, _miniStore.connect)(function (_ref2, _ref3) {
var openKeys = _ref2.openKeys,
activeKey = _ref2.activeKey,
selectedKeys = _ref2.selectedKeys;
var eventKey = _ref3.eventKey,
subMenuKey = _ref3.subMenuKey;
return {
isOpen: openKeys.indexOf(eventKey) > -1,
active: activeKey[subMenuKey] === eventKey,
selectedKeys: selectedKeys
};
})(SubMenu);
connected.isSubMenu = true;
var _default = connected;
exports.default = _default;