@hi-ui/menu 4.2.2-alpha.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,19 +1,25 @@
1
1
  # @hi-ui/menu
2
2
 
3
- ## 4.2.2-alpha.0
3
+ ## 4.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2880](https://github.com/XiaoMi/hiui/pull/2880) [`1b46f791f`](https://github.com/XiaoMi/hiui/commit/1b46f791f99c20a946250c2921f3362304e86371) Thanks [@xiamiao1121](https://github.com/xiamiao1121)! - feat: 新增支持配置字段别名
8
+
9
+ ## 4.2.2
4
10
 
5
11
  ### Patch Changes
6
12
 
7
- - 1a00f9fc4: build: style-inject(node 环境下有问题) 替换为 @hi-ui/style-inject
8
- - Updated dependencies [1a00f9fc4]
9
- - @hi-ui/env@4.0.5-alpha.0
10
- - @hi-ui/icons@4.0.19-alpha.0
11
- - @hi-ui/popper@4.1.5-alpha.0
12
- - @hi-ui/scrollbar@4.1.1-alpha.0
13
- - @hi-ui/tooltip@4.0.11-alpha.0
14
- - @hi-ui/array-utils@4.0.5-alpha.0
15
- - @hi-ui/classname@4.0.5-alpha.0
16
- - @hi-ui/core@4.0.9-alpha.0
13
+ - [#2791](https://github.com/XiaoMi/hiui/pull/2791) [`1a00f9fc4`](https://github.com/XiaoMi/hiui/commit/1a00f9fc4a44619059d7852e846b54fedbd56715) Thanks [@zyprepare](https://github.com/zyprepare)! - build: style-inject(node 环境下有问题) 替换为 @hi-ui/style-inject
14
+
15
+ - Updated dependencies [[`1a00f9fc4`](https://github.com/XiaoMi/hiui/commit/1a00f9fc4a44619059d7852e846b54fedbd56715)]:
16
+ - @hi-ui/env@4.0.5
17
+ - @hi-ui/icons@4.0.19
18
+ - @hi-ui/popper@4.1.5
19
+ - @hi-ui/scrollbar@4.1.1
20
+ - @hi-ui/tooltip@4.0.11
21
+ - @hi-ui/array-utils@4.0.5
22
+ - @hi-ui/classname@4.0.5
17
23
 
18
24
  ## 4.2.1
19
25
 
package/lib/cjs/Menu.js CHANGED
@@ -52,6 +52,7 @@ var Menu = /*#__PURE__*/React.forwardRef(function (_a, ref) {
52
52
  className = _a.className,
53
53
  _a$data = _a.data,
54
54
  data = _a$data === void 0 ? NOOP_ARRAY : _a$data,
55
+ fieldNames = _a.fieldNames,
55
56
  _a$placement = _a.placement,
56
57
  placement = _a$placement === void 0 ? 'vertical' : _a$placement,
57
58
  _a$showCollapse = _a.showCollapse,
@@ -81,7 +82,7 @@ var Menu = /*#__PURE__*/React.forwardRef(function (_a, ref) {
81
82
  onClick = _a.onClick,
82
83
  _a$size = _a.size,
83
84
  size = _a$size === void 0 ? 'lg' : _a$size,
84
- rest = tslib.__rest(_a, ["prefixCls", "role", "className", "data", "placement", "showCollapse", "expandedType", "showAllSubMenus", "defaultExpandAll", "defaultExpandedIds", "expandedIds", "onExpand", "defaultActiveId", "activeId", "onClickSubMenu", "collapsed", "defaultCollapsed", "overlayClassName", "onCollapse", "footerRender", "render", "extraHeader", "onClick", "size"]);
85
+ rest = tslib.__rest(_a, ["prefixCls", "role", "className", "data", "fieldNames", "placement", "showCollapse", "expandedType", "showAllSubMenus", "defaultExpandAll", "defaultExpandedIds", "expandedIds", "onExpand", "defaultActiveId", "activeId", "onClickSubMenu", "collapsed", "defaultCollapsed", "overlayClassName", "onCollapse", "footerRender", "render", "extraHeader", "onClick", "size"]);
85
86
  var _useUncontrolledState = useUncontrolledState.useUncontrolledState(defaultActiveId, activeIdProp, onClick),
86
87
  activeId = _useUncontrolledState[0],
87
88
  updateActiveId = _useUncontrolledState[1];
@@ -90,11 +91,14 @@ var Menu = /*#__PURE__*/React.forwardRef(function (_a, ref) {
90
91
  }),
91
92
  activeParents = _useState[0],
92
93
  updateActiveParents = _useState[1];
94
+ var transformedData = React.useMemo(function () {
95
+ return util.transformTreeData(data, fieldNames);
96
+ }, [data, fieldNames]);
93
97
  React.useEffect(function () {
94
- updateActiveParents(util.getAncestorIds(activeId, data));
95
- }, [activeId, data]);
98
+ updateActiveParents(util.getAncestorIds(activeId, transformedData));
99
+ }, [activeId, transformedData]);
96
100
  var _useUncontrolledState2 = useUncontrolledState.useUncontrolledState(function () {
97
- return defaultExpandAll ? treeUtils.getTreeNodesWithChildren(data).map(function (node) {
101
+ return defaultExpandAll ? treeUtils.getTreeNodesWithChildren(transformedData).map(function (node) {
98
102
  return node.id;
99
103
  }) : defaultExpandedIds;
100
104
  }, expandedIdsProp, onExpand),
@@ -152,14 +156,14 @@ var Menu = /*#__PURE__*/React.forwardRef(function (_a, ref) {
152
156
  tagMaxCount = _useState4[0],
153
157
  setTagMaxCount = _useState4[1];
154
158
  var mergedTagList = React.useMemo(function () {
155
- if (showVertical) return data;
156
- if (containerWidth < MIN_WIDTH) return data;
157
- return data.slice(0, Math.min(data.length, containerWidth / MIN_WIDTH));
158
- }, [showVertical, data, containerWidth]);
159
+ if (showVertical) return transformedData;
160
+ if (containerWidth < MIN_WIDTH) return transformedData;
161
+ return transformedData.slice(0, Math.min(transformedData.length, containerWidth / MIN_WIDTH));
162
+ }, [showVertical, transformedData, containerWidth]);
159
163
  var restTagList = React.useMemo(function () {
160
- if (tagMaxCount > 0) return data.slice(tagMaxCount);
164
+ if (tagMaxCount > 0) return transformedData.slice(tagMaxCount);
161
165
  return [];
162
- }, [data, tagMaxCount]);
166
+ }, [transformedData, tagMaxCount]);
163
167
  var getTagWidth = React.useCallback(function (index) {
164
168
  if (!containerElement) return MIN_WIDTH;
165
169
  var elements = containerElement.getElementsByClassName('hi-v4-menu-item');
package/lib/cjs/util.js CHANGED
@@ -62,7 +62,36 @@ var filterTreeData = function filterTreeData(treeData, searchKey, activeId) {
62
62
  return d.title.includes && d.title.includes(searchKey);
63
63
  })) !== null && _d !== void 0 ? _d : [];
64
64
  };
65
+ var transformTreeData = function transformTreeData(data, fieldNames) {
66
+ /**
67
+ * 转换对象
68
+ */
69
+ var getKeyFields = function getKeyFields(node, key) {
70
+ if (fieldNames) {
71
+ return node[fieldNames[key] || key];
72
+ }
73
+ return node[key];
74
+ };
75
+ /**
76
+ * 递归处理树形数组
77
+ */
78
+ var traverseTreeNode = function traverseTreeNode(node) {
79
+ var _a;
80
+ var newNode = Object.assign({}, node);
81
+ newNode.id = getKeyFields(newNode, 'id');
82
+ newNode.title = getKeyFields(newNode, 'title');
83
+ newNode.icon = getKeyFields(newNode, 'icon');
84
+ newNode.disabled = (_a = getKeyFields(newNode, 'disabled')) !== null && _a !== void 0 ? _a : false;
85
+ newNode.children = getKeyFields(newNode, 'children');
86
+ if (newNode.children) {
87
+ newNode.children = newNode.children.map(traverseTreeNode);
88
+ }
89
+ return newNode;
90
+ };
91
+ return data.map(traverseTreeNode);
92
+ };
65
93
  exports.filterTreeData = filterTreeData;
66
94
  exports.getAncestorIds = getAncestorIds;
67
95
  exports.getIdsWithChildren = getIdsWithChildren;
68
96
  exports.getParentId = getParentId;
97
+ exports.transformTreeData = transformTreeData;
package/lib/esm/Menu.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
10
  import { __rest } from 'tslib';
11
- import React, { forwardRef, useState, useEffect, useCallback, useMemo, useLayoutEffect } from 'react';
11
+ import React, { forwardRef, useState, useMemo, useEffect, useCallback, useLayoutEffect } from 'react';
12
12
  import { getPrefixCls, cx } from '@hi-ui/classname';
13
13
  import { MenuUnfoldOutlined, MenuFoldOutlined } from '@hi-ui/icons';
14
14
  import { __DEV__ } from '@hi-ui/env';
@@ -22,7 +22,7 @@ import { useMergeRefs } from '@hi-ui/use-merge-refs';
22
22
  import { uuid } from '@hi-ui/use-id';
23
23
  import { MenuItem } from './MenuItem.js';
24
24
  import MenuContext from './context.js';
25
- import { getAncestorIds } from './util.js';
25
+ import { getAncestorIds, transformTreeData } from './util.js';
26
26
  var MENU_PREFIX = getPrefixCls('menu');
27
27
  var DEFAULT_EXPANDED_IDS = [];
28
28
  var NOOP_ARRAY = [];
@@ -39,6 +39,7 @@ var Menu = /*#__PURE__*/forwardRef(function (_a, ref) {
39
39
  className = _a.className,
40
40
  _a$data = _a.data,
41
41
  data = _a$data === void 0 ? NOOP_ARRAY : _a$data,
42
+ fieldNames = _a.fieldNames,
42
43
  _a$placement = _a.placement,
43
44
  placement = _a$placement === void 0 ? 'vertical' : _a$placement,
44
45
  _a$showCollapse = _a.showCollapse,
@@ -68,7 +69,7 @@ var Menu = /*#__PURE__*/forwardRef(function (_a, ref) {
68
69
  onClick = _a.onClick,
69
70
  _a$size = _a.size,
70
71
  size = _a$size === void 0 ? 'lg' : _a$size,
71
- rest = __rest(_a, ["prefixCls", "role", "className", "data", "placement", "showCollapse", "expandedType", "showAllSubMenus", "defaultExpandAll", "defaultExpandedIds", "expandedIds", "onExpand", "defaultActiveId", "activeId", "onClickSubMenu", "collapsed", "defaultCollapsed", "overlayClassName", "onCollapse", "footerRender", "render", "extraHeader", "onClick", "size"]);
72
+ rest = __rest(_a, ["prefixCls", "role", "className", "data", "fieldNames", "placement", "showCollapse", "expandedType", "showAllSubMenus", "defaultExpandAll", "defaultExpandedIds", "expandedIds", "onExpand", "defaultActiveId", "activeId", "onClickSubMenu", "collapsed", "defaultCollapsed", "overlayClassName", "onCollapse", "footerRender", "render", "extraHeader", "onClick", "size"]);
72
73
  var _useUncontrolledState = useUncontrolledState(defaultActiveId, activeIdProp, onClick),
73
74
  activeId = _useUncontrolledState[0],
74
75
  updateActiveId = _useUncontrolledState[1];
@@ -77,11 +78,14 @@ var Menu = /*#__PURE__*/forwardRef(function (_a, ref) {
77
78
  }),
78
79
  activeParents = _useState[0],
79
80
  updateActiveParents = _useState[1];
81
+ var transformedData = useMemo(function () {
82
+ return transformTreeData(data, fieldNames);
83
+ }, [data, fieldNames]);
80
84
  useEffect(function () {
81
- updateActiveParents(getAncestorIds(activeId, data));
82
- }, [activeId, data]);
85
+ updateActiveParents(getAncestorIds(activeId, transformedData));
86
+ }, [activeId, transformedData]);
83
87
  var _useUncontrolledState2 = useUncontrolledState(function () {
84
- return defaultExpandAll ? getTreeNodesWithChildren(data).map(function (node) {
88
+ return defaultExpandAll ? getTreeNodesWithChildren(transformedData).map(function (node) {
85
89
  return node.id;
86
90
  }) : defaultExpandedIds;
87
91
  }, expandedIdsProp, onExpand),
@@ -139,14 +143,14 @@ var Menu = /*#__PURE__*/forwardRef(function (_a, ref) {
139
143
  tagMaxCount = _useState4[0],
140
144
  setTagMaxCount = _useState4[1];
141
145
  var mergedTagList = useMemo(function () {
142
- if (showVertical) return data;
143
- if (containerWidth < MIN_WIDTH) return data;
144
- return data.slice(0, Math.min(data.length, containerWidth / MIN_WIDTH));
145
- }, [showVertical, data, containerWidth]);
146
+ if (showVertical) return transformedData;
147
+ if (containerWidth < MIN_WIDTH) return transformedData;
148
+ return transformedData.slice(0, Math.min(transformedData.length, containerWidth / MIN_WIDTH));
149
+ }, [showVertical, transformedData, containerWidth]);
146
150
  var restTagList = useMemo(function () {
147
- if (tagMaxCount > 0) return data.slice(tagMaxCount);
151
+ if (tagMaxCount > 0) return transformedData.slice(tagMaxCount);
148
152
  return [];
149
- }, [data, tagMaxCount]);
153
+ }, [transformedData, tagMaxCount]);
150
154
  var getTagWidth = useCallback(function (index) {
151
155
  if (!containerElement) return MIN_WIDTH;
152
156
  var elements = containerElement.getElementsByClassName('hi-v4-menu-item');
package/lib/esm/util.js CHANGED
@@ -57,4 +57,32 @@ var filterTreeData = function filterTreeData(treeData, searchKey, activeId) {
57
57
  return d.title.includes && d.title.includes(searchKey);
58
58
  })) !== null && _d !== void 0 ? _d : [];
59
59
  };
60
- export { filterTreeData, getAncestorIds, getIdsWithChildren, getParentId };
60
+ var transformTreeData = function transformTreeData(data, fieldNames) {
61
+ /**
62
+ * 转换对象
63
+ */
64
+ var getKeyFields = function getKeyFields(node, key) {
65
+ if (fieldNames) {
66
+ return node[fieldNames[key] || key];
67
+ }
68
+ return node[key];
69
+ };
70
+ /**
71
+ * 递归处理树形数组
72
+ */
73
+ var traverseTreeNode = function traverseTreeNode(node) {
74
+ var _a;
75
+ var newNode = Object.assign({}, node);
76
+ newNode.id = getKeyFields(newNode, 'id');
77
+ newNode.title = getKeyFields(newNode, 'title');
78
+ newNode.icon = getKeyFields(newNode, 'icon');
79
+ newNode.disabled = (_a = getKeyFields(newNode, 'disabled')) !== null && _a !== void 0 ? _a : false;
80
+ newNode.children = getKeyFields(newNode, 'children');
81
+ if (newNode.children) {
82
+ newNode.children = newNode.children.map(traverseTreeNode);
83
+ }
84
+ return newNode;
85
+ };
86
+ return data.map(traverseTreeNode);
87
+ };
88
+ export { filterTreeData, getAncestorIds, getIdsWithChildren, getParentId, transformTreeData };
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { HiBaseHTMLProps, HiBaseSizeEnum } from '@hi-ui/core';
2
+ import { HiBaseFieldNames, HiBaseHTMLProps, HiBaseSizeEnum } from '@hi-ui/core';
3
3
  import { MenuDataItem, MenuFooterRenderProps } from './types';
4
4
  /**
5
5
  * 菜单
@@ -10,6 +10,10 @@ export interface MenuProps extends Omit<HiBaseHTMLProps<'div'>, 'onClick'> {
10
10
  * 菜单项数据列表
11
11
  */
12
12
  data: MenuDataItem[];
13
+ /**
14
+ * 设置 data 中 id, title, disabled, children 对应的 key
15
+ */
16
+ fieldNames?: HiBaseFieldNames;
13
17
  /**
14
18
  * 默认激活的菜单项 id
15
19
  */
@@ -1,6 +1,8 @@
1
- /// <reference types="react" />
2
1
  import { MenuDataItem } from './types';
2
+ import { HiBaseFieldNames } from '@hi-ui/core';
3
+ import React from 'react';
3
4
  export declare const getParentId: (id: string | number, data: Record<string, any>[]) => string | number;
4
5
  export declare const getAncestorIds: (id: string | number, data: Record<string, any>[], arr?: (string | number)[]) => (string | number)[];
5
- export declare const getIdsWithChildren: (treeData: MenuDataItem[]) => import("react").ReactText[];
6
+ export declare const getIdsWithChildren: (treeData: MenuDataItem[]) => React.ReactText[];
6
7
  export declare const filterTreeData: (treeData: MenuDataItem[], searchKey: string, activeId: string | number) => MenuDataItem[];
8
+ export declare const transformTreeData: (data: MenuDataItem[], fieldNames?: HiBaseFieldNames | undefined) => MenuDataItem[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/menu",
3
- "version": "4.2.2-alpha.0",
3
+ "version": "4.3.0",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",
@@ -43,14 +43,14 @@
43
43
  "url": "https://github.com/XiaoMi/hiui/issues"
44
44
  },
45
45
  "dependencies": {
46
- "@hi-ui/array-utils": "^4.0.5-alpha.0",
47
- "@hi-ui/classname": "^4.0.5-alpha.0",
48
- "@hi-ui/env": "^4.0.5-alpha.0",
49
- "@hi-ui/icons": "^4.0.19-alpha.0",
50
- "@hi-ui/popper": "^4.1.5-alpha.0",
51
- "@hi-ui/scrollbar": "^4.1.1-alpha.0",
46
+ "@hi-ui/array-utils": "^4.0.5",
47
+ "@hi-ui/classname": "^4.0.5",
48
+ "@hi-ui/env": "^4.0.5",
49
+ "@hi-ui/icons": "^4.0.19",
50
+ "@hi-ui/popper": "^4.1.5",
51
+ "@hi-ui/scrollbar": "^4.1.1",
52
52
  "@hi-ui/times": "^4.0.4",
53
- "@hi-ui/tooltip": "^4.0.11-alpha.0",
53
+ "@hi-ui/tooltip": "^4.0.11",
54
54
  "@hi-ui/tree-utils": "^4.1.5",
55
55
  "@hi-ui/type-assertion": "^4.0.4",
56
56
  "@hi-ui/use-id": "^4.0.4",
@@ -61,12 +61,12 @@
61
61
  "react-transition-group": "^4.4.2"
62
62
  },
63
63
  "peerDependencies": {
64
- "@hi-ui/core": ">=4.0.9-alpha.0",
64
+ "@hi-ui/core": ">=4.0.8",
65
65
  "react": ">=16.8.6",
66
66
  "react-dom": ">=16.8.6"
67
67
  },
68
68
  "devDependencies": {
69
- "@hi-ui/core": "^4.0.9-alpha.0",
69
+ "@hi-ui/core": "^4.0.8",
70
70
  "@hi-ui/core-css": "^4.1.5",
71
71
  "react": "^17.0.1",
72
72
  "react-dom": "^17.0.1"