UNPKG

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