1 | import React from 'react';
|
2 |
|
3 | export function noop() {
|
4 | }
|
5 |
|
6 | export function getKeyFromChildrenIndex(child, menuEventKey, index) {
|
7 | const prefix = menuEventKey || '';
|
8 | return child.key || `${prefix}item_${index}`;
|
9 | }
|
10 |
|
11 | export function getMenuIdFromSubMenuEventKey(eventKey) {
|
12 | return `${eventKey}-menu-`;
|
13 | }
|
14 |
|
15 | export function loopMenuItem(children, cb) {
|
16 | let index = -1;
|
17 | React.Children.forEach(children, (c) => {
|
18 | index++;
|
19 | if (c && c.type && c.type.isMenuItemGroup) {
|
20 | React.Children.forEach(c.props.children, (c2) => {
|
21 | index++;
|
22 | cb(c2, index);
|
23 | });
|
24 | } else {
|
25 | cb(c, index);
|
26 | }
|
27 | });
|
28 | }
|
29 |
|
30 | export function loopMenuItemRecursively(children, keys, ret) {
|
31 |
|
32 | if (!children || ret.find) {
|
33 | return;
|
34 | }
|
35 | React.Children.forEach(children, (c) => {
|
36 | if (c) {
|
37 | const construct = c.type;
|
38 | if (!construct
|
39 | ||
|
40 | !(construct.isSubMenu || construct.isMenuItem || construct.isMenuItemGroup)
|
41 | ) {
|
42 | return;
|
43 | }
|
44 | if (keys.indexOf(c.key) !== -1) {
|
45 | ret.find = true;
|
46 | } else if (c.props.children) {
|
47 | loopMenuItemRecursively(c.props.children, keys, ret);
|
48 | }
|
49 | }
|
50 | });
|
51 | }
|
52 |
|
53 | export const menuAllProps = [
|
54 | 'defaultSelectedKeys',
|
55 | 'selectedKeys',
|
56 | 'defaultOpenKeys',
|
57 | 'openKeys',
|
58 | 'mode',
|
59 | 'getPopupContainer',
|
60 | 'onSelect',
|
61 | 'onDeselect',
|
62 | 'onDestroy',
|
63 | 'openTransitionName',
|
64 | 'openAnimation',
|
65 | 'subMenuOpenDelay',
|
66 | 'subMenuCloseDelay',
|
67 | 'forceSubMenuRender',
|
68 | 'triggerSubMenuAction',
|
69 | 'level',
|
70 | 'selectable',
|
71 | 'multiple',
|
72 | 'onOpenChange',
|
73 | 'visible',
|
74 | 'focusable',
|
75 | 'defaultActiveFirst',
|
76 | 'prefixCls',
|
77 | 'inlineIndent',
|
78 | 'parentMenu',
|
79 | 'title',
|
80 | 'rootPrefixCls',
|
81 | 'eventKey',
|
82 | 'active',
|
83 | 'onItemHover',
|
84 | 'onTitleMouseEnter',
|
85 | 'onTitleMouseLeave',
|
86 | 'onTitleClick',
|
87 | 'popupAlign',
|
88 | 'popupOffset',
|
89 | 'isOpen',
|
90 | 'renderMenuItem',
|
91 | 'manualRef',
|
92 | 'subMenuKey',
|
93 | 'disabled',
|
94 | 'index',
|
95 | 'isSelected',
|
96 | 'store',
|
97 | 'activeKey',
|
98 | 'builtinPlacements',
|
99 | 'overflowedIndicator',
|
100 |
|
101 |
|
102 | 'attribute',
|
103 | 'value',
|
104 | 'popupClassName',
|
105 | 'inlineCollapsed',
|
106 | 'menu',
|
107 | 'theme',
|
108 | 'itemIcon',
|
109 | 'expandIcon',
|
110 | ];
|
111 |
|
112 | export const getWidth = (elem) => (
|
113 | elem &&
|
114 | typeof elem.getBoundingClientRect === 'function' &&
|
115 | elem.getBoundingClientRect().width
|
116 | ) || 0;
|
117 |
|
118 | export const setStyle = (elem, styleProperty, value) => {
|
119 | if (elem && typeof elem.style === 'object') {
|
120 | elem.style[styleProperty] = value;
|
121 | }
|
122 | };
|
123 |
|
124 |
|
125 | export function fireKeyEvent(el, evtType, keyCode) {
|
126 | var evtObj;
|
127 | if (document.createEvent) {
|
128 | if (window.KeyEvent) {
|
129 | evtObj = document.createEvent('KeyEvents');
|
130 | evtObj.initKeyEvent(evtType, true, true, window, true, false, false, false, keyCode, 0);
|
131 | } else {
|
132 | evtObj = document.createEvent('UIEvents');
|
133 | evtObj.initUIEvent(evtType, true, true, window, 1);
|
134 |
|
135 | delete evtObj.keyCode;
|
136 | if (typeof evtObj.keyCode === "undefined") {
|
137 | Object.defineProperty(evtObj, "keyCode", { value: keyCode });
|
138 | } else {
|
139 | evtObj.key = String.fromCharCode(keyCode);
|
140 | }
|
141 |
|
142 | if (typeof evtObj.ctrlKey === 'undefined') {
|
143 | Object.defineProperty(evtObj, "ctrlKey", { value: true });
|
144 | } else {
|
145 | evtObj.ctrlKey = true;
|
146 | }
|
147 | }
|
148 | el.dispatchEvent(evtObj);
|
149 |
|
150 | } else if (document.createEventObject) {
|
151 | evtObj = document.createEventObject();
|
152 | evtObj.keyCode = keyCode
|
153 | el.fireEvent('on' + evtType, evtObj);
|
154 | }
|
155 | } |
\ | No newline at end of file |