@snack-uikit/tree 0.12.4-preview-6a625dbd.0 → 0.12.4-preview-74429015.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.
@@ -25,18 +25,18 @@ const helperComponents_1 = require("../../helperComponents");
25
25
  const styles_module_scss_1 = __importDefault(require('./styles.module.css'));
26
26
  const utils_2 = require("./utils");
27
27
  function Tree(_a) {
28
- var {
29
- data,
30
- onNodeClick,
31
- onExpand,
32
- expandedNodes,
33
- nodeActions,
34
- parentActions,
35
- onDataLoad,
36
- showLines = true,
37
- showIcons = true,
38
- className
39
- } = _a,
28
+ var data = _a.data,
29
+ onNodeClick = _a.onNodeClick,
30
+ onExpand = _a.onExpand,
31
+ expandedNodes = _a.expandedNodes,
32
+ nodeActions = _a.nodeActions,
33
+ parentActions = _a.parentActions,
34
+ onDataLoad = _a.onDataLoad,
35
+ _a$showLines = _a.showLines,
36
+ showLines = _a$showLines === void 0 ? true : _a$showLines,
37
+ _a$showIcons = _a.showIcons,
38
+ showIcons = _a$showIcons === void 0 ? true : _a$showIcons,
39
+ className = _a.className,
40
40
  rest = __rest(_a, ["data", "onNodeClick", "onExpand", "expandedNodes", "nodeActions", "parentActions", "onDataLoad", "showLines", "showIcons", "className"]);
41
41
  return (0, jsx_runtime_1.jsx)("div", Object.assign({
42
42
  className: (0, classnames_1.default)(styles_module_scss_1.default.tree, className),
@@ -6,11 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.extractSelectableProps = extractSelectableProps;
7
7
  const constants_1 = require("../../constants");
8
8
  function extractSelectableProps(_ref) {
9
- let {
10
- selectionMode,
11
- selected,
12
- onSelect
13
- } = _ref;
9
+ let selectionMode = _ref.selectionMode,
10
+ selected = _ref.selected,
11
+ onSelect = _ref.onSelect;
14
12
  switch (selectionMode) {
15
13
  case constants_1.SELECTION_MODE.Single:
16
14
  return {
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/slicedToArray"));
3
5
  var __rest = void 0 && (void 0).__rest || function (s, e) {
4
6
  var t = {};
5
7
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
@@ -36,28 +38,30 @@ exports.TreeContext = (0, react_1.createContext)({
36
38
  isSelectable: false
37
39
  });
38
40
  function TreeContextProvider(_ref) {
39
- let {
40
- children,
41
- value
42
- } = _ref;
43
- const {
44
- onNodeClick: onNodeClickProp,
45
- onExpand: onExpandProp,
46
- onSelect: onSelectProp,
47
- selectionMode,
48
- data
49
- } = value,
41
+ let children = _ref.children,
42
+ value = _ref.value;
43
+ const onNodeClickProp = value.onNodeClick,
44
+ onExpandProp = value.onExpand,
45
+ onSelectProp = value.onSelect,
46
+ selectionMode = value.selectionMode,
47
+ data = value.data,
50
48
  props = __rest(value, ["onNodeClick", "onExpand", "onSelect", "selectionMode", "data"]);
51
49
  const isMultiSelect = selectionMode === constants_1.SELECTION_MODE.Multi;
52
50
  const isSingleSelect = selectionMode === constants_1.SELECTION_MODE.Single;
53
51
  const isSelectable = Boolean(selectionMode);
54
- const [expandedNodes, onExpandHandler] = (0, uncontrollable_1.useUncontrolledProp)(value.expandedNodes, value.expandedNodes || [], onExpandProp);
52
+ const _ref2 = (0, uncontrollable_1.useUncontrolledProp)(value.expandedNodes, value.expandedNodes || [], onExpandProp),
53
+ _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
54
+ expandedNodes = _ref3[0],
55
+ onExpandHandler = _ref3[1];
55
56
  const onExpand = (0, react_1.useCallback)(node => {
56
57
  const isExpanded = expandedNodes.includes(node.id);
57
58
  const nodes = isExpanded ? expandedNodes.filter(id => id !== node.id) : expandedNodes.concat(node.id);
58
59
  onExpandHandler(nodes, node);
59
60
  }, [expandedNodes, onExpandHandler]);
60
- const [selectedNodes, onSelectHandler] = (0, uncontrollable_1.useUncontrolledProp)(value.selected, [], onSelectProp);
61
+ const _ref4 = (0, uncontrollable_1.useUncontrolledProp)(value.selected, [], onSelectProp),
62
+ _ref5 = (0, _slicedToArray2.default)(_ref4, 2),
63
+ selectedNodes = _ref5[0],
64
+ onSelectHandler = _ref5[1];
61
65
  const onSelect = (0, react_1.useCallback)((node, parentNode) => {
62
66
  if (!isSelectable || node.disabled) return;
63
67
  if (isSingleSelect) {
@@ -87,7 +91,10 @@ function TreeContextProvider(_ref) {
87
91
  return acc;
88
92
  }, []);
89
93
  }, [data, expandedNodes]);
90
- const [focusIndex, setFocusIndex] = (0, react_1.useState)();
94
+ const _ref6 = (0, react_1.useState)(),
95
+ _ref7 = (0, _slicedToArray2.default)(_ref6, 2),
96
+ focusIndex = _ref7[0],
97
+ setFocusIndex = _ref7[1];
91
98
  const focusedNodeId = focusIndex !== undefined ? focusableNodeIds[focusIndex] : undefined;
92
99
  const setFocusPosition = (0, react_1.useCallback)(nodeId => {
93
100
  const newFocusIndex = focusableNodeIds.indexOf(nodeId);
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/slicedToArray"));
3
5
  var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
4
6
  function adopt(value) {
5
7
  return value instanceof P ? value : new P(function (resolve) {
@@ -55,40 +57,43 @@ const TreeNode_1 = require("../TreeNode");
55
57
  const styles_module_scss_1 = __importDefault(require('./styles.module.css'));
56
58
  exports.ExpandableTreeNode = (0, react_1.forwardRef)((_a, ref) => {
57
59
  var _b = _a.node,
58
- {
59
- 'data-test-id': dataTestId
60
- } = _b,
60
+ dataTestId = _b['data-test-id'],
61
61
  node = __rest(_b, ['data-test-id']),
62
- {
63
- parentNode,
64
- tabIndexAvailable
65
- } = _a;
66
- const {
67
- expandedNodes,
68
- onExpand,
69
- onDataLoad,
70
- showLines
71
- } = (0, TreeContext_1.useTreeContext)();
62
+ parentNode = _a.parentNode,
63
+ tabIndexAvailable = _a.tabIndexAvailable;
64
+ const _ref = (0, TreeContext_1.useTreeContext)(),
65
+ expandedNodes = _ref.expandedNodes,
66
+ onExpand = _ref.onExpand,
67
+ onDataLoad = _ref.onDataLoad,
68
+ showLines = _ref.showLines;
72
69
  const isExpandable = Boolean(node.nested);
73
70
  const isExpanded = (expandedNodes === null || expandedNodes === void 0 ? void 0 : expandedNodes.includes(node.id)) || false;
74
- const [isLoading, setLoading] = (0, react_1.useState)(false);
75
- const [state, toggle] = (0, react_transition_state_1.default)({
76
- timeout: constants_1.TRANSITION_TIMING.accordionFolding,
77
- initialEntered: isExpanded || undefined,
78
- enter: isExpanded
79
- });
71
+ const _ref2 = (0, react_1.useState)(false),
72
+ _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
73
+ isLoading = _ref3[0],
74
+ setLoading = _ref3[1];
75
+ const _ref4 = (0, react_transition_state_1.default)({
76
+ timeout: constants_1.TRANSITION_TIMING.accordionFolding,
77
+ initialEntered: isExpanded || undefined,
78
+ enter: isExpanded
79
+ }),
80
+ _ref5 = (0, _slicedToArray2.default)(_ref4, 2),
81
+ state = _ref5[0],
82
+ toggle = _ref5[1];
80
83
  const showContent = node.nested && node.nested.length > 0 && state.status !== 'exited';
81
84
  const childrenRefs = (0, react_1.useRef)([]);
82
- const [lastItemHeight, setLastItemHeight] = (0, react_1.useState)(0);
85
+ const _ref6 = (0, react_1.useState)(0),
86
+ _ref7 = (0, _slicedToArray2.default)(_ref6, 2),
87
+ lastItemHeight = _ref7[0],
88
+ setLastItemHeight = _ref7[1];
83
89
  (0, react_1.useEffect)(() => {
84
90
  var _a, _b;
85
91
  const lastItem = (_a = childrenRefs.current) === null || _a === void 0 ? void 0 : _a.at(-1);
86
92
  if (lastItem) {
87
93
  const observer = new ResizeObserver(entities => entities.forEach(entity => {
88
94
  if (entity.target === lastItem) {
89
- const [{
90
- blockSize: newHeight
91
- }] = entity.contentBoxSize;
95
+ const _entity$contentBoxSiz = (0, _slicedToArray2.default)(entity.contentBoxSize, 1),
96
+ newHeight = _entity$contentBoxSiz[0].blockSize;
92
97
  setLastItemHeight(newHeight);
93
98
  }
94
99
  }));
@@ -156,7 +161,7 @@ exports.ExpandableTreeNode = (0, react_1.forwardRef)((_a, ref) => {
156
161
  children: [(0, jsx_runtime_1.jsx)(TreeLine_1.TreeLine, {
157
162
  visible: showLines,
158
163
  "data-test-id": constants_1.TEST_IDS.line,
159
- height: `calc(100% - ${lastItemHeight}px)`,
164
+ height: "calc(100% - ".concat(lastItemHeight, "px)"),
160
165
  className: styles_module_scss_1.default.treeLine
161
166
  }), (0, jsx_runtime_1.jsx)("div", {
162
167
  className: styles_module_scss_1.default.expandableNested,
@@ -10,11 +10,9 @@ const constants_1 = require("../../constants");
10
10
  const ExpandableTreeNode_1 = require("../ExpandableTreeNode");
11
11
  const TreeNode_1 = require("../TreeNode");
12
12
  exports.TreeItem = (0, react_1.forwardRef)((_ref, ref) => {
13
- let {
14
- node,
15
- tabIndexAvailable,
16
- parentNode
17
- } = _ref;
13
+ let node = _ref.node,
14
+ tabIndexAvailable = _ref.tabIndexAvailable,
15
+ parentNode = _ref.parentNode;
18
16
  const dataTestId = node['data-test-id'] || constants_1.TEST_IDS.node;
19
17
  const nodeProps = Object.assign(Object.assign({}, node), {
20
18
  'data-test-id': dataTestId
@@ -14,14 +14,12 @@ const classnames_1 = __importDefault(require("classnames"));
14
14
  const constants_1 = require("../../constants");
15
15
  const styles_module_scss_1 = __importDefault(require('./styles.module.css'));
16
16
  function TreeLine(_ref) {
17
- let {
18
- className,
19
- halfWidth,
20
- horizontal,
21
- visible,
22
- height,
23
- width
24
- } = _ref;
17
+ let className = _ref.className,
18
+ halfWidth = _ref.halfWidth,
19
+ horizontal = _ref.horizontal,
20
+ visible = _ref.visible,
21
+ height = _ref.height,
22
+ width = _ref.width;
25
23
  return (0, jsx_runtime_1.jsx)("div", {
26
24
  className: (0, classnames_1.default)(styles_module_scss_1.default.treeLine, className),
27
25
  style: {
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/slicedToArray"));
3
5
  var __rest = void 0 && (void 0).__rest || function (s, e) {
4
6
  var t = {};
5
7
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
@@ -34,50 +36,56 @@ const components_1 = require("./components");
34
36
  const styles_module_scss_1 = __importDefault(require('./styles.module.css'));
35
37
  const utils_2 = require("./utils");
36
38
  exports.TreeNode = (0, react_1.forwardRef)((_a, ref) => {
37
- var {
38
- id,
39
- title,
40
- icon = (0, jsx_runtime_1.jsx)(icons_1.FileSVG, {
41
- size: 24
42
- }),
43
- expandedIcon = (0, jsx_runtime_1.jsx)(icons_1.FolderOpenSVG, {
44
- size: 24
45
- }),
46
- collapsedIcon = (0, jsx_runtime_1.jsx)(icons_1.FolderSVG, {
47
- size: 24
48
- }),
49
- disabled,
50
- onClick,
51
- nested,
52
- className,
53
- onChevronClick,
54
- onKeyDown,
55
- isLoading,
56
- parentNode,
57
- tabIndexAvailable,
58
- href
59
- } = _a,
39
+ var id = _a.id,
40
+ title = _a.title,
41
+ _a$icon = _a.icon,
42
+ icon = _a$icon === void 0 ? (0, jsx_runtime_1.jsx)(icons_1.FileSVG, {
43
+ size: 24
44
+ }) : _a$icon,
45
+ _a$expandedIcon = _a.expandedIcon,
46
+ expandedIcon = _a$expandedIcon === void 0 ? (0, jsx_runtime_1.jsx)(icons_1.FolderOpenSVG, {
47
+ size: 24
48
+ }) : _a$expandedIcon,
49
+ _a$collapsedIcon = _a.collapsedIcon,
50
+ collapsedIcon = _a$collapsedIcon === void 0 ? (0, jsx_runtime_1.jsx)(icons_1.FolderSVG, {
51
+ size: 24
52
+ }) : _a$collapsedIcon,
53
+ disabled = _a.disabled,
54
+ onClick = _a.onClick,
55
+ nested = _a.nested,
56
+ className = _a.className,
57
+ onChevronClick = _a.onChevronClick,
58
+ onKeyDown = _a.onKeyDown,
59
+ isLoading = _a.isLoading,
60
+ parentNode = _a.parentNode,
61
+ tabIndexAvailable = _a.tabIndexAvailable,
62
+ href = _a.href,
60
63
  rest = __rest(_a, ["id", "title", "icon", "expandedIcon", "collapsedIcon", "disabled", "onClick", "nested", "className", "onChevronClick", "onKeyDown", "isLoading", "parentNode", "tabIndexAvailable", "href"]);
61
- const {
62
- isMultiSelect,
63
- isSelectable,
64
- onNodeClick,
65
- selected,
66
- expandedNodes,
67
- onSelect,
68
- nodeActions,
69
- parentActions,
70
- setFocusPosition,
71
- resetFocusPosition,
72
- focusedNodeId,
73
- setFocusIndex,
74
- focusableNodeIds,
75
- showToggle,
76
- showLines,
77
- showIcons
78
- } = (0, TreeContext_1.useTreeContext)();
79
- const [isDroplistOpen, setDroplistOpen] = (0, react_1.useState)(false);
80
- const [isDroplistTriggerFocused, setFocusDroplistTrigger] = (0, react_1.useState)(false);
64
+ const _ref = (0, TreeContext_1.useTreeContext)(),
65
+ isMultiSelect = _ref.isMultiSelect,
66
+ isSelectable = _ref.isSelectable,
67
+ onNodeClick = _ref.onNodeClick,
68
+ selected = _ref.selected,
69
+ expandedNodes = _ref.expandedNodes,
70
+ onSelect = _ref.onSelect,
71
+ nodeActions = _ref.nodeActions,
72
+ parentActions = _ref.parentActions,
73
+ setFocusPosition = _ref.setFocusPosition,
74
+ resetFocusPosition = _ref.resetFocusPosition,
75
+ focusedNodeId = _ref.focusedNodeId,
76
+ setFocusIndex = _ref.setFocusIndex,
77
+ focusableNodeIds = _ref.focusableNodeIds,
78
+ showToggle = _ref.showToggle,
79
+ showLines = _ref.showLines,
80
+ showIcons = _ref.showIcons;
81
+ const _ref2 = (0, react_1.useState)(false),
82
+ _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
83
+ isDroplistOpen = _ref3[0],
84
+ setDroplistOpen = _ref3[1];
85
+ const _ref4 = (0, react_1.useState)(false),
86
+ _ref5 = (0, _slicedToArray2.default)(_ref4, 2),
87
+ isDroplistTriggerFocused = _ref5[0],
88
+ setFocusDroplistTrigger = _ref5[1];
81
89
  const contentRef = (0, react_1.useRef)(null);
82
90
  const anchorRef = (0, react_1.useRef)(null);
83
91
  const isExpandable = Array.isArray(nested);
@@ -18,15 +18,13 @@ const constants_1 = require("../../../constants");
18
18
  const styles_module_scss_1 = __importDefault(require('../styles.module.css'));
19
19
  const utils_1 = require("../utils");
20
20
  function TreeNodeActions(_ref) {
21
- let {
22
- getNodeActions,
23
- isDroplistTriggerFocused,
24
- focusNode,
25
- isDroplistOpen,
26
- setDroplistOpen,
27
- onBlurActions,
28
- node
29
- } = _ref;
21
+ let getNodeActions = _ref.getNodeActions,
22
+ isDroplistTriggerFocused = _ref.isDroplistTriggerFocused,
23
+ focusNode = _ref.focusNode,
24
+ isDroplistOpen = _ref.isDroplistOpen,
25
+ setDroplistOpen = _ref.setDroplistOpen,
26
+ onBlurActions = _ref.onBlurActions,
27
+ node = _ref.node;
30
28
  const droplistActions = getNodeActions(node);
31
29
  const localRef = (0, react_1.useRef)(null);
32
30
  (0, react_1.useEffect)(() => {
@@ -14,11 +14,9 @@ const react_1 = require("react");
14
14
  const constants_1 = require("../../../constants");
15
15
  const styles_module_scss_1 = __importDefault(require('../styles.module.css'));
16
16
  exports.TreeNodeHref = (0, react_1.forwardRef)((_ref, ref) => {
17
- let {
18
- href,
19
- children,
20
- onClick
21
- } = _ref;
17
+ let href = _ref.href,
18
+ children = _ref.children,
19
+ onClick = _ref.onClick;
22
20
  return href ? (0, jsx_runtime_1.jsx)("a", {
23
21
  href: href,
24
22
  className: styles_module_scss_1.default.treeNodeLink,
@@ -9,10 +9,8 @@ exports.extractTreeNodeTitle = void 0;
9
9
  * Если title задан функцией, использует getTitle (если он доступен).
10
10
  */
11
11
  const extractTreeNodeTitle = _ref => {
12
- let {
13
- title,
14
- getTitle
15
- } = _ref;
12
+ let title = _ref.title,
13
+ getTitle = _ref.getTitle;
16
14
  var _a;
17
15
  return typeof title === 'string' ? title : (_a = getTitle === null || getTitle === void 0 ? void 0 : getTitle()) !== null && _a !== void 0 ? _a : '';
18
16
  };
@@ -19,15 +19,12 @@ const isMatchedTreeItem = search => {
19
19
  * @returns Новое дерево, содержащее только совпавшие узлы и их путь в иерархии.
20
20
  */
21
21
  const getSearchedTreeItems = _ref => {
22
- let {
23
- tree,
24
- searchOptions
25
- } = _ref;
22
+ let tree = _ref.tree,
23
+ searchOptions = _ref.searchOptions;
26
24
  if (!(searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.query)) return tree;
27
- const {
28
- query = '',
29
- includeChildrenMatchedParent
30
- } = searchOptions;
25
+ const _searchOptions$query = searchOptions.query,
26
+ query = _searchOptions$query === void 0 ? '' : _searchOptions$query,
27
+ includeChildrenMatchedParent = searchOptions.includeChildrenMatchedParent;
31
28
  const matchFunc = isMatchedTreeItem(query);
32
29
  const searchItems = treeItems => treeItems.reduce((acc, item) => {
33
30
  const hasMatchingTitle = matchFunc(item);
@@ -13,13 +13,11 @@ exports.getSearchedTreeNodeById = void 0;
13
13
  * @returns Для одиночного id возвращает узел или null, для массива id — список узлов.
14
14
  */
15
15
  const getSearchedTreeNodeById = _ref => {
16
- let {
17
- tree,
18
- searchOptions: {
19
- id,
20
- includeNested = true
21
- }
22
- } = _ref;
16
+ let tree = _ref.tree,
17
+ _ref$searchOptions = _ref.searchOptions,
18
+ id = _ref$searchOptions.id,
19
+ _ref$searchOptions$in = _ref$searchOptions.includeNested,
20
+ includeNested = _ref$searchOptions$in === void 0 ? true : _ref$searchOptions$in;
23
21
  const isArrayIds = Array.isArray(id);
24
22
  if (!id || isArrayIds && id.length === 0) {
25
23
  return isArrayIds ? [] : null;
@@ -14,15 +14,11 @@ const findAllChildNodeIds_1 = require("./findAllChildNodeIds");
14
14
  * @returns Обновленный список выбранных id.
15
15
  */
16
16
  function lookupTreeForSelectedNodes(_ref) {
17
- let {
18
- node,
19
- selectedNodes,
20
- parentNode
21
- } = _ref;
17
+ let node = _ref.node,
18
+ selectedNodes = _ref.selectedNodes,
19
+ parentNode = _ref.parentNode;
22
20
  var _a;
23
- const {
24
- nested
25
- } = node;
21
+ const nested = node.nested;
26
22
  const childSelection = (nested === null || nested === void 0 ? void 0 : nested.length) ? (0, checkNestedNodesSelection_1.checkNestedNodesSelection)(nested, selectedNodes) : undefined;
27
23
  const isSelected = childSelection ? childSelection.someSelected || childSelection.allSelected : selectedNodes.includes(node.id);
28
24
  let updatedSelectedNodes = [];
@@ -30,10 +30,8 @@ const traverse = (nodes, callback) => {
30
30
  if (!nodeWithDepth) {
31
31
  continue;
32
32
  }
33
- const {
34
- node,
35
- depth
36
- } = nodeWithDepth;
33
+ const node = nodeWithDepth.node,
34
+ depth = nodeWithDepth.depth;
37
35
  callback(node, depth);
38
36
  for (const child of (_a = node.nested) !== null && _a !== void 0 ? _a : []) {
39
37
  queue.enqueue({
@@ -32,11 +32,9 @@ const traverseWithTarget = (nodes, rootTargetList, callback) => {
32
32
  while (!queue.isEmpty()) {
33
33
  const item = queue.dequeue();
34
34
  if (!item) continue;
35
- const {
36
- node,
37
- depth,
38
- targetList
39
- } = item;
35
+ const node = item.node,
36
+ depth = item.depth,
37
+ targetList = item.targetList;
40
38
  const childTargetList = callback(node, depth, targetList);
41
39
  if (childTargetList !== undefined && ((_a = node.nested) === null || _a === void 0 ? void 0 : _a.length)) {
42
40
  for (const child of node.nested) {
@@ -63,15 +63,14 @@ const mapNodeToRecordItem = node => ({
63
63
  (0, vitest_1.describe)('useSearchableTree', () => {
64
64
  (0, vitest_1.it)('should initialize tree and treeItemsRecord from initTree', () => {
65
65
  const initTree = [createLeaf('node-1'), createParent('node-2', [createLeaf('node-2-1')])];
66
- const {
67
- result
68
- } = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
69
- initTree,
70
- onPreloadNode: vitest_1.vi.fn(),
71
- onPreloadNodes: vitest_1.vi.fn(),
72
- onSearch: vitest_1.vi.fn(),
73
- mapNodeToRecordItem
74
- }));
66
+ const _ref = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
67
+ initTree,
68
+ onPreloadNode: vitest_1.vi.fn(),
69
+ onPreloadNodes: vitest_1.vi.fn(),
70
+ onSearch: vitest_1.vi.fn(),
71
+ mapNodeToRecordItem
72
+ })),
73
+ result = _ref.result;
75
74
  (0, vitest_1.expect)(result.current.tree.current).toEqual(initTree);
76
75
  (0, vitest_1.expect)(result.current.treeItemsRecord.current).toEqual({
77
76
  'node-1': {
@@ -86,15 +85,14 @@ const mapNodeToRecordItem = node => ({
86
85
  });
87
86
  });
88
87
  (0, vitest_1.it)('should update expandedNodes on onExpand call', () => {
89
- const {
90
- result
91
- } = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
92
- initTree: [createLeaf('root')],
93
- onPreloadNode: vitest_1.vi.fn(),
94
- onPreloadNodes: vitest_1.vi.fn(),
95
- onSearch: vitest_1.vi.fn(),
96
- mapNodeToRecordItem
97
- }));
88
+ const _ref2 = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
89
+ initTree: [createLeaf('root')],
90
+ onPreloadNode: vitest_1.vi.fn(),
91
+ onPreloadNodes: vitest_1.vi.fn(),
92
+ onSearch: vitest_1.vi.fn(),
93
+ mapNodeToRecordItem
94
+ })),
95
+ result = _ref2.result;
98
96
  (0, react_1.act)(() => {
99
97
  result.current.onExpand(['root', 'child']);
100
98
  });
@@ -104,15 +102,14 @@ const mapNodeToRecordItem = node => ({
104
102
  const initTree = [createParent('root', [])];
105
103
  const preloadedChildren = [createLeaf('child-1'), createLeaf('child-2')];
106
104
  const onPreloadNode = vitest_1.vi.fn().mockResolvedValue(preloadedChildren);
107
- const {
108
- result
109
- } = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
110
- initTree,
111
- onPreloadNode,
112
- onPreloadNodes: vitest_1.vi.fn(),
113
- onSearch: vitest_1.vi.fn(),
114
- mapNodeToRecordItem
115
- }));
105
+ const _ref3 = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
106
+ initTree,
107
+ onPreloadNode,
108
+ onPreloadNodes: vitest_1.vi.fn(),
109
+ onSearch: vitest_1.vi.fn(),
110
+ mapNodeToRecordItem
111
+ })),
112
+ result = _ref3.result;
116
113
  let loadResult;
117
114
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
118
115
  loadResult = yield result.current.onDataLoad(createParent('root', []));
@@ -160,15 +157,14 @@ const mapNodeToRecordItem = node => ({
160
157
  'external-root': [createLeaf('external-child')]
161
158
  }, Object.fromEntries(nodeIds.filter(id => id !== 'expandable-root' && id !== 'external-root').map(id => [id, []])));
162
159
  }));
163
- const {
164
- result
165
- } = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
166
- initTree: [createParent('init-root', [])],
167
- onPreloadNode: vitest_1.vi.fn(),
168
- onPreloadNodes,
169
- onSearch,
170
- mapNodeToRecordItem
171
- }));
160
+ const _ref4 = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
161
+ initTree: [createParent('init-root', [])],
162
+ onPreloadNode: vitest_1.vi.fn(),
163
+ onPreloadNodes,
164
+ onSearch,
165
+ mapNodeToRecordItem
166
+ })),
167
+ result = _ref4.result;
172
168
  (0, react_1.act)(() => {
173
169
  result.current.onExpand(['expandable-root']);
174
170
  result.current.search.onChange('query');
@@ -200,15 +196,14 @@ const mapNodeToRecordItem = node => ({
200
196
  needPreloadNodes: []
201
197
  };
202
198
  }));
203
- const {
204
- result
205
- } = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
206
- initTree: [createLeaf('initial')],
207
- onPreloadNode: vitest_1.vi.fn(),
208
- onPreloadNodes: vitest_1.vi.fn(),
209
- onSearch,
210
- mapNodeToRecordItem
211
- }));
199
+ const _ref5 = (0, react_1.renderHook)(() => (0, useSearchableTree_1.useSearchableTree)({
200
+ initTree: [createLeaf('initial')],
201
+ onPreloadNode: vitest_1.vi.fn(),
202
+ onPreloadNodes: vitest_1.vi.fn(),
203
+ onSearch,
204
+ mapNodeToRecordItem
205
+ })),
206
+ result = _ref5.result;
212
207
  (0, react_1.act)(() => {
213
208
  result.current.search.onChange('query');
214
209
  });
@@ -44,20 +44,19 @@ const createParent = (id, nested) => ({
44
44
  });
45
45
  (0, vitest_1.describe)('useTreeMultiSelection', () => {
46
46
  (0, vitest_1.it)('should keep selected state uncontrolled by default', () => __awaiter(void 0, void 0, void 0, function* () {
47
- const {
48
- result
49
- } = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
50
- onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
51
- return {
52
- preloadedChildren: [],
53
- updatedTree: []
54
- };
47
+ const _ref = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
48
+ onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
49
+ return {
50
+ preloadedChildren: [],
51
+ updatedTree: []
52
+ };
53
+ })),
54
+ onSelect: () => ({
55
+ added: ['a'],
56
+ removed: []
57
+ })
55
58
  })),
56
- onSelect: () => ({
57
- added: ['a'],
58
- removed: []
59
- })
60
- }));
59
+ result = _ref.result;
61
60
  (0, vitest_1.expect)(result.current.selected).toEqual([]);
62
61
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
63
62
  yield result.current.onSelect(['a'], createLeaf('a'));
@@ -66,36 +65,7 @@ const createParent = (id, nested) => ({
66
65
  }));
67
66
  (0, vitest_1.it)('should call onChangeSelected when selection updates', () => __awaiter(void 0, void 0, void 0, function* () {
68
67
  const onChangeSelected = vitest_1.vi.fn();
69
- const {
70
- result
71
- } = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
72
- onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
73
- return {
74
- preloadedChildren: [],
75
- updatedTree: []
76
- };
77
- })),
78
- onSelect: () => ({
79
- added: ['a'],
80
- removed: []
81
- }),
82
- onChangeSelected
83
- }));
84
- yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
85
- yield result.current.onSelect(['a'], createLeaf('a'));
86
- }));
87
- (0, vitest_1.expect)(onChangeSelected).toHaveBeenCalledWith(['a']);
88
- }));
89
- (0, vitest_1.it)('should treat selected prop as controlled', () => __awaiter(void 0, void 0, void 0, function* () {
90
- const onChangeSelected = vitest_1.vi.fn();
91
- const {
92
- result,
93
- rerender
94
- } = (0, react_1.renderHook)(_ref => {
95
- let {
96
- selected
97
- } = _ref;
98
- return (0, useTreeMultiSelection_1.useTreeMultiSelection)({
68
+ const _ref2 = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
99
69
  onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
100
70
  return {
101
71
  preloadedChildren: [],
@@ -103,17 +73,42 @@ const createParent = (id, nested) => ({
103
73
  };
104
74
  })),
105
75
  onSelect: () => ({
106
- added: ['b'],
76
+ added: ['a'],
107
77
  removed: []
108
78
  }),
109
- selected,
110
79
  onChangeSelected
111
- });
112
- }, {
113
- initialProps: {
114
- selected: ['a']
115
- }
116
- });
80
+ })),
81
+ result = _ref2.result;
82
+ yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
83
+ yield result.current.onSelect(['a'], createLeaf('a'));
84
+ }));
85
+ (0, vitest_1.expect)(onChangeSelected).toHaveBeenCalledWith(['a']);
86
+ }));
87
+ (0, vitest_1.it)('should treat selected prop as controlled', () => __awaiter(void 0, void 0, void 0, function* () {
88
+ const onChangeSelected = vitest_1.vi.fn();
89
+ const _ref3 = (0, react_1.renderHook)(_ref4 => {
90
+ let selected = _ref4.selected;
91
+ return (0, useTreeMultiSelection_1.useTreeMultiSelection)({
92
+ onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
93
+ return {
94
+ preloadedChildren: [],
95
+ updatedTree: []
96
+ };
97
+ })),
98
+ onSelect: () => ({
99
+ added: ['b'],
100
+ removed: []
101
+ }),
102
+ selected,
103
+ onChangeSelected
104
+ });
105
+ }, {
106
+ initialProps: {
107
+ selected: ['a']
108
+ }
109
+ }),
110
+ result = _ref3.result,
111
+ rerender = _ref3.rerender;
117
112
  (0, vitest_1.expect)(result.current.selected).toEqual(['a']);
118
113
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
119
114
  yield result.current.onSelect(['a', 'b'], createLeaf('b'));
@@ -139,12 +134,11 @@ const createParent = (id, nested) => ({
139
134
  added: ['parent', 'child-1', 'child-2'],
140
135
  removed: []
141
136
  }));
142
- const {
143
- result
144
- } = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
145
- onDataLoad,
146
- onSelect
147
- }));
137
+ const _ref5 = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
138
+ onDataLoad,
139
+ onSelect
140
+ })),
141
+ result = _ref5.result;
148
142
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
149
143
  yield result.current.onSelect(['parent'], node);
150
144
  }));
@@ -168,12 +162,11 @@ const createParent = (id, nested) => ({
168
162
  added: ['parent'],
169
163
  removed: []
170
164
  }));
171
- const {
172
- result
173
- } = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
174
- onDataLoad,
175
- onSelect
176
- }));
165
+ const _ref6 = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
166
+ onDataLoad,
167
+ onSelect
168
+ })),
169
+ result = _ref6.result;
177
170
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
178
171
  yield result.current.onSelect(['parent'], node);
