@kep-platform/basic-component 0.0.14 → 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
 
@@ -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需要一个触发元素!');
@@ -100,6 +100,9 @@ export default (function () {
100
100
  multiple: true,
101
101
  titleWidth: 400,
102
102
  contextMenuRender: contextMenuRender,
103
+ onContextMenuItemSelect: function onContextMenuItemSelect(node) {
104
+ console.log(node.key);
105
+ },
103
106
  treeData: treeData,
104
107
  columns: columns,
105
108
  selectedKeys: selectedKeys,
@@ -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
@@ -57,6 +57,7 @@ var Tree = function Tree(_ref) {
57
57
  _ref$onExpand = _ref.onExpand,
58
58
  onExpand = _ref$onExpand === void 0 ? function () {} : _ref$onExpand,
59
59
  contextMenuRender = _ref.contextMenuRender,
60
+ onContextMenuItemSelect = _ref.onContextMenuItemSelect,
60
61
  _ref$multiple = _ref.multiple,
61
62
  multiple = _ref$multiple === void 0 ? false : _ref$multiple,
62
63
  _ref$bordered = _ref.bordered,
@@ -166,6 +167,7 @@ var Tree = function Tree(_ref) {
166
167
  level: rootLevel,
167
168
  mainWidth: titleWidth,
168
169
  contextMenuRender: contextMenuRender,
170
+ onContextMenuItemSelect: onContextMenuItemSelect,
169
171
  titleRender: titleRender
170
172
  })));
171
173
  };
@@ -14,6 +14,7 @@ export type TreeNodeControllerProps = {
14
14
  expandedKeys: Key[];
15
15
  onExpand: onExpandHandler;
16
16
  contextMenuRender?: (node: TreeNodeType) => MenuItem[];
17
+ onContextMenuItemSelect?: (node: TreeNodeType, key: Key, item: MenuItem) => void;
17
18
  titleRender?: ((node: TreeNodeType) => ReactNode | null) | null | undefined;
18
19
  };
19
20
  export declare const nodeTypeMap: {
@@ -105,6 +105,7 @@ export var TreeNodeController = function TreeNodeController(_ref) {
105
105
  expandedKeys = _ref.expandedKeys,
106
106
  onExpand = _ref.onExpand,
107
107
  contextMenuRender = _ref.contextMenuRender,
108
+ onContextMenuItemSelect = _ref.onContextMenuItemSelect,
108
109
  titleRender = _ref.titleRender;
109
110
  /* 处理子节点缩进 */
110
111
  var childLevel = useMemo(function () {
@@ -192,10 +193,14 @@ export var TreeNodeController = function TreeNodeController(_ref) {
192
193
  return /*#__PURE__*/React.createElement(React.Fragment, null, node.nodeType !== nodeTypeMap.virtual && /*#__PURE__*/React.createElement(TreeNode, {
193
194
  selected: selectedKeys.includes(node[fieldNames.key]),
194
195
  width: width,
195
- onClick: onClickNodeHandler
196
+ onClick: onClickNodeHandler,
197
+ onContextMenu: onClickNodeHandler
196
198
  }, contextMenuRender ? /*#__PURE__*/React.createElement(Menu.Popup, {
197
199
  trigger: "contextmenu",
198
- items: contextMenuRender(node)
200
+ items: contextMenuRender(node),
201
+ onSelect: function onSelect(key, item) {
202
+ return onContextMenuItemSelect === null || onContextMenuItemSelect === void 0 ? void 0 : onContextMenuItemSelect(node, key, item);
203
+ }
199
204
  }, nodeEntity) : nodeEntity), expanded && treeNodeGroup && Object.keys(treeNodeGroup).map(function (groupKey) {
200
205
  return /*#__PURE__*/React.createElement(TreeNodeGroup, {
201
206
  width: width,
@@ -221,7 +226,8 @@ export var TreeNodeController = function TreeNodeController(_ref) {
221
226
  onExpand: onExpand,
222
227
  expandedKeys: expandedKeys,
223
228
  contextMenuRender: contextMenuRender,
224
- titleRender: titleRender
229
+ titleRender: titleRender,
230
+ onContextMenuItemSelect: onContextMenuItemSelect
225
231
  });
226
232
  }));
227
233
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kep-platform/basic-component",
3
- "version": "0.0.14",
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": "b346fc9c989221248d4882e8c3f7833e53750e92",
77
+ "gitHead": "fe3e26f7d0e44ccd78969f3acc6f417ce39dd32d",
78
78
  "dependencies": {
79
79
  "@ant-design/icons": "^5.3.7",
80
- "@kep-platform/hooks": "^0.0.14",
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"