rsuite 5.32.0 → 5.33.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.
@@ -9,8 +9,8 @@ import isNil from 'lodash/isNil';
9
9
  import isFunction from 'lodash/isFunction';
10
10
  import shallowEqual from '../utils/shallowEqual';
11
11
  import DropdownMenu from './DropdownMenu';
12
- import { findNodeOfTree, flattenTree } from '../utils/treeUtils';
13
- import { getParentMap, getPathTowardsItem, usePaths } from './utils';
12
+ import { getParentMap, getPathTowardsItem, findNodeOfTree, flattenTree } from '../utils/treeUtils';
13
+ import { usePaths } from './utils';
14
14
  import { getSafeRegExpString, createChainedFunction, mergeRefs, useControlled, useCustom, useClassNames, useIsMounted } from '../utils';
15
15
  import { PickerToggle, PickerOverlay, SearchBar, PickerToggleTrigger, usePickerClassName, usePublicMethods, useToggleKeyDownEvent, useFocusItemValue, pickTriggerPropKeys, omitTriggerPropKeys, listPickerPropTypes } from '../Picker';
16
16
  import { useMap } from '../utils/useMap';
@@ -46,13 +46,4 @@ export declare function usePaths<T extends Record<string, unknown>>({ data, acti
46
46
  pathTowardsSelectedItem: T[];
47
47
  pathTowardsActiveItem: T[];
48
48
  };
49
- /**
50
- * Returns a WeakMap that maps each item in `items` to its parent
51
- * indicated by `getChildren` function
52
- */
53
- export declare function getParentMap<T extends Record<string, unknown>>(items: readonly T[], getChildren: (item: T) => readonly T[] | undefined): WeakMap<T, T>;
54
- /**
55
- * Returns an array indicating the hirearchy path from root towards `target` item
56
- */
57
- export declare function getPathTowardsItem<T>(target: T | undefined, getParent: (item: T) => T | undefined): T[];
58
49
  export {};
@@ -1,10 +1,5 @@
1
- function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
2
-
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
-
5
- 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; }
6
-
7
1
  import { useMemo } from 'react';
2
+ import { getPathTowardsItem } from '../utils/treeUtils';
8
3
 
9
4
  /**
10
5
  * Calculate columns to be displayed:
@@ -85,42 +80,4 @@ export function usePaths(_ref) {
85
80
  pathTowardsSelectedItem: pathTowardsSelectedItem,
86
81
  pathTowardsActiveItem: pathTowardsActiveItem
87
82
  };
88
- }
89
- /**
90
- * Returns a WeakMap that maps each item in `items` to its parent
91
- * indicated by `getChildren` function
92
- */
93
-
94
- export function getParentMap(items, getChildren) {
95
- var map = new WeakMap();
96
-
97
- for (var queue = [].concat(items); queue.length > 0;) {
98
- var _item = queue.shift();
99
-
100
- var children = getChildren(_item);
101
-
102
- if (children) {
103
- for (var _iterator = _createForOfIteratorHelperLoose(children), _step; !(_step = _iterator()).done;) {
104
- var child = _step.value;
105
- map.set(child, _item);
106
- queue.push(child);
107
- }
108
- }
109
- }
110
-
111
- return map;
112
- }
113
- /**
114
- * Returns an array indicating the hirearchy path from root towards `target` item
115
- */
116
-
117
- export function getPathTowardsItem(target, getParent) {
118
- if (!target) return [];
119
- var path = [target];
120
-
121
- for (var parent = getParent(target); !!parent; parent = getParent(parent)) {
122
- path.unshift(parent);
123
- }
124
-
125
- return path;
126
83
  }
@@ -12,7 +12,7 @@ import classNames from 'classnames';
12
12
  import { List, AutoSizer } from '../Windowing';
13
13
  import CheckTreeNode from './CheckTreeNode';
14
14
  import TreeContext from '../Tree/TreeContext';
15
- import { getTreeNodeIndent } from '../utils/treeUtils';
15
+ import { getKeyParentMap, getPathTowardsItem, getTreeNodeIndent } from '../utils/treeUtils';
16
16
  import { createChainedFunction, useCustom, useClassNames, useControlled, KEY_VALUES, mergeRefs } from '../utils';
