util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import _typeof from 'babel-runtime/helpers/typeof';
  2. import isMobile from './utils/isMobile';
  3. export function noop() {}
  4. export function getKeyFromChildrenIndex(child, menuEventKey, index) {
  5. var prefix = menuEventKey || '';
  6. return child.key === undefined ? prefix + 'item_' + index : child.key;
  7. }
  8. export function getMenuIdFromSubMenuEventKey(eventKey) {
  9. return eventKey + '-menu-';
  10. }
  11. export function loopMenuItem(children, cb) {
  12. var index = -1;
  13. children.forEach(function (c) {
  14. index++;
  15. if (c && c.type && c.type.isMenuItemGroup) {
  16. c.$slots['default'].forEach(function (c2) {
  17. index++;
  18. c.componentOptions && cb(c2, index);
  19. });
  20. } else {
  21. c.componentOptions && cb(c, index);
  22. }
  23. });
  24. }
  25. export function loopMenuItemRecursively(children, keys, ret) {
  26. if (!children || ret.find) {
  27. return;
  28. }
  29. children.forEach(function (c) {
  30. if (ret.find) {
  31. return;
  32. }
  33. if (c.data && c.data.slot && c.data.slot !== 'default') {
  34. return;
  35. }
  36. if (c && c.componentOptions) {
  37. var options = c.componentOptions.Ctor.options;
  38. if (!options || !(options.isSubMenu || options.isMenuItem || options.isMenuItemGroup)) {
  39. return;
  40. }
  41. if (keys.indexOf(c.key) !== -1) {
  42. ret.find = true;
  43. } else if (c.componentOptions.children) {
  44. loopMenuItemRecursively(c.componentOptions.children, keys, ret);
  45. }
  46. }
  47. });
  48. }
  49. export var menuAllProps = {
  50. props: ['defaultSelectedKeys', 'selectedKeys', 'defaultOpenKeys', 'openKeys', 'mode', 'getPopupContainer', 'openTransitionName', 'openAnimation', 'subMenuOpenDelay', 'subMenuCloseDelay', 'forceSubMenuRender', 'triggerSubMenuAction', 'level', 'selectable', 'multiple', 'visible', 'focusable', 'defaultActiveFirst', 'prefixCls', 'inlineIndent', 'parentMenu', 'title', 'rootPrefixCls', 'eventKey', 'active', 'popupAlign', 'popupOffset', 'isOpen', 'renderMenuItem', 'manualRef', 'subMenuKey', 'disabled', 'index', 'isSelected', 'store', 'activeKey', 'builtinPlacements', 'overflowedIndicator',
  51. // the following keys found need to be removed from test regression
  52. 'attribute', 'value', 'popupClassName', 'inlineCollapsed', 'menu', 'theme', 'itemIcon', 'expandIcon'],
  53. on: ['select', 'deselect', 'destroy', 'openChange', 'itemHover', 'titleMouseenter', 'titleMouseleave', 'titleClick']
  54. };
  55. // ref: https://github.com/ant-design/ant-design/issues/14007
  56. // ref: https://bugs.chromium.org/p/chromium/issues/detail?id=360889
  57. // getBoundingClientRect return the full precision value, which is
  58. // not the same behavior as on chrome. Set the precision to 6 to
  59. // unify their behavior
  60. export var getWidth = function getWidth(elem) {
  61. var width = elem && typeof elem.getBoundingClientRect === 'function' && elem.getBoundingClientRect().width;
  62. if (width) {
  63. width = +width.toFixed(6);
  64. }
  65. return width || 0;
  66. };
  67. export var setStyle = function setStyle(elem, styleProperty, value) {
  68. if (elem && _typeof(elem.style) === 'object') {
  69. elem.style[styleProperty] = value;
  70. }
  71. };
  72. export var isMobileDevice = function isMobileDevice() {
  73. return isMobile.any;
  74. };