179
172
  }));
@@ -186,20 +179,19 @@ const createParent = (id, nested) => ({
186
179
  (0, vitest_1.expect)(result.current.selected).toEqual(['parent']);
187
180
  }));
188
181
  (0, vitest_1.it)('should deduplicate ids when merging added into selection', () => __awaiter(void 0, void 0, void 0, function* () {
189
- const {
190
- result
191
- } = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
192
- onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
193
- return {
194
- preloadedChildren: [],
195
- updatedTree: []
196
- };
182
+ const _ref7 = (0, react_1.renderHook)(() => (0, useTreeMultiSelection_1.useTreeMultiSelection)({
183
+ onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
184
+ return {
185
+ preloadedChildren: [],
186
+ updatedTree: []
187
+ };
188
+ })),
189
+ onSelect: () => ({
190
+ added: ['a', 'a', 'b'],
191
+ removed: []
192
+ })
197
193
  })),
198
- onSelect: () => ({
199
- added: ['a', 'a', 'b'],
200
- removed: []
201
- })
202
- }));
194
+ result = _ref7.result;
203
195
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
204
196
  yield result.current.onSelect(['a'], createLeaf('a'));
205
197
  }));
@@ -207,32 +199,29 @@ const createParent = (id, nested) => ({
207
199
  }));