17
17
  import { PickerToggle, onMenuKeyDown as _onMenuKeyDown, PickerOverlay, SearchBar, SelectedElement, PickerToggleTrigger, createConcatChildrenFunction, usePickerClassName, usePublicMethods, pickTriggerPropKeys, omitTriggerPropKeys, listPickerPropTypes, useToggleKeyDownEvent } from '../Picker';
18
18
  import { isEveryChildChecked, isSomeNodeHasChildren, isAllSiblingNodeUncheckable, isEveryFirstLevelNodeUncheckable, getFormattedTree, getDisabledState, getCheckTreePickerDefaultValue, getSelectedItems, isNodeUncheckable } from './utils';
@@ -91,13 +91,14 @@ var CheckTreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
91
91
  onExited = props.onExited,
92
92
  onSearch = props.onSearch,
93
93
  onSelect = props.onSelect,
94
+ onSelectItem = props.onSelectItem,
94
95
  onOpen = props.onOpen,
95
96
  onScroll = props.onScroll,
96
97
  onExpand = props.onExpand,
97
98
  renderValue = props.renderValue,
98
99
  renderTreeIcon = props.renderTreeIcon,
99
100
  renderTreeNode = props.renderTreeNode,
100
- rest = _objectWithoutPropertiesLoose(props, ["as", "data", "style", "appearance", "cleanable", "countable", "searchBy", "toggleAs", "searchKeyword", "showIndentLine", "locale", "cascade", "disabled", "valueKey", "labelKey", "placement", "childrenKey", "placeholder", "value", "defaultValue", "defaultExpandAll", "disabledItemValues", "expandItemValues", "defaultExpandItemValues", "height", "menuMaxHeight", "menuStyle", "searchable", "virtualized", "className", "classPrefix", "menuClassName", "menuAutoWidth", "uncheckableItemValues", "id", "listProps", "renderMenu", "getChildren", "renderExtraFooter", "onEntered", "onChange", "onClean", "onClose", "onExited", "onSearch", "onSelect", "onOpen", "onScroll", "onExpand", "renderValue", "renderTreeIcon", "renderTreeNode"]);
101
+ rest = _objectWithoutPropertiesLoose(props, ["as", "data", "style", "appearance", "cleanable", "countable", "searchBy", "toggleAs", "searchKeyword", "showIndentLine", "locale", "cascade", "disabled", "valueKey", "labelKey", "placement", "childrenKey", "placeholder", "value", "defaultValue", "defaultExpandAll", "disabledItemValues", "expandItemValues", "defaultExpandItemValues", "height", "menuMaxHeight", "menuStyle", "searchable", "virtualized", "className", "classPrefix", "menuClassName", "menuAutoWidth", "uncheckableItemValues", "id", "listProps", "renderMenu", "getChildren", "renderExtraFooter", "onEntered", "onChange", "onClean", "onClose", "onExited", "onSearch", "onSelect", "onSelectItem", "onOpen", "onScroll", "onExpand", "renderValue", "renderTreeIcon", "renderTreeNode"]);
101
102
 
102
103
  var _useContext = useContext(TreeContext),
103
104
  inline = _useContext.inline;
@@ -343,7 +344,16 @@ var CheckTreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
343
344
  return values.filter(function (v) {
344
345
  return !uncheckableItemValues.includes(v);
345
346
  });
