util.js
4.16 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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.noop = noop;
exports.getKeyFromChildrenIndex = getKeyFromChildrenIndex;
exports.getMenuIdFromSubMenuEventKey = getMenuIdFromSubMenuEventKey;
exports.loopMenuItem = loopMenuItem;
exports.loopMenuItemRecursively = loopMenuItemRecursively;
exports.isMobileDevice = exports.setStyle = exports.getWidth = exports.menuAllProps = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var React = _interopRequireWildcard(require("react"));
var _isMobile = _interopRequireDefault(require("./utils/isMobile"));
function noop() {}
function getKeyFromChildrenIndex(child, menuEventKey, index) {
var prefix = menuEventKey || '';
return child.key || "".concat(prefix, "item_").concat(index);
}
function getMenuIdFromSubMenuEventKey(eventKey) {
return "".concat(eventKey, "-menu-");
}
function loopMenuItem(children, cb) {
var index = -1;
React.Children.forEach(children, function (c) {
index += 1;
if (c && c.type && c.type.isMenuItemGroup) {
React.Children.forEach(c.props.children, function (c2) {
index += 1;
cb(c2, index);
});
} else {
cb(c, index);
}
});
}
function loopMenuItemRecursively(children, keys, ret) {
/* istanbul ignore if */
if (!children || ret.find) {
return;
}
React.Children.forEach(children, function (c) {
if (c) {
var construct = c.type;
if (!construct || !(construct.isSubMenu || construct.isMenuItem || construct.isMenuItemGroup)) {
return;
}
if (keys.indexOf(c.key) !== -1) {
// eslint-disable-next-line no-param-reassign
ret.find = true;
} else if (c.props.children) {
loopMenuItemRecursively(c.props.children, keys, ret);
}
}
});
}
var menuAllProps = ['defaultSelectedKeys', 'selectedKeys', 'defaultOpenKeys', 'openKeys', 'mode', 'getPopupContainer', 'onSelect', 'onDeselect', 'onDestroy', 'openTransitionName', 'openAnimation', 'subMenuOpenDelay', 'subMenuCloseDelay', 'forceSubMenuRender', 'triggerSubMenuAction', 'level', 'selectable', 'multiple', 'onOpenChange', 'visible', 'focusable', 'defaultActiveFirst', 'prefixCls', 'inlineIndent', 'parentMenu', 'title', 'rootPrefixCls', 'eventKey', 'active', 'onItemHover', 'onTitleMouseEnter', 'onTitleMouseLeave', 'onTitleClick', 'popupAlign', 'popupOffset', 'isOpen', 'renderMenuItem', 'manualRef', 'subMenuKey', 'disabled', 'index', 'isSelected', 'store', 'activeKey', 'builtinPlacements', 'overflowedIndicator', 'motion', // the following keys found need to be removed from test regression
'attribute', 'value', 'popupClassName', 'inlineCollapsed', 'menu', 'theme', 'itemIcon', 'expandIcon']; // ref: https://github.com/ant-design/ant-design/issues/14007
// ref: https://bugs.chromium.org/p/chromium/issues/detail?id=360889
// getBoundingClientRect return the full precision value, which is
// not the same behavior as on chrome. Set the precision to 6 to
// unify their behavior
exports.menuAllProps = menuAllProps;
var getWidth = function getWidth(elem) {
var includeMargin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var width = elem && typeof elem.getBoundingClientRect === 'function' && elem.getBoundingClientRect().width;
if (width) {
if (includeMargin) {
var _getComputedStyle = getComputedStyle(elem),
marginLeft = _getComputedStyle.marginLeft,
marginRight = _getComputedStyle.marginRight;
width += +marginLeft.replace('px', '') + +marginRight.replace('px', '');
}
width = +width.toFixed(6);
}
return width || 0;
};
exports.getWidth = getWidth;
var setStyle = function setStyle(elem, styleProperty, value) {
if (elem && (0, _typeof2.default)(elem.style) === 'object') {
// eslint-disable-next-line no-param-reassign
elem.style[styleProperty] = value;
}
};
exports.setStyle = setStyle;
var isMobileDevice = function isMobileDevice() {
return _isMobile.default.any;
};
exports.isMobileDevice = isMobileDevice;