208
200
  (0, vitest_1.it)('should apply removed ids from selection', () => __awaiter(void 0, void 0, void 0, function* () {
209
201
  const onChangeSelected = vitest_1.vi.fn();
210
- const {
211
- result,
212
- rerender
213
- } = (0, react_1.renderHook)(_ref2 => {
214
- let {
215
- selected
216
- } = _ref2;
217
- return (0, useTreeMultiSelection_1.useTreeMultiSelection)({
218
- onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
219
- return {
220
- preloadedChildren: [],
221
- updatedTree: []
222
- };
223
- })),
224
- onSelect: () => ({
225
- added: [],
226
- removed: ['a']
227
- }),
228
- selected,
229
- onChangeSelected
230
- });
231
- }, {
232
- initialProps: {
233
- selected: ['a', 'b']
234
- }
235
- });
202
+ const _ref8 = (0, react_1.renderHook)(_ref9 => {
203
+ let selected = _ref9.selected;
204
+ return (0, useTreeMultiSelection_1.useTreeMultiSelection)({
205
+ onDataLoad: vitest_1.vi.fn(() => __awaiter(void 0, void 0, void 0, function* () {
206
+ return {
207
+ preloadedChildren: [],
208
+ updatedTree: []
209
+ };
210
+ })),
211
+ onSelect: () => ({
212
+ added: [],
213
+ removed: ['a']
214
+ }),
215
+ selected,
216
+ onChangeSelected
217
+ });
218
+ }, {
219
+ initialProps: {
220
+ selected: ['a', 'b']
221
+ }
222
+ }),
223
+ result = _ref8.result,
224
+ rerender = _ref8.rerender;
236
225
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
237
226
  yield result.current.onSelect(['b'], createLeaf('a'));