346
- }, [flattenNodes, uncheckableItemValues, serializeListOnlyParent, toggleDownChecked, toggleUpChecked]);
347
+ }, [flattenNodes, uncheckableItemValues, serializeListOnlyParent, toggleDownChecked, toggleUpChecked]); // TODO-Doma
348
+ // Replace `getKeyParentMap` with `getParentMap`
349
+
350
+ var itemParentMap = useMemo(function () {
351
+ return getKeyParentMap(data, function (node) {
352
+ return node[valueKey];
353
+ }, function (node) {
354
+ return node[childrenKey];
355
+ });
356
+ }, [childrenKey, data, valueKey]);
347
357
  var handleSelect = useCallback(function (node, event) {
348
358
  var currentNode = node.refKey ? flattenNodes[node.refKey] : null;
349
359
 
@@ -368,7 +378,10 @@ var CheckTreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
368
378
  setFocusItemValue(node[valueKey]);
369
379
  onChange === null || onChange === void 0 ? void 0 : onChange(selectedValues, event);
370
380
  onSelect === null || onSelect === void 0 ? void 0 : onSelect(node, selectedValues, event);
371
- }, [cascade, valueKey, flattenNodes, isControlled, uncheckableItemValues, setValue, onChange, onSelect, toggleChecked, unSerializeList]);
381
+ onSelectItem === null || onSelectItem === void 0 ? void 0 : onSelectItem(node, getPathTowardsItem(node, function (item) {
382
+ return itemParentMap.get(item[valueKey]);
383
+ }));
384
+ }, [flattenNodes, toggleChecked, isControlled, valueKey, onChange, onSelect, onSelectItem, unSerializeList, cascade, uncheckableItemValues, setValue, itemParentMap]);
372
385
  var handleOpen = useCallback(function () {
373
386
  var _triggerRef$current, _triggerRef$current$o;
374
387
 
@@ -60,6 +60,8 @@ export interface TreeBaseProps<ValueType = string | number, ItemDataType = Recor
60
60
  onExpand?: (expandItemValues: ItemDataType[], activeNode: ItemDataType, concat: (data: ItemDataType[], children: ItemDataType[]) => ItemDataType[]) => void;
61
61
  /** Callback function after selecting tree node */
62
62
  onSelect?: (activeNode: ItemDataType, value: ValueType, event: React.SyntheticEvent) => void;
63
+ /** Callback when a tree item is clicked */
64
+ onSelectItem?: (item: ItemDataType, path: ItemDataType[]) => void;
63
65
  /** Custom Render tree Node */
64
66
  renderTreeNode?: (nodeData: ItemDataType) => React.ReactNode;
65
67
  /** Custom Render icon */
@@ -5,11 +5,11 @@ import _isNil from "lodash/isNil";
5
5
  import _isUndefined from "lodash/isUndefined";
6
6
  import _omit from "lodash/omit";
7
7
  import _pick from "lodash/pick";
8
- import React, { useState, useRef, useEffect, useCallback, useContext } from 'react';
8
+ import React, { useState, useRef, useEffect, useCallback, useContext, useMemo } from 'react';
9
9
  import PropTypes from 'prop-types';
10
10
  import { List, AutoSizer } from '../Windowing';
11
11
  import TreeNode from './TreeNode';
12
- import { createDragPreview, getTreeNodeIndent, removeDragPreview, stringifyTreeNodeLabel } from '../utils/treeUtils';
12
+ import { createDragPreview, getKeyParentMap, getPathTowardsItem, getTreeNodeIndent, removeDragPreview, stringifyTreeNodeLabel } from '../utils/treeUtils';
13
13
  import { createChainedFunction, useClassNames, useCustom, useControlled, TREE_NODE_DROP_POSITION, KEY_VALUES, mergeRefs, shallowEqual } from '../utils';
14
14
  import { getExpandWhenSearching, hasVisibleChildren, getDragNodeKeys, calDropNodePosition, createUpdateTreeDataFunction, useTreeDrag, useFlattenTreeData, getTreeActiveNode, getDefaultExpandItemValues, useTreeNodeRefs, useTreeSearch, focusPreviousItem, getFocusableItems, focusNextItem, getActiveItem, toggleExpand, useGetTreeNodeChildren, focusToActiveTreeNode, leftArrowHandler, focusTreeNode, rightArrowHandler, isSearching } from '../utils/treeUtils';
15
15
  import { PickerToggle, PickerOverlay, SearchBar, PickerToggleTrigger, createConcatChildrenFunction, usePickerClassName, onMenuKeyDown as _onMenuKeyDown, usePublicMethods, listPickerPropTypes, pickTriggerPropKeys, omitTriggerPropKeys, useToggleKeyDownEvent } from '../Picker';
@@ -81,6 +81,7 @@ var TreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
81
81
  onOpen = props.onOpen,
82
82
  onSearch = props.onSearch,
83
83
  onSelect = props.onSelect,
84
+ onSelectItem = props.onSelectItem,
84
85
  onChange = props.onChange,
85
86
  onEntered = props.onEntered,
86
87
  onClose = props.onClose,
@@ -94,7 +95,7 @@ var TreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
94
95
  renderExtraFooter = props.renderExtraFooter,
95
96
  renderMenu = props.renderMenu,
96
97
  renderValue = props.renderValue,
97
- rest = _objectWithoutPropertiesLoose(props, ["as", "data", "appearance", "style", "showIndentLine", "value", "locale", "height", "menuMaxHeight", "menuStyle", "className", "disabled", "placement", "cleanable", "searchable", "virtualized", "classPrefix", "defaultValue", "placeholder", "searchKeyword", "menuClassName", "menuAutoWidth", "searchBy", "toggleAs", "labelKey", "valueKey", "childrenKey", "draggable", "defaultExpandAll", "disabledItemValues", "expandItemValues", "defaultExpandItemValues", "id", "listProps", "getChildren", "renderTreeIcon", "renderTreeNode", "onExit", "onExited", "onClean", "onOpen", "onSearch", "onSelect", "onChange", "onEntered", "onClose", "onDragEnd", "onDragStart", "onDragEnter", "onDragLeave", "onDragOver", "onDrop", "onExpand", "renderExtraFooter", "renderMenu", "renderValue"]);
98
+ rest = _objectWithoutPropertiesLoose(props, ["as", "data", "appearance", "style", "showIndentLine", "value", "locale", "height", "menuMaxHeight", "menuStyle", "className", "disabled", "placement", "cleanable", "searchable", "virtualized", "classPrefix", "defaultValue", "placeholder", "searchKeyword", "menuClassName", "menuAutoWidth", "searchBy", "toggleAs", "labelKey", "valueKey", "childrenKey", "draggable", "defaultExpandAll", "disabledItemValues", "expandItemValues", "defaultExpandItemValues", "id", "listProps", "getChildren", "renderTreeIcon", "renderTreeNode", "onExit", "onExited", "onClean", "onOpen", "onSearch", "onSelect", "onSelectItem", "onChange", "onEntered", "onClose", "onDragEnd", "onDragStart", "onDragEnter", "onDragLeave", "onDragOver", "onDrop", "onExpand", "renderExtraFooter", "renderMenu", "renderValue"]);
98
99
 
99
100
  var triggerRef = useRef(null);
100
101
  var targetRef = useRef(null);
@@ -291,8 +292,17 @@ var TreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
291
292
  onRenderTreeNode: renderTreeNode,
292
293
  onRenderTreeIcon: renderTreeIcon
293
294
  };
294
- };
295
+ }; // TODO-Doma
296
+ // Replace `getKeyParentMap` with `getParentMap`
297
+
295
298
 
