@kep-platform/basic-component 0.0.13 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,7 +29,6 @@ declare global {
29
29
  key: Key;
30
30
  title?: string;
31
31
  label: ReactNode;
32
- onSelect?(item: MenuItem): void;
33
32
  };
34
33
  type RequiredTreeNodeKeys = 'key' | 'title' | 'children';
35
34
 
@@ -10,7 +10,7 @@ export type ColumnsProps = {
10
10
  } & HtmlHTMLAttributes<HTMLUListElement>;
11
11
  export declare const Column: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components/dist/types").Substitute<Omit<import("styled-components").FastOmit<React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>, never>, "ref"> & {
12
12
  ref?: ((instance: HTMLLIElement | null) => void) | React.RefObject<HTMLLIElement> | null | undefined;
13
- }, Omit<ColumnType, "dataIndex" | "render"> & {
13
+ }, Omit<ColumnType, "render" | "dataIndex"> & {
14
14
  key: React.Key;
15
15
  title?: React.ReactNode;
16
16
  index?: number | undefined;
@@ -1,9 +1,11 @@
1
- import React, { ReactNode } from 'react';
1
+ import React, { ReactNode, type Key } from 'react';
2
2
  export type MenuProps = {
3
3
  items: MenuItem[];
4
4
  direction?: Direction;
5
+ activeKey?: Key;
6
+ onSelect?: (key: Key, item: MenuItem) => void;
5
7
  };
6
- declare function Menu({ items, direction }: MenuProps): React.JSX.Element;
8
+ declare function Menu({ items, direction, onSelect, activeKey: outerActiveKey }: MenuProps): React.JSX.Element;
7
9
  declare namespace Menu {
8
10
  var Popup: typeof PopupMenu;
9
11
  }
package/dist/Menu/Menu.js CHANGED
@@ -1,4 +1,4 @@
1
- var _excluded = ["children", "trigger"];
1
+ var _excluded = ["children", "onSelect", "activeKey", "trigger"];
2
2
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3
3
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
4
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
@@ -20,7 +20,7 @@ var MenuList = styled(List).withConfig({
20
20
  var MenuListItem = styled(ListItem).withConfig({
21
21
  displayName: "MenuListItem",
22
22
  componentId: "basic-component-347b__sc-d665dh-1"
23
- })(["background-color:var(--kep-platform-color-bg-base);border-radius:var(--kep-platform-border-radius-sm);margin:var(--kep-platform-margin-xxxs);padding:0 var(--kep-platform-padding-xs);height:var(--kep-platform-menu-item-height);line-height:var(--kep-platform-menu-item-line-height);&:hover{background-color:var(--kep-platform-color-bg-hover);}", ""], function (props) {
23
+ })(["background-color:var(--kep-platform-color-bg-base);border-radius:var(--kep-platform-border-radius-sm);margin:var(--kep-platform-margin-xxxs);padding:0 var(--kep-platform-padding-xs);height:var(--kep-platform-menu-item-height);line-height:var(--kep-platform-menu-item-line-height);color:var(--kep-platform-color-text) !important;&:hover{background-color:var(--kep-platform-color-bg-hover);}", ""], function (props) {
24
24
  if (props.active) {
25
25
  return css(["background-color:var(--kep-platform-color-bg-active) !important;color:var(--kep-platform-color-text-active) !important;"]);
26
26
  }
@@ -28,38 +28,48 @@ var MenuListItem = styled(ListItem).withConfig({
28
28
  function Menu(_ref) {
29
29
  var items = _ref.items,
30
30
  _ref$direction = _ref.direction,
31
- direction = _ref$direction === void 0 ? 'row' : _ref$direction;
32
- var _useState = useState(null),
31
+ direction = _ref$direction === void 0 ? 'row' : _ref$direction,
32
+ onSelect = _ref.onSelect,
33
+ outerActiveKey = _ref.activeKey;
34
+ var _useState = useState(outerActiveKey),
33
35
  _useState2 = _slicedToArray(_useState, 2),
34
36
  activeKey = _useState2[0],
35
37
  setActiveKey = _useState2[1];
36
- var menuClickHandler = useCallback(function (e) {
37
- var dataKey = e.target.getAttribute('data-key');
38
- setActiveKey(dataKey);
39
- }, []);
38
+ useEffect(function () {
39
+ setActiveKey(outerActiveKey);
40
+ }, [outerActiveKey]);
40
41
  return /*#__PURE__*/React.createElement(MenuList, {
41
42
  direction: direction
42
43
  }, items.map(function (item) {
43
44
  return /*#__PURE__*/React.createElement(MenuListItem, {
44
45
  key: item.key,
45
46
  active: activeKey === item.key,
46
- onClick: menuClickHandler,
47
+ onClick: function onClick() {
48
+ setActiveKey(item.key);
49
+ onSelect === null || onSelect === void 0 || onSelect(item.key, item);
50
+ },
47
51
  "data-key": item.key
48
52
  }, item.label);
49
53
  }));
50
54
  }
51
55
  export function PopupMenu(props) {
52
56
  var children = props.children,
57
+ onSelect = props.onSelect,
58
+ outerActiveKey = props.activeKey,
53
59
  _props$trigger = props.trigger,
54
60
  trigger = _props$trigger === void 0 ? 'click' : _props$trigger,
55
61
  rest = _objectWithoutProperties(props, _excluded);
56
- var _useState3 = useState({
62
+ var _useState3 = useState(outerActiveKey),
63
+ _useState4 = _slicedToArray(_useState3, 2),
64
+ activeKey = _useState4[0],
65
+ setActiveKey = _useState4[1];
66
+ var _useState5 = useState({
57
67
  left: 0,
58
68
  top: 0
59
69
  }),
60
- _useState4 = _slicedToArray(_useState3, 2),
61
- pos = _useState4[0],
62
- setPos = _useState4[1];
70
+ _useState6 = _slicedToArray(_useState5, 2),
71
+ pos = _useState6[0],
72
+ setPos = _useState6[1];
63
73
  var triggerController = useRef(null);
64
74
  var popupBox = useRef(null);
65
75
  var mergeProps = useMemo(function () {
@@ -68,7 +78,6 @@ export function PopupMenu(props) {
68
78
  };
69
79
  }, []);
70
80
  var showPopupMenu = useCallback(function (e) {
71
- e.stopPropagation();
72
81
  e.preventDefault();
73
82
  var _ref2 = e,
74
83
  clientX = _ref2.clientX,
@@ -77,6 +86,7 @@ export function PopupMenu(props) {
77
86
  left: clientX,
78
87
  top: clientY
79
88
  });
89
+ setActiveKey(undefined);
80
90
  }, []);
81
91
  var hidePopupMenu = useCallback(function () {
82
92
  setPos({
@@ -84,6 +94,11 @@ export function PopupMenu(props) {
84
94
  top: -999
85
95
  });
86
96
  }, []);
97
+ var onSelectHandler = useCallback(function (key, item) {
98
+ onSelect === null || onSelect === void 0 || onSelect(key, item);
99
+ setActiveKey(key);
100
+ hidePopupMenu();
101
+ }, [onSelect, hidePopupMenu]);
87
102
  useClickOutsize('mousedown', popupBox, hidePopupMenu);
88
103
  useClickOutsize('scroll', popupBox, hidePopupMenu);
89
104
  useEffect(function () {
@@ -97,8 +112,11 @@ export function PopupMenu(props) {
97
112
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(PopupBox, {
98
113
  left: pos.left,
99
114
  top: pos.top,
100
- ref: popupBox
115
+ ref: popupBox,
116
+ onClick: hidePopupMenu
101
117
  }, /*#__PURE__*/React.createElement(Menu, _extends({}, rest, {
118
+ activeKey: outerActiveKey || activeKey,
119
+ onSelect: onSelectHandler,
102
120
  direction: "column"
103
121
  }))), /*#__PURE__*/React.cloneElement(children, mergeProps));
104
122
  } else console.warn('PopupMenu需要一个触发元素!');
@@ -4,9 +4,10 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
4
4
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
- import { GlobalStyles, Tree } from "./..";
7
+ import { GlobalStyles } from "./..";
8
8
  import React, { useState } from 'react';
9
9
  import styled from 'styled-components';
10
+ import Tree from "./Tree";
10
11
  var Title = styled.div.withConfig({
11
12
  displayName: "Title",
12
13
  componentId: "basic-component-347b__sc-klaw2r-0"
@@ -24,6 +25,7 @@ export default (function () {
24
25
  key: '1',
25
26
  title: 'jss',
26
27
  count: 1,
28
+ groupBy: 'count',
27
29
  children: [{
28
30
  key: '4',
29
31
  title: 'skjdfkljasklfjkljdsklajfkl就是要长一的点点滴滴的点点滴滴哒哒哒哒哒哒哒哒哒哒哒哒哒哒',
@@ -41,24 +43,25 @@ export default (function () {
41
43
  key: '2121212121212',
42
44
  title: '2',
43
45
  count: 1,
44
- children: []
45
- }, {
46
- key: '3',
47
- title: 'jss',
48
- name: 'jss',
49
- children: [],
50
- count: 2
51
- }, {
52
- key: '10',
53
- title: 'jss',
54
- name: 'jss',
55
- children: [],
56
- count: 2
57
- }, {
58
- key: '20',
59
- title: '3',
60
- children: [],
61
- count: 2
46
+ groupBy: 'count',
47
+ children: [{
48
+ key: '3',
49
+ title: 'jss',
50
+ name: 'jss',
51
+ children: [],
52
+ count: 2
53
+ }, {
54
+ key: '10',
55
+ title: 'jss',
56
+ name: 'jss',
57
+ children: [],
58
+ count: 2
59
+ }, {
60
+ key: '20',
61
+ title: '3',
62
+ children: [],
63
+ count: 2
64
+ }]
62
65
  }];
63
66
  var columns = [{
64
67
  key: 'key',
@@ -94,8 +97,12 @@ export default (function () {
94
97
  $css: []
95
98
  }), /*#__PURE__*/React.createElement(Tree, {
96
99
  title: /*#__PURE__*/React.createElement(Title, null, "\u6807\u8BC6"),
100
+ multiple: true,
97
101
  titleWidth: 400,
98
102
  contextMenuRender: contextMenuRender,
103
+ onContextMenuItemSelect: function onContextMenuItemSelect(node) {
104
+ console.log(node.key);
105
+ },
99
106
  treeData: treeData,
100
107
  columns: columns,
101
108
  selectedKeys: selectedKeys,
@@ -105,7 +112,6 @@ export default (function () {
105
112
  },
106
113
  onExpand: function onExpand(expandedKeys) {
107
114
  return setExpandedKeys(expandedKeys);
108
- },
109
- groupBy: "count"
115
+ }
110
116
  }));
111
117
  });
@@ -8,5 +8,5 @@ type MainPropertyProps = {
8
8
  property?: ReactNode;
9
9
  isFlex: boolean;
10
10
  };
11
- export declare const MainProperty: React.FC<MainPropertyProps>;
11
+ export declare const MainProperty: React.ForwardRefExoticComponent<MainPropertyProps & React.RefAttributes<HTMLDivElement>>;
12
12
  export {};
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { forwardRef } from 'react';
2
2
  import styled from 'styled-components';
3
3
  import { Flex, FlexItem } from "../Flex";
4
4
  export var MainArea = styled(FlexItem).withConfig({
@@ -10,14 +10,18 @@ var PropertiesArea = styled(FlexItem).withConfig({
10
10
  componentId: "basic-component-347b__sc-u1pspr-1"
11
11
  })([""]);
12
12
  /* 有属性就渲染属性,没有属性就不处理 */
13
- export var MainProperty = function MainProperty(_ref) {
13
+ export var MainProperty = /*#__PURE__*/forwardRef(function (_ref, ref) {
14
14
  var mainWidth = _ref.mainWidth,
15
15
  main = _ref.main,
16
16
  property = _ref.property,
17
17
  isFlex = _ref.isFlex;
18
- if (property) return /*#__PURE__*/React.createElement(Flex, null, /*#__PURE__*/React.createElement(MainArea, {
18
+ if (property) return /*#__PURE__*/React.createElement(Flex, {
19
+ ref: ref
20
+ }, /*#__PURE__*/React.createElement(MainArea, {
19
21
  width: mainWidth
20
22
  }, main), /*#__PURE__*/React.createElement(PropertiesArea, {
21
23
  flex: isFlex ? 1 : undefined
22
- }, property));else return main;
23
- };
24
+ }, property));else return /*#__PURE__*/React.cloneElement(main, {
25
+ ref: ref
26
+ });
27
+ });
@@ -9,7 +9,8 @@ export type TreeProps = {
9
9
  titleRender?: ((node: TreeNodeType) => ReactNode | null) | null | undefined;
10
10
  onExpand?: onExpandHandler;
11
11
  onSelect?: onSelectHandler;
12
- contextMenuRender: (node: TreeNodeType) => MenuItem[];
12
+ contextMenuRender?: (node: TreeNodeType) => MenuItem[];
13
+ onContextMenuItemSelect?: (node: TreeNodeType, key: Key, item: MenuItem) => void;
13
14
  titleWidth?: number;
14
15
  title?: ReactNode;
15
16
  isFlex?: boolean;
package/dist/Tree/Tree.js CHANGED
@@ -1,17 +1,15 @@
1
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
- import React, { useMemo, useRef, useState } from 'react';
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
3
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
4
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
5
+ import React, { useMemo, useRef } from 'react';
8
6
  import styled, { css } from 'styled-components';
9
7
  import { BoxShadowBox } from "../BoxShadowBox";
10
8
  import { Column, Columns } from "../Columns";
11
9
  import { DefaultColumnWidth } from "../Columns/Columns";
12
10
  import { List } from "../List";
13
- import { MainArea } from "./MainProperties";
14
- import { MainProperty, TreeNodeController } from "./TreeNode";
11
+ import { MainArea, MainProperty } from "./MainProperties";
12
+ import { TreeNodeController, nodeTypeMap } from "./TreeNode";
15
13
  var TreeContainer = styled.div.withConfig({
16
14
  displayName: "TreeContainer",
17
15
  componentId: "basic-component-347b__sc-n4lkq9-0"
@@ -33,35 +31,7 @@ var TreeHeader = styled.div.withConfig({
33
31
  return css(["width:", "px;"], props.width);
34
32
  }
35
33
  }, Column, MainArea);
36
- var rootLevel = 0;
37
- var TreeGroupNode = styled.div.withConfig({
38
- displayName: "TreeGroupNode",
39
- componentId: "basic-component-347b__sc-n4lkq9-3"
40
- })(["height:40px;line-height:40px;padding:0 16px;color:var(--kep-platform-color-primary);cursor:pointer;border:1px dotted var(--kep-platform-color-primary);"]);
41
- var TreeNodeGroup = function TreeNodeGroup(props) {
42
- var _useState = useState(false),
43
- _useState2 = _slicedToArray(_useState, 2),
44
- expanded = _useState2[0],
45
- setExpanded = _useState2[1];
46
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TreeGroupNode, {
47
- onClick: function onClick() {
48
- return setExpanded(!expanded);
49
- }
50
- }, props.groupName, "\uFF1A", props.groupValue), expanded && props.children);
51
- };
52
- function groupTreeNodeByPropertyName(arr, propertyName, fieldNames) {
53
- var grouped = arr.reduce(function (acc, obj) {
54
- var key = obj[propertyName] || obj[fieldNames.key];
55
- if (key) {
56
- if (!acc[key]) {
57
- acc[key] = [];
58
- }
59
- acc[key].push(obj);
60
- } else {}
61
- return acc;
62
- }, {});
63
- return grouped;
64
- }
34
+ var rootLevel = -1;
65
35
  var Tree = function Tree(_ref) {
66
36
  var treeData = _ref.treeData,
67
37
  _ref$columns = _ref.columns,
@@ -87,18 +57,21 @@ var Tree = function Tree(_ref) {
87
57
  _ref$onExpand = _ref.onExpand,
88
58
  onExpand = _ref$onExpand === void 0 ? function () {} : _ref$onExpand,
89
59
  contextMenuRender = _ref.contextMenuRender,
60
+ onContextMenuItemSelect = _ref.onContextMenuItemSelect,
90
61
  _ref$multiple = _ref.multiple,
91
62
  multiple = _ref$multiple === void 0 ? false : _ref$multiple,
92
63
  _ref$bordered = _ref.bordered,
93
- bordered = _ref$bordered === void 0 ? false : _ref$bordered,
94
- _ref$groupBy = _ref.groupBy,
95
- groupBy = _ref$groupBy === void 0 ? null : _ref$groupBy;
64
+ bordered = _ref$bordered === void 0 ? false : _ref$bordered;
65
+ var rootNode = useMemo(function () {
66
+ return _defineProperty(_defineProperty(_defineProperty({}, fieldNames.key, 'root'), "nodeType", nodeTypeMap.virtual), "children", treeData);
67
+ }, [treeData]);
68
+
96
69
  /* 复用Columns组件显示行,构造一个类似表格头数据来适应Columns组件的数据结构 */
97
70
  var treeHeaderRowData = useMemo(function () {
98
71
  var data = {};
99
- if (columns) columns.forEach(function (_ref2) {
100
- var title = _ref2.title,
101
- dataIndex = _ref2.dataIndex;
72
+ if (columns) columns.forEach(function (_ref3) {
73
+ var title = _ref3.title,
74
+ dataIndex = _ref3.dataIndex;
102
75
  data[dataIndex] = title;
103
76
  });
104
77
  return data;
@@ -143,15 +116,6 @@ var Tree = function Tree(_ref) {
143
116
  }
144
117
  return newColumns;
145
118
  }, [columns, fieldNames]);
146
- var treeNodeGroup = useMemo(function () {
147
- if (groupBy) return groupTreeNodeByPropertyName(treeData, groupBy, fieldNames);
148
- }, [treeData, groupBy]);
149
- var groupByTitle = useMemo(function () {
150
- var _columns$find;
151
- return (_columns$find = columns.find(function (column) {
152
- return column.dataIndex === groupBy;
153
- })) === null || _columns$find === void 0 ? void 0 : _columns$find.title;
154
- }, [columns, groupBy]);
155
119
  var boxShadowBox = useRef(null);
156
120
  /* 树组件的结构其实比较简单,就是一个树标题(TableMode下),然后树节点形成的列表 */
157
121
  return /*#__PURE__*/React.createElement(TreeContainer, {
@@ -187,71 +151,24 @@ var Tree = function Tree(_ref) {
187
151
  isFlex: isFlex
188
152
  })), /*#__PURE__*/React.createElement(TreeBody, {
189
153
  direction: "column"
190
- }, !treeNodeGroup && !groupBy && treeData.map(function (node) {
191
- return /*#__PURE__*/React.createElement(TreeNodeController, {
192
- key: node[fieldNames.key],
193
- selectedKeys: selectedKeys,
194
- onSelect: onSelect,
195
- expandedKeys: expandedKeys,
196
- onExpand: onExpand,
197
- width: isTableMode && !isFlex ? rowWidth : undefined,
198
- node: node,
199
- fieldNames: fieldNames,
200
- isFlex: isFlex,
201
- isTableMode: !!isTableMode,
202
- columns: columns,
203
- multiple: multiple,
204
- level: rootLevel,
205
- mainWidth: titleWidth,
206
- contextMenuRender: contextMenuRender,
207
- titleRender: titleRender
208
- });
209
- }), treeNodeGroup && groupBy && Object.keys(treeNodeGroup).map(function (key) {
210
- var group = treeNodeGroup[key];
211
- if (group.length < 2) {
212
- return /*#__PURE__*/React.createElement(TreeNodeController, {
213
- key: group[0][fieldNames.key],
214
- selectedKeys: selectedKeys,
215
- onSelect: onSelect,
216
- expandedKeys: expandedKeys,
217
- onExpand: onExpand,
218
- width: isTableMode && !isFlex ? rowWidth : undefined,
219
- node: group[0],
220
- fieldNames: fieldNames,
221
- isFlex: isFlex,
222
- isTableMode: !!isTableMode,
223
- columns: columns,
224
- multiple: multiple,
225
- level: rootLevel,
226
- mainWidth: titleWidth,
227
- contextMenuRender: contextMenuRender,
228
- titleRender: titleRender
229
- });
230
- }
231
- return /*#__PURE__*/React.createElement(TreeNodeGroup, {
232
- groupName: groupByTitle,
233
- key: key,
234
- groupValue: key
235
- }, treeNodeGroup[key].map(function (node) {
236
- return /*#__PURE__*/React.createElement(TreeNodeController, {
237
- key: node[fieldNames.key],
238
- selectedKeys: selectedKeys,
239
- onSelect: onSelect,
240
- expandedKeys: expandedKeys,
241
- onExpand: onExpand,
242
- width: isTableMode && !isFlex ? rowWidth : undefined,
243
- node: node,
244
- fieldNames: fieldNames,
245
- isFlex: isFlex,
246
- isTableMode: !!isTableMode,
247
- columns: columns,
248
- multiple: multiple,
249
- level: rootLevel + 1,
250
- mainWidth: titleWidth,
251
- contextMenuRender: contextMenuRender,
252
- titleRender: titleRender
253
- });
254
- }));
154
+ }, /*#__PURE__*/React.createElement(TreeNodeController, {
155
+ key: rootNode[fieldNames.key],
156
+ selectedKeys: selectedKeys,
157
+ onSelect: onSelect,
158
+ expandedKeys: expandedKeys,
159
+ onExpand: onExpand,
160
+ width: isTableMode && !isFlex ? rowWidth : undefined,
161
+ node: rootNode,
162
+ fieldNames: fieldNames,
163
+ isFlex: isFlex,
164
+ isTableMode: isTableMode,
165
+ columns: columns,
166
+ multiple: multiple,
167
+ level: rootLevel,
168
+ mainWidth: titleWidth,
169
+ contextMenuRender: contextMenuRender,
170
+ onContextMenuItemSelect: onContextMenuItemSelect,
171
+ titleRender: titleRender
255
172
  })));
256
173
  };
257
174
  export default Tree;
@@ -1,5 +1,4 @@
1
1
  import React, { Key, ReactNode } from 'react';
2
- import { MainProperty } from './MainProperties';
3
2
  export type TreeNodeControllerProps = {
4
3
  node: TreeNodeType;
5
4
  selectedKeys: Key[];
@@ -15,7 +14,10 @@ export type TreeNodeControllerProps = {
15
14
  expandedKeys: Key[];
16
15
  onExpand: onExpandHandler;
17
16
  contextMenuRender?: (node: TreeNodeType) => MenuItem[];
17
+ onContextMenuItemSelect?: (node: TreeNodeType, key: Key, item: MenuItem) => void;
18
18
  titleRender?: ((node: TreeNodeType) => ReactNode | null) | null | undefined;
19
19
  };
20
+ export declare const nodeTypeMap: {
21
+ virtual: string;
22
+ };
20
23
  export declare const TreeNodeController: React.FC<TreeNodeControllerProps>;
21
- export { MainProperty };
@@ -1,5 +1,11 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
1
7
  import { CaretRightOutlined } from '@ant-design/icons';
2
- import React, { useCallback, useMemo } from 'react';
8
+ import React, { useCallback, useMemo, useState } from 'react';
3
9
  import styled, { css } from 'styled-components';
4
10
  import { Column, Columns } from "../Columns";
5
11
  import { Flex, FlexItem } from "../Flex";
@@ -27,6 +33,62 @@ var TreeNode = styled(ListItem).withConfig({
27
33
  return css(["background-color:var(--kep-platform-color-bg-active) !important;color:var(--kep-platform-color-text-active) !important;& ", ",& ", "{background-color:var(--kep-platform-color-bg-active) !important;color:var(--kep-platform-color-text-active) !important;}& ", "{overflow-x:auto;& > ", "{width:auto;}}"], Column, MainArea, MainArea, Flex);
28
34
  }
29
35
  });
36
+ var TreeNodeGroupNode = styled.li.withConfig({
37
+ displayName: "TreeNodeGroupNode",
38
+ componentId: "basic-component-347b__sc-tzoomn-2"
39
+ })(["height:40px;line-height:40px;background-color:var(--kep-platform-color-primary);color:var(--kep-platform-color-white);& span{color:var(--kep-platform-color-white) !important;}cursor:pointer;", ""], function (props) {
40
+ if (props.width) {
41
+ return css(["width:", "px;"], props.width);
42
+ }
43
+ });
44
+ var TreeNodeGroup = function TreeNodeGroup(props) {
45
+ var _useState = useState(false),
46
+ _useState2 = _slicedToArray(_useState, 2),
47
+ expanded = _useState2[0],
48
+ setExpanded = _useState2[1];
49
+ if (React.Children.toArray(props.children).length > 1) return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TreeNodeGroupNode, {
50
+ width: props.width,
51
+ onClick: function onClick() {
52
+ return setExpanded(!expanded);
53
+ }
54
+ }, /*#__PURE__*/React.createElement(Flex, {
55
+ width: props.mainWidth,
56
+ style: {
57
+ position: 'sticky',
58
+ left: 0
59
+ }
60
+ }, /*#__PURE__*/React.createElement(FlexItem, null, /*#__PURE__*/React.createElement(Indent, {
61
+ level: props.level
62
+ })), /*#__PURE__*/React.createElement(FlexItem, {
63
+ width: 24,
64
+ align: "center",
65
+ cursor: "pointer"
66
+ }, /*#__PURE__*/React.createElement(ExpandedIcon, {
67
+ expanded: expanded
68
+ })), /*#__PURE__*/React.createElement(FlexItem, {
69
+ flex: 1
70
+ }, props.groupName, " : ", props.groupValue))), expanded && React.Children.map(props.children, function (child) {
71
+ return /*#__PURE__*/React.cloneElement(child, {
72
+ level: props.level + 1
73
+ });
74
+ }));else return props.children;
75
+ };
76
+ function groupTreeNodeByPropertyName(arr, propertyName, fieldNames) {
77
+ var grouped = arr.reduce(function (acc, obj) {
78
+ var key = obj[propertyName || fieldNames.key] || obj[fieldNames.key];
79
+ if (key) {
80
+ if (!acc[key]) {
81
+ acc[key] = [];
82
+ }
83
+ acc[key].push(obj);
84
+ } else {}
85
+ return acc;
86
+ }, {});
87
+ return grouped;
88
+ }
89
+ export var nodeTypeMap = {
90
+ virtual: 'virtual'
91
+ };
30
92
  export var TreeNodeController = function TreeNodeController(_ref) {
31
93
  var node = _ref.node,
32
94
  _ref$selectedKeys = _ref.selectedKeys,
@@ -43,6 +105,7 @@ export var TreeNodeController = function TreeNodeController(_ref) {
43
105
  expandedKeys = _ref.expandedKeys,
44
106
  onExpand = _ref.onExpand,
45
107
  contextMenuRender = _ref.contextMenuRender,
108
+ onContextMenuItemSelect = _ref.onContextMenuItemSelect,
46
109
  titleRender = _ref.titleRender;
47
110
  /* 处理子节点缩进 */
48
111
  var childLevel = useMemo(function () {
@@ -52,8 +115,17 @@ export var TreeNodeController = function TreeNodeController(_ref) {
52
115
  return selectedKeys.includes(node[fieldNames.key]);
53
116
  }, [selectedKeys, fieldNames, node]);
54
117
  var expanded = useMemo(function () {
55
- return expandedKeys.includes(node[fieldNames.key]);
118
+ return expandedKeys.includes(node[fieldNames.key]) || node.nodeType === nodeTypeMap.virtual;
56
119
  }, [expandedKeys, fieldNames, node]);
120
+ var treeNodeGroup = useMemo(function () {
121
+ return groupTreeNodeByPropertyName(node.children || [], node.groupBy, fieldNames);
122
+ }, [node.children, node.groupBy]);
123
+ var groupTitle = useMemo(function () {
124
+ var _columns$find;
125
+ return (_columns$find = columns.find(function (column) {
126
+ return column.dataIndex === node.groupBy;
127
+ })) === null || _columns$find === void 0 ? void 0 : _columns$find.title;
128
+ }, [columns, node.groupBy]);
57
129
  var onClickNodeHandler = useCallback(function (e) {
58
130
  if (multiple) {
59
131
  if (selected) {
@@ -96,11 +168,7 @@ export var TreeNodeController = function TreeNodeController(_ref) {
96
168
  });
97
169
  }
98
170
  }, [expandedKeys, onExpand, expanded]);
99
- var treeNode = /*#__PURE__*/React.createElement(TreeNode, {
100
- selected: selectedKeys.includes(node[fieldNames.key]),
101
- width: width,
102
- onClick: onClickNodeHandler
103
- }, /*#__PURE__*/React.createElement(MainProperty, {
171
+ var nodeEntity = /*#__PURE__*/React.createElement(MainProperty, {
104
172
  isFlex: isFlex,
105
173
  mainWidth: mainWidth,
106
174
  main: /*#__PURE__*/React.createElement(Flex, null, /*#__PURE__*/React.createElement(FlexItem, null, /*#__PURE__*/React.createElement(Indent, {
@@ -121,29 +189,46 @@ export var TreeNodeController = function TreeNodeController(_ref) {
121
189
  rowData: node,
122
190
  isFlex: isFlex
123
191
  })
124
- }));
125
- return /*#__PURE__*/React.createElement(React.Fragment, null, contextMenuRender && /*#__PURE__*/React.createElement(Menu.Popup, {
192
+ });
193
+ return /*#__PURE__*/React.createElement(React.Fragment, null, node.nodeType !== nodeTypeMap.virtual && /*#__PURE__*/React.createElement(TreeNode, {
194
+ selected: selectedKeys.includes(node[fieldNames.key]),
195
+ width: width,
196
+ onClick: onClickNodeHandler,
197
+ onContextMenu: onClickNodeHandler
198
+ }, contextMenuRender ? /*#__PURE__*/React.createElement(Menu.Popup, {
126
199
  trigger: "contextmenu",
127
- items: contextMenuRender(node)
128
- }, treeNode), !contextMenuRender && treeNode, expanded && node.children.map(function (childNode) {
129
- return /*#__PURE__*/React.createElement(TreeNodeController, {
200
+ items: contextMenuRender(node),
201
+ onSelect: function onSelect(key, item) {
202
+ return onContextMenuItemSelect === null || onContextMenuItemSelect === void 0 ? void 0 : onContextMenuItemSelect(node, key, item);
203
+ }
204
+ }, nodeEntity) : nodeEntity), expanded && treeNodeGroup && Object.keys(treeNodeGroup).map(function (groupKey) {
205
+ return /*#__PURE__*/React.createElement(TreeNodeGroup, {
130
206
  width: width,
131
- selectedKeys: selectedKeys,
132
- key: childNode[fieldNames.key],
133
- node: childNode,
134
- fieldNames: fieldNames,
135
- isFlex: isFlex,
136
- isTableMode: isTableMode,
137
- columns: columns,
138
- onSelect: onSelect,
139
- multiple: multiple,
140
- level: childLevel,
141
207
  mainWidth: mainWidth,
142
- onExpand: onExpand,
143
- expandedKeys: expandedKeys,
144
- contextMenuRender: contextMenuRender,
145
- titleRender: titleRender
146
- });
208
+ key: groupKey,
209
+ groupValue: groupKey,
210
+ groupName: groupTitle,
211
+ level: childLevel
212
+ }, treeNodeGroup[groupKey].map(function (childNode) {
213
+ return /*#__PURE__*/React.createElement(TreeNodeController, {
214
+ width: width,
215
+ selectedKeys: selectedKeys,
216
+ key: childNode[fieldNames.key],
217
+ node: childNode,
218
+ fieldNames: fieldNames,
219
+ isFlex: isFlex,
220
+ isTableMode: isTableMode,
221
+ columns: columns,
222
+ onSelect: onSelect,
223
+ multiple: multiple,
224
+ level: childLevel,
225
+ mainWidth: mainWidth,
226
+ onExpand: onExpand,
227
+ expandedKeys: expandedKeys,
228
+ contextMenuRender: contextMenuRender,
229
+ titleRender: titleRender,
230
+ onContextMenuItemSelect: onContextMenuItemSelect
231
+ });
232
+ }));
147
233
  }));
148
- };
149
- export { MainProperty };
234
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kep-platform/basic-component",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "A react library developed with dumi",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -74,10 +74,10 @@
74
74
  "react-dom": "^18.0.0",
75
75
  "stylelint": "^14.9.1"
76
76
  },
77
- "gitHead": "326ab1de4aaa605ef65582b87238e2d10a581600",
77
+ "gitHead": "fe3e26f7d0e44ccd78969f3acc6f417ce39dd32d",
78
78
  "dependencies": {
79
79
  "@ant-design/icons": "^5.3.7",
80
- "@kep-platform/hooks": "^0.0.13",
80
+ "@kep-platform/hooks": "^0.0.15",
81
81
  "color": "^4.2.3",
82
82
  "rc-pagination": "^4.1.0",
83
83
  "styled-components": "^6.1.11"