238
227
  }));
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/slicedToArray"));
3
5
  var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
4
6
  function adopt(value) {
5
7
  return value instanceof P ? value : new P(function (resolve) {
@@ -48,19 +50,23 @@ const helpers_1 = require("../helpers");
48
50
  * @returns Ссылки на состояние дерева, контролы поиска и колбэки для `expand`/`dataLoad`.
49
51
  */
50
52
  function useSearchableTree(_ref) {
51
- let {
52
- initTree,
53
- onPreloadNode,
54
- onPreloadNodes,
55
- onSearch,
56
- mapNodeToRecordItem
57
- } = _ref;
53
+ let initTree = _ref.initTree,
54
+ onPreloadNode = _ref.onPreloadNode,
55
+ onPreloadNodes = _ref.onPreloadNodes,
56
+ onSearch = _ref.onSearch,
57
+ mapNodeToRecordItem = _ref.mapNodeToRecordItem;
58
58
  const tree = (0, reactuse_1.useRefState)(initTree);
59
59
  const treeItemsRecord = (0, reactuse_1.useRefState)({});
60
60
  const expandedNodes = (0, reactuse_1.useRefState)([]);
61
- const [search, setSearch] = (0, react_1.useState)('');
61
+ const _ref2 = (0, react_1.useState)(''),
62
+ _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
63
+ search = _ref3[0],
64
+ setSearch = _ref3[1];
62
65
  const debouncedSearch = (0, reactuse_1.useDebounceValue)(search, 500);
63
- const [loading, setLoading] = (0, react_1.useState)(false);
66
+ const _ref4 = (0, react_1.useState)(false),
67
+ _ref5 = (0, _slicedToArray2.default)(_ref4, 2),
68
+ loading = _ref5[0],
69
+ setLoading = _ref5[1];
64
70
  const searchPromiseRef = (0, react_1.useRef)(null);
65
71
  const searchAbortControllerRef = (0, react_1.useRef)(null);
66
72
  const buildTreeItemsRecord = (0, react_1.useCallback)(nodes => {
@@ -103,10 +109,9 @@ function useSearchableTree(_ref) {
103
109
  }, abortController.signal));
104
110
  searchPromiseRef.current = searchPromise;
105
111
  try {
106
- const {
107
- tree: searchedTree,
108
- needPreloadNodes
109
- } = yield searchPromise;
112
+ const _yield$searchPromise = yield searchPromise,
113
+ searchedTree = _yield$searchPromise.tree,
114
+ needPreloadNodes = _yield$searchPromise.needPreloadNodes;
110
115
  if (!searchPromise.isCanceled()) {
111
116
  tree.current = searchedTree;
112
117
  treeItemsRecord.current = buildTreeItemsRecord(searchedTree);
@@ -121,7 +126,10 @@ function useSearchableTree(_ref) {
121
126
  return;
122
127
  }
123
128
  let tmpTree = [...searchedTree];
124
- for (const [nodeId, children] of Object.entries(preloadedNodes)) {
129
+ for (const _ref6 of Object.entries(preloadedNodes)) {
130
+ var _ref7 = (0, _slicedToArray2.default)(_ref6, 2);
131
+ const nodeId = _ref7[0];
132
+ const children = _ref7[1];
125
133
  tmpTree = (0, helpers_1.updateTreeNode)(tmpTree, nodeId, {
126
134
  nested: children
127
135
  });
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/slicedToArray"));
3
5
  var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
4
6
  function adopt(value) {
5
7
  return value instanceof P ? value : new P(function (resolve) {
@@ -54,34 +56,34 @@ const getNewSelectedIds = (selectedIds, added, removed) => {
54
56
  * @returns Текущий список выбранных id и обработчик выбора узла.
55
57
  */
56
58
  function useTreeMultiSelection(_ref) {
57
- let {
58
- onDataLoad,
59
- onSelect: onSelectProp,
60
- selected,
61
- onChangeSelected
62
- } = _ref;
63
- const [selectedIds = [], setSelectedIds] = (0, utils_1.useValueControl)({
64
- value: selected,
65
- defaultValue: [],
66
- onChange: onChangeSelected
67
- });
59
+ let onDataLoad = _ref.onDataLoad,
60
+ onSelectProp = _ref.onSelect,
61
+ selected = _ref.selected,
62
+ onChangeSelected = _ref.onChangeSelected;
63
+ const _ref2 = (0, utils_1.useValueControl)({
64
+ value: selected,
65
+ defaultValue: [],
66
+ onChange: onChangeSelected
67
+ }),
68
+ _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
69
+ _ref3$ = _ref3[0],
70
+ selectedIds = _ref3$ === void 0 ? [] : _ref3$,
71
+ setSelectedIds = _ref3[1];
68
72
  const onSelect = (0, react_1.useCallback)((selectedKeys, node) => __awaiter(this, void 0, void 0, function* () {
69
73
  const isSelected = selectedKeys.includes(node.id);
70
74
  const clonedNode = Object.assign({}, node);
71
75
  if (node.nested && !node.nested.length) {
72
- const {
73
- preloadedChildren
74
- } = yield onDataLoad(node);
76
+ const _yield$onDataLoad = yield onDataLoad(node),
77
+ preloadedChildren = _yield$onDataLoad.preloadedChildren;
75
78
  clonedNode.nested = preloadedChildren;
76
79
  }
77
- const {
78
- added,
79
- removed
80
- } = onSelectProp({
81
- selectedKeys,
82
- node: clonedNode,
83
- isSelected
84
- });
80
+ const _onSelectProp = onSelectProp({
81
+ selectedKeys,
82
+ node: clonedNode,
83
+ isSelected
84
+ }),
85
+ added = _onSelectProp.added,
86
+ removed = _onSelectProp.removed;
85
87
  const updatedSelectedIds = getNewSelectedIds(selectedIds, added, removed);
86
88
  setSelectedIds(updatedSelectedIds);
87
89
  }), [onDataLoad, onSelectProp, selectedIds, setSelectedIds]);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Tree",
7
- "version": "0.12.4-preview-6a625dbd.0",
7
+ "version": "0.12.4-preview-74429015.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -55,9 +55,9 @@
55
55
  "@siberiacancode/reactuse": "0.3.14",
56
56
  "@snack-uikit/button": "0.19.19",
57
57
  "@snack-uikit/icons": "0.27.7",
58
- "@snack-uikit/list": "0.33.3-preview-6a625dbd.0",
58
+ "@snack-uikit/list": "0.33.3-preview-74429015.0",
59
59
  "@snack-uikit/toggles": "0.13.27",
60
- "@snack-uikit/truncate-string": "0.7.14-preview-6a625dbd.0",
60
+ "@snack-uikit/truncate-string": "0.7.14-preview-74429015.0",
61
61
  "@snack-uikit/typography": "0.8.13",
62
62
  "@snack-uikit/utils": "4.0.2",
63
63
  "cancelable-promise": "4.3.1",
@@ -66,5 +66,5 @@
66
66
  "react-transition-state": "2.1.1",
67
67
  "uncontrollable": "8.0.4"
68
68
  },
69
- "gitHead": "38c6c6054ab0755be48b32041085b8b497f4960a"
69
+ "gitHead": "d3215f7071f7e99365d9da19a2485d8c39cf3aeb"
70
70
  }