299
+ var itemParentMap = useMemo(function () {
300
+ return getKeyParentMap(data, function (node) {
301
+ return node[valueKey];
302
+ }, function (node) {
303
+ return node[childrenKey];
304
+ });
305
+ }, [childrenKey, data, valueKey]);
296
306
  var handleSelect = useCallback(function (nodeData, event) {
297
307
  var _targetRef$current, _triggerRef$current, _triggerRef$current$c;
298
308
 
@@ -309,9 +319,12 @@ var TreePicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
309
319
  setFocusItemValue(nodeData[valueKey]);
310
320
  onChange === null || onChange === void 0 ? void 0 : onChange(nodeValue, event);
311
321
  onSelect === null || onSelect === void 0 ? void 0 : onSelect(nodeData, nodeValue, event);
322
+ onSelectItem === null || onSelectItem === void 0 ? void 0 : onSelectItem(nodeData, getPathTowardsItem(nodeData, function (item) {
323
+ return itemParentMap.get(item[valueKey]);
324
+ }));
312
325
  (_targetRef$current = targetRef.current) === null || _targetRef$current === void 0 ? void 0 : _targetRef$current.focus();
313
326
  (_triggerRef$current = triggerRef.current) === null || _triggerRef$current === void 0 ? void 0 : (_triggerRef$current$c = _triggerRef$current.close) === null || _triggerRef$current$c === void 0 ? void 0 : _triggerRef$current$c.call(_triggerRef$current);
314
- }, [valueKey, isControlled, onChange, onSelect, setValue]);
327
+ }, [valueKey, isControlled, onChange, onSelect, onSelectItem, setValue, itemParentMap]);
315
328
  var handleExpand = useCallback(function (node) {
316
329
  var nextExpandItemValues = toggleExpand({
317
330
  node: node,
@@ -278,3 +278,21 @@ export declare function createDragPreview(name: string, className: string): HTML
278
278
  */
279
279
  export declare function removeDragPreview(): void;
280
280
  export declare function stringifyTreeNodeLabel(label: string | React.ReactNode): string;
281
+ /**
282
+ * Returns a WeakMap that maps each item in `items` to its parent
283
+ * indicated by `getChildren` function
284
+ */
285
+ export declare function getParentMap<T extends Record<string, unknown>>(items: readonly T[], getChildren: (item: T) => readonly T[] | undefined): WeakMap<T, T>;
286
+ /**
287
+ * Returns a Map that maps each item's "key", indicated by `getKey` function,
288
+ * to its parent indicated by `getChildren` function
289
+ *
290
+ * NOTICE:
291
+ * Using this function is discouraged.
292
+ * Use {@link getParentMap} whenever possible.
293
+ */
294
+ export declare function getKeyParentMap<T extends Record<string, unknown>, K = React.Key>(items: readonly T[], getKey: (item: T) => K, getChildren: (item: T) => readonly T[] | undefined): Map<K, T>;
295
+ /**
296
+ * Returns an array indicating the hirearchy path from root towards `target` item
297
+ */
298
+ export declare function getPathTowardsItem<T>(target: T | undefined, getParent: (item: T) => T | undefined): T[];
@@ -6,6 +6,13 @@ import _isArray from "lodash/isArray";
6
6
  import _omit from "lodash/omit";
7
7
  import _isUndefined from "lodash/isUndefined";
8
8
  import _intersection from "lodash/intersection";
9
+
10
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+
12
+ 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); }
13
+
14
+ 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; }
15
+
9
16
  import React, { useRef, useState, useEffect, useCallback } from 'react';
10
17
  import shallowEqualArray from '../utils/shallowEqualArray';
11
18
  import { getNodeCheckState } from '../CheckTreePicker/utils';
@@ -1091,4 +1098,70 @@ export function stringifyTreeNodeLabel(label) {
1091
1098
  }
1092
1099
 
1093
1100
  return '';
1101
+ }
1102
+ /**
1103
+ * Returns a WeakMap that maps each item in `items` to its parent
1104
+ * indicated by `getChildren` function
1105
+ */
1106
+
1107
+ export function getParentMap(items, getChildren) {
1108
+ var map = new WeakMap();
1109
+
1110
+ for (var queue = [].concat(items); queue.length > 0;) {
1111
+ var _item5 = queue.shift();
1112
+
1113
+ var children = getChildren(_item5);
1114
+
1115
+ if (children) {
1116
+ for (var _iterator = _createForOfIteratorHelperLoose(children), _step; !(_step = _iterator()).done;) {
1117
+ var child = _step.value;
1118
+ map.set(child, _item5);
1119
+ queue.push(child);
1120
+ }
1121
+ }
1122
+ }
1123
+
1124
+ return map;
1125
+ }
1126
+ /**
1127
+ * Returns a Map that maps each item's "key", indicated by `getKey` function,
1128
+ * to its parent indicated by `getChildren` function
1129
+ *
1130
+ * NOTICE:
1131
+ * Using this function is discouraged.
1132
+ * Use {@link getParentMap} whenever possible.
1133
+ */
1134
+
1135
+ export function getKeyParentMap(items, getKey, getChildren) {
1136
+ var map = new Map();
1137
+
1138
+ for (var queue = [].concat(items); queue.length > 0;) {
1139
+ var _item6 = queue.shift();
1140
+
1141
+ var children = getChildren(_item6);
1142
+
1143
+ if (children) {
1144
+ for (var _iterator2 = _createForOfIteratorHelperLoose(children), _step2; !(_step2 = _iterator2()).done;) {
1145
+ var child = _step2.value;
1146
+ map.set(getKey(child), _item6);
1147
+ queue.push(child);
1148
+ }
1149
+ }
1150
+ }
1151
+
1152
+ return map;
1153
+ }
1154
+ /**
1155
+ * Returns an array indicating the hirearchy path from root towards `target` item
1156
+ */
1157
+
1158
+ export function getPathTowardsItem(target, getParent) {
1159
+ if (!target) return [];
1160
+ var path = [target];
1161
+
1162
+ for (var parent = getParent(target); !!parent; parent = getParent(parent)) {
1163
+ path.unshift(parent);
1164
+ }
1165
+
1166
+ return path;
1094
1167
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsuite",
3
- "version": "5.32.0",
3
+ "version": "5.33.0",
4
4
  "description": "A suite of react components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",