react-arborist 0.1.1 → 0.1.5

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.
Files changed (38) hide show
  1. package/README.md +16 -2
  2. package/dist/lib/components/drop-cursor.js +11 -7
  3. package/dist/lib/components/preview.d.ts +0 -1
  4. package/dist/lib/components/preview.js +12 -8
  5. package/dist/lib/components/row.d.ts +2 -2
  6. package/dist/lib/components/row.js +27 -24
  7. package/dist/lib/components/tree.d.ts +2 -0
  8. package/dist/lib/components/tree.js +37 -0
  9. package/dist/lib/context.d.ts +3 -4
  10. package/dist/lib/context.js +47 -37
  11. package/dist/lib/data/enrich-tree.d.ts +2 -0
  12. package/dist/lib/data/enrich-tree.js +34 -0
  13. package/dist/lib/data/flatten-tree.d.ts +2 -0
  14. package/dist/lib/data/flatten-tree.js +20 -0
  15. package/dist/lib/data/make-tree.d.ts +5 -0
  16. package/dist/lib/data/make-tree.js +40 -0
  17. package/dist/lib/data/visible-nodes-hook.d.ts +2 -0
  18. package/dist/lib/data/visible-nodes-hook.js +13 -0
  19. package/dist/lib/dnd/compute-drop.js +11 -7
  20. package/dist/lib/dnd/drag-hook.js +17 -13
  21. package/dist/lib/dnd/drop-hook.js +17 -13
  22. package/dist/lib/dnd/measure-hover.js +7 -3
  23. package/dist/lib/dnd/outer-drop-hook.d.ts +1 -0
  24. package/dist/lib/dnd/outer-drop-hook.js +49 -0
  25. package/dist/lib/index.d.ts +1 -1
  26. package/dist/lib/index.js +5 -1
  27. package/dist/lib/reducer.js +26 -14
  28. package/dist/lib/selection/range.js +4 -1
  29. package/dist/lib/selection/selection-hook.js +10 -6
  30. package/dist/lib/selection/selection.js +6 -3
  31. package/dist/lib/selection/selection.test.js +4 -2
  32. package/dist/lib/types.d.ts +47 -43
  33. package/dist/lib/types.js +2 -1
  34. package/dist/lib/utils.d.ts +1 -5
  35. package/dist/lib/utils.js +13 -46
  36. package/package.json +6 -6
  37. package/dist/lib/components/tree-list.d.ts +0 -3
  38. package/dist/lib/components/tree-list.js +0 -75
package/README.md CHANGED
@@ -1,9 +1,23 @@
1
1
  # React Arborist
2
2
 
3
- *A full-featured tree component for React.*
3
+ A full-featured tree component for React.
4
4
 
5
- ![React Arborist Demo](http://g.recordit.co/XGrsubdORi.gif)
5
+ The tree is a ubiquitous UI component in software applications. There are already many libraries that provide this componet for React apps, but none were full-featured enough to stand on their own.
6
6
 
7
+ This libraries provides all the common features expected in a tree viewer. You can select one or many nodes to drag and drop into new positions, open and close folders, render an inline form for renaming, efficiently show thousands of items with virtualization, and provide your own node renderer to control the style.
8
+
9
+ ![Demo](https://user-images.githubusercontent.com/3460638/131920177-c47c34e5-d3e3-4826-937d-b366f527cdfe.gif)
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ yarn add react-arborist
15
+ ```
16
+
17
+ ```
18
+ npm install react-arborist
19
+ ```
20
+
7
21
  ## Features
8
22
 
9
23
  * sortable
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __assign = (this && this.__assign) || function () {
2
3
  __assign = Object.assign || function(t) {
3
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -9,12 +10,14 @@ var __assign = (this && this.__assign) || function () {
9
10
  };
10
11
  return __assign.apply(this, arguments);
11
12
  };
12
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
- import { createPortal } from "react-dom";
14
- import { useStaticContext } from "../context";
15
- export function DropCursor(_a) {
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.DropCursor = void 0;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ var react_dom_1 = require("react-dom");
17
+ var context_1 = require("../context");
18
+ function DropCursor(_a) {
16
19
  var root = _a.root, cursor = _a.cursor;
17
- var treeView = useStaticContext();
20
+ var treeView = (0, context_1.useStaticContext)();
18
21
  if (!cursor || !root || cursor.index === null || cursor.level === null)
19
22
  return null;
20
23
  var top = treeView.rowHeight * cursor.index;
@@ -26,8 +29,9 @@ export function DropCursor(_a) {
26
29
  left: left + "px",
27
30
  right: treeView.indent + "px",
28
31
  };
29
- return createPortal(_jsx(DefaultCursor, { style: style }, void 0), root);
32
+ return (0, react_dom_1.createPortal)((0, jsx_runtime_1.jsx)(DefaultCursor, { style: style }, void 0), root);
30
33
  }
34
+ exports.DropCursor = DropCursor;
31
35
  var placeholderStyle = {
32
36
  display: "flex",
33
37
  alignItems: "center",
@@ -45,5 +49,5 @@ var circleStyle = {
45
49
  };
46
50
  function DefaultCursor(_a) {
47
51
  var style = _a.style;
48
- return (_jsxs("div", __assign({ style: __assign(__assign({}, placeholderStyle), style) }, { children: [_jsx("div", { style: __assign({}, circleStyle) }, void 0), _jsx("div", { style: __assign({}, lineStyle) }, void 0)] }), void 0));
52
+ return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: __assign(__assign({}, placeholderStyle), style) }, { children: [(0, jsx_runtime_1.jsx)("div", { style: __assign({}, circleStyle) }, void 0), (0, jsx_runtime_1.jsx)("div", { style: __assign({}, lineStyle) }, void 0)] }), void 0));
49
53
  }
@@ -1,2 +1 @@
1
- /// <reference types="react" />
2
1
  export declare function Preview(): JSX.Element | null;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __assign = (this && this.__assign) || function () {
2
3
  __assign = Object.assign || function(t) {
3
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -9,9 +10,11 @@ var __assign = (this && this.__assign) || function () {
9
10
  };
10
11
  return __assign.apply(this, arguments);
11
12
  };
12
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
- import { useDragLayer } from "react-dnd";
14
- import { useStaticContext } from "../context";
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.Preview = void 0;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ var react_dnd_1 = require("react-dnd");
17
+ var context_1 = require("../context");
15
18
  var layerStyles = {
16
19
  position: "fixed",
17
20
  pointerEvents: "none",
@@ -33,9 +36,9 @@ var getCountStyle = function (offset) {
33
36
  var x = offset.x, y = offset.y;
34
37
  return { transform: "translate(" + (x + 10) + "px, " + (y + 10) + "px)" };
35
38
  };
36
- export function Preview() {
37
- var treeView = useStaticContext();
38
- var _a = useDragLayer(function (m) { return ({
39
+ function Preview() {
40
+ var treeView = (0, context_1.useStaticContext)();
41
+ var _a = (0, react_dnd_1.useDragLayer)(function (m) { return ({
39
42
  isDragging: m.isDragging(),
40
43
  offset: m.getSourceClientOffset(),
41
44
  mouse: m.getClientOffset(),
@@ -46,7 +49,7 @@ export function Preview() {
46
49
  var node = treeView.getNode(item.id);
47
50
  if (!node)
48
51
  return null;
49
- return (_jsxs("div", __assign({ style: layerStyles }, { children: [_jsx("div", __assign({ className: "row preview", style: getStyle(offset) }, { children: _jsx(treeView.renderer, { preview: true, node: node, props: {}, state: {
52
+ return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: layerStyles }, { children: [(0, jsx_runtime_1.jsx)("div", __assign({ className: "row preview", style: getStyle(offset) }, { children: (0, jsx_runtime_1.jsx)(treeView.renderer, { preview: true, node: node, props: { style: {}, ref: function () { } }, state: {
50
53
  isEditing: false,
51
54
  isSelected: false,
52
55
  isHoveringOverChild: false,
@@ -56,5 +59,6 @@ export function Preview() {
56
59
  toggleIsSelected: function () { },
57
60
  toggleIsOpen: function () { },
58
61
  rename: function () { },
59
- } }, void 0) }), void 0), item.dragIds.length > 1 && (_jsx("div", __assign({ className: "selected-count", style: getCountStyle(mouse) }, { children: item.dragIds.length }), void 0))] }), void 0));
62
+ } }, void 0) }), void 0), item.dragIds.length > 1 && ((0, jsx_runtime_1.jsx)("div", __assign({ className: "selected-count", style: getCountStyle(mouse) }, { children: item.dragIds.length }), void 0))] }), void 0));
60
63
  }
64
+ exports.Preview = Preview;
@@ -1,7 +1,7 @@
1
- import { CSSProperties } from "react";
1
+ import React, { CSSProperties } from "react";
2
2
  declare type Props = {
3
3
  style: CSSProperties;
4
4
  index: number;
5
5
  };
6
- export declare const Row: import("react").NamedExoticComponent<Props>;
6
+ export declare const Row: React.NamedExoticComponent<Props>;
7
7
  export {};
@@ -1,27 +1,30 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { memo, useMemo, useRef } from "react";
3
- import { useCursorParentId, useEditingId, useIsSelected, useStaticContext, } from "../context";
4
- import { useDragHook } from "../dnd/drag-hook";
5
- import { useDropHook } from "../dnd/drop-hook";
6
- import { edit, select } from "../reducer";
7
- export var Row = memo(function Row(_a) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Row = void 0;
4
+ var jsx_runtime_1 = require("react/jsx-runtime");
5
+ var react_1 = require("react");
6
+ var context_1 = require("../context");
7
+ var drag_hook_1 = require("../dnd/drag-hook");
8
+ var drop_hook_1 = require("../dnd/drop-hook");
9
+ var reducer_1 = require("../reducer");
10
+ exports.Row = (0, react_1.memo)(function Row(_a) {
8
11
  var index = _a.index, style = _a.style;
9
- var treeView = useStaticContext();
10
- var selected = useIsSelected();
12
+ var treeView = (0, context_1.useStaticContext)();
13
+ var selected = (0, context_1.useIsSelected)();
11
14
  var node = treeView.visibleNodes[index];
12
15
  var next = treeView.visibleNodes[index + 1] || null;
13
16
  var prev = treeView.visibleNodes[index - 1] || null;
14
- var cursorParentId = useCursorParentId();
15
- var el = useRef(null);
16
- var _b = useDragHook(node), isDragging = _b[0].isDragging, dragRef = _b[1];
17
- var dropRef = useDropHook(el, node, prev, next);
18
- var isEditing = node.id === useEditingId();
17
+ var cursorParentId = (0, context_1.useCursorParentId)();
18
+ var el = (0, react_1.useRef)(null);
19
+ var _b = (0, drag_hook_1.useDragHook)(node), isDragging = _b[0].isDragging, dragRef = _b[1];
20
+ var dropRef = (0, drop_hook_1.useDropHook)(el, node, prev, next);
21
+ var isEditing = node.id === (0, context_1.useEditingId)();
19
22
  var isSelected = selected(index);
20
23
  var nextSelected = next && selected(index + 1);
21
24
  var prevSelected = prev && selected(index - 1);
22
25
  var isHoveringOverChild = node.id === cursorParentId;
23
26
  var isOpen = node.isOpen;
24
- var state = useMemo(function () {
27
+ var state = (0, react_1.useMemo)(function () {
25
28
  return {
26
29
  isEditing: isEditing,
27
30
  isDragging: isDragging,
@@ -40,19 +43,19 @@ export var Row = memo(function Row(_a) {
40
43
  isOpen,
41
44
  isDragging,
42
45
  ]);
43
- var props = useMemo(function () { return ({
46
+ var props = (0, react_1.useMemo)(function () { return ({
44
47
  style: style,
45
48
  ref: function (n) {
46
49
  el.current = n;
47
50
  dragRef(dropRef(n));
48
51
  },
49
52
  }); }, [style, dragRef, dropRef]);
50
- var handlers = useMemo(function () {
53
+ var handlers = (0, react_1.useMemo)(function () {
51
54
  return {
52
55
  toggleIsSelected: function (e) {
53
56
  if (node.rowIndex === null)
54
57
  return;
55
- treeView.dispatch(select(node.rowIndex, e.metaKey, e.shiftKey));
58
+ treeView.dispatch((0, reducer_1.select)(node.rowIndex, e.metaKey, e.shiftKey));
56
59
  },
57
60
  toggleIsOpen: function (e) {
58
61
  e.stopPropagation();
@@ -60,21 +63,21 @@ export var Row = memo(function Row(_a) {
60
63
  },
61
64
  toggleIsEditing: function () {
62
65
  if (isEditing) {
63
- treeView.dispatch(edit(null));
66
+ treeView.dispatch((0, reducer_1.edit)(null));
64
67
  }
65
68
  else {
66
- treeView.dispatch(edit(node.id));
69
+ treeView.dispatch((0, reducer_1.edit)(node.id));
67
70
  }
68
71
  },
69
72
  rename: function (name) {
70
73
  if (name.trim())
71
74
  treeView.onRename(node.id, name);
72
- treeView.dispatch(edit(null));
75
+ treeView.dispatch((0, reducer_1.edit)(null));
73
76
  },
74
77
  };
75
78
  }, [treeView, node, isEditing]);
76
- var Renderer = useMemo(function () {
77
- return memo(treeView.renderer);
79
+ var Renderer = (0, react_1.useMemo)(function () {
80
+ return (0, react_1.memo)(treeView.renderer);
78
81
  }, [treeView.renderer]);
79
- return (_jsx(Renderer, { preview: false, node: node, props: props, state: state, indent: treeView.indent * node.level, handlers: handlers }, void 0));
82
+ return ((0, jsx_runtime_1.jsx)(Renderer, { preview: false, node: node, props: props, state: state, indent: treeView.indent * node.level, handlers: handlers }, void 0));
80
83
  });
@@ -0,0 +1,2 @@
1
+ import { IdObj, TreeProps } from "../types";
2
+ export declare function Tree<T extends IdObj>(props: TreeProps<T>): JSX.Element;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.Tree = void 0;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ var react_1 = require("react");
17
+ var react_dnd_1 = require("react-dnd");
18
+ var react_dnd_html5_backend_1 = require("react-dnd-html5-backend");
19
+ var react_window_1 = require("react-window");
20
+ var context_1 = require("../context");
21
+ var visible_nodes_hook_1 = require("../data/visible-nodes-hook");
22
+ var outer_drop_hook_1 = require("../dnd/outer-drop-hook");
23
+ var utils_1 = require("../utils");
24
+ var preview_1 = require("./preview");
25
+ var row_1 = require("./row");
26
+ function Tree(props) {
27
+ return ((0, jsx_runtime_1.jsx)(context_1.TreeViewProvider, __assign({ handle: props.handle, visibleNodes: (0, visible_nodes_hook_1.useVisibleNodes)(props), listRef: (0, react_1.useRef)(null), renderer: props.children, width: props.width, height: props.height, indent: props.indent || 12, rowHeight: props.rowHeight || 24, onClose: props.onClose || utils_1.noop, onMove: props.onMove || utils_1.noop, onOpen: props.onOpen || utils_1.noop, onRename: props.onRename || utils_1.noop }, { children: (0, jsx_runtime_1.jsxs)(react_dnd_1.DndProvider, __assign({ backend: react_dnd_html5_backend_1.HTML5Backend }, { children: [(0, jsx_runtime_1.jsx)(OuterDrop, { children: (0, jsx_runtime_1.jsx)(List, { className: props.className }, void 0) }, void 0), (0, jsx_runtime_1.jsx)(preview_1.Preview, {}, void 0)] }), void 0) }), void 0));
28
+ }
29
+ exports.Tree = Tree;
30
+ function OuterDrop(props) {
31
+ (0, outer_drop_hook_1.useOuterDrop)();
32
+ return props.children;
33
+ }
34
+ function List(props) {
35
+ var tree = (0, context_1.useStaticContext)();
36
+ return ((0, jsx_runtime_1.jsx)(react_window_1.FixedSizeList, __assign({ className: props.className, outerRef: tree.listRef, itemCount: tree.visibleNodes.length, height: tree.height, width: tree.width, itemSize: tree.rowHeight }, { children: row_1.Row }), void 0));
37
+ }
@@ -1,9 +1,8 @@
1
- /// <reference types="react" />
2
- import { StaticContext, TreeViewProviderProps } from "./types";
1
+ import { IdObj, StaticContext, TreeProviderProps } from "./types";
3
2
  export declare function useCursorParentId(): string | null;
4
- export declare function useStaticContext(): StaticContext;
3
+ export declare function useStaticContext(): StaticContext<IdObj>;
5
4
  export declare function useDispatch(): never;
6
5
  export declare function useSelectedIds(): string[];
7
6
  export declare function useIsSelected(): (index: number | null) => boolean;
8
7
  export declare function useEditingId(): string | null;
9
- export declare function TreeViewProvider(props: TreeViewProviderProps): JSX.Element;
8
+ export declare function TreeViewProvider<T>(props: TreeProviderProps<T>): JSX.Element;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __assign = (this && this.__assign) || function () {
2
3
  __assign = Object.assign || function(t) {
3
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -9,84 +10,93 @@ var __assign = (this && this.__assign) || function () {
9
10
  };
10
11
  return __assign.apply(this, arguments);
11
12
  };
12
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
- import { createContext, useContext, useImperativeHandle, useLayoutEffect, useMemo, useReducer, } from "react";
14
- import { DropCursor } from "./components/drop-cursor";
15
- import { edit, initState, reducer, select, selectId, setVisibleIds, } from "./reducer";
16
- import { Selection } from "./selection/selection";
17
- import { useSelectionKeys } from "./selection/selection-hook";
18
- var CursorParentId = createContext(null);
19
- export function useCursorParentId() {
20
- return useContext(CursorParentId);
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.TreeViewProvider = exports.useEditingId = exports.useIsSelected = exports.useSelectedIds = exports.useDispatch = exports.useStaticContext = exports.useCursorParentId = void 0;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ var react_1 = require("react");
17
+ var drop_cursor_1 = require("./components/drop-cursor");
18
+ var reducer_1 = require("./reducer");
19
+ var selection_1 = require("./selection/selection");
20
+ var selection_hook_1 = require("./selection/selection-hook");
21
+ var CursorParentId = (0, react_1.createContext)(null);
22
+ function useCursorParentId() {
23
+ return (0, react_1.useContext)(CursorParentId);
21
24
  }
22
- var Static = createContext(null);
23
- export function useStaticContext() {
24
- var value = useContext(Static);
25
+ exports.useCursorParentId = useCursorParentId;
26
+ var Static = (0, react_1.createContext)(null);
27
+ function useStaticContext() {
28
+ var value = (0, react_1.useContext)(Static);
25
29
  if (!value)
26
30
  throw new Error("Context must be in a provider");
27
31
  return value;
28
32
  }
29
- var DispatchContext = createContext(null);
30
- export function useDispatch() {
31
- var dispatch = useContext(DispatchContext);
33
+ exports.useStaticContext = useStaticContext;
34
+ var DispatchContext = (0, react_1.createContext)(null);
35
+ function useDispatch() {
36
+ var dispatch = (0, react_1.useContext)(DispatchContext);
32
37
  if (!dispatch)
33
38
  throw new Error("No dispatch provided");
34
39
  return dispatch;
35
40
  }
36
- var SelectionContext = createContext(null);
37
- export function useSelectedIds() {
38
- var value = useContext(SelectionContext);
41
+ exports.useDispatch = useDispatch;
42
+ var SelectionContext = (0, react_1.createContext)(null);
43
+ function useSelectedIds() {
44
+ var value = (0, react_1.useContext)(SelectionContext);
39
45
  if (!value)
40
46
  throw new Error("Must provide selection context");
41
47
  return value.ids;
42
48
  }
43
- export function useIsSelected() {
44
- var value = useContext(SelectionContext);
49
+ exports.useSelectedIds = useSelectedIds;
50
+ function useIsSelected() {
51
+ var value = (0, react_1.useContext)(SelectionContext);
45
52
  if (!value)
46
53
  throw new Error("Must provide selection context");
47
- var s = useMemo(function () { return Selection.parse(value.data, []); }, [value.data]);
54
+ var s = (0, react_1.useMemo)(function () { return selection_1.Selection.parse(value.data, []); }, [value.data]);
48
55
  return function (i) { return s.contains(i); };
49
56
  }
50
- var EditingIdContext = createContext(null);
51
- export function useEditingId() {
52
- return useContext(EditingIdContext);
57
+ exports.useIsSelected = useIsSelected;
58
+ var EditingIdContext = (0, react_1.createContext)(null);
59
+ function useEditingId() {
60
+ return (0, react_1.useContext)(EditingIdContext);
53
61
  }
54
- export function TreeViewProvider(props) {
62
+ exports.useEditingId = useEditingId;
63
+ function TreeViewProvider(props) {
55
64
  var _a;
56
- var _b = useReducer(reducer, initState()), state = _b[0], dispatch = _b[1];
57
- var visibleIds = useMemo(function () { return props.visibleNodes.map(function (n) { return n.id; }); }, [props.visibleNodes]);
58
- useSelectionKeys(props.listRef, dispatch, visibleIds);
59
- var idToIndex = useMemo(function () {
65
+ var _b = (0, react_1.useReducer)(reducer_1.reducer, (0, reducer_1.initState)()), state = _b[0], dispatch = _b[1];
66
+ var visibleIds = (0, react_1.useMemo)(function () { return props.visibleNodes.map(function (n) { return n.id; }); }, [props.visibleNodes]);
67
+ (0, selection_hook_1.useSelectionKeys)(props.listRef, dispatch, visibleIds);
68
+ var idToIndex = (0, react_1.useMemo)(function () {
60
69
  return props.visibleNodes.reduce(function (map, node, index) {
61
70
  map[node.id] = index;
62
71
  return map;
63
72
  }, {});
64
73
  }, [props.visibleNodes]);
65
- var staticValue = useMemo(function () { return (__assign(__assign({}, props), { dispatch: dispatch, getNode: function (id) {
74
+ var staticValue = (0, react_1.useMemo)(function () { return (__assign(__assign({}, props), { dispatch: dispatch, getNode: function (id) {
66
75
  if (id in idToIndex)
67
76
  return props.visibleNodes[idToIndex[id]] || null;
68
77
  else
69
78
  return null;
70
79
  } })); }, [props, idToIndex]);
71
- useLayoutEffect(function () {
72
- dispatch(setVisibleIds(visibleIds, idToIndex));
80
+ (0, react_1.useLayoutEffect)(function () {
81
+ dispatch((0, reducer_1.setVisibleIds)(visibleIds, idToIndex));
73
82
  }, [idToIndex, visibleIds]);
74
- useImperativeHandle(props.handle, function () { return ({
83
+ (0, react_1.useImperativeHandle)(props.handle, function () { return ({
75
84
  selectedIds: state.selection.ids,
76
85
  selectId: function (id) {
77
86
  var node = staticValue.getNode(id);
78
87
  if (node && node.rowIndex) {
79
- dispatch(select(node.rowIndex, false, false));
88
+ dispatch((0, reducer_1.select)(node.rowIndex, false, false));
80
89
  }
81
90
  else {
82
- dispatch(selectId(id));
91
+ dispatch((0, reducer_1.selectId)(id));
83
92
  }
84
93
  },
85
94
  edit: function (id) {
86
- dispatch(edit(id));
95
+ dispatch((0, reducer_1.edit)(id));
87
96
  },
88
97
  }); }, [staticValue, state.selection.ids]);
89
98
  return (
90
99
  // @ts-ignore
91
- _jsx(Static.Provider, __assign({ value: staticValue }, { children: _jsx(EditingIdContext.Provider, __assign({ value: state.editingId }, { children: _jsxs(SelectionContext.Provider, __assign({ value: state.selection }, { children: [_jsx(CursorParentId.Provider, __assign({ value: ((_a = state.cursorLocation) === null || _a === void 0 ? void 0 : _a.parentId) || null }, { children: props.children }), void 0), _jsx(DropCursor, { root: props.listRef.current, cursor: state.cursorLocation }, void 0)] }), void 0) }), void 0) }), void 0));
100
+ (0, jsx_runtime_1.jsx)(Static.Provider, __assign({ value: staticValue }, { children: (0, jsx_runtime_1.jsx)(EditingIdContext.Provider, __assign({ value: state.editingId }, { children: (0, jsx_runtime_1.jsxs)(SelectionContext.Provider, __assign({ value: state.selection }, { children: [(0, jsx_runtime_1.jsx)(CursorParentId.Provider, __assign({ value: ((_a = state.cursorLocation) === null || _a === void 0 ? void 0 : _a.parentId) || null }, { children: props.children }), void 0), (0, jsx_runtime_1.jsx)(drop_cursor_1.DropCursor, { root: props.listRef.current, cursor: state.cursorLocation }, void 0)] }), void 0) }), void 0) }), void 0));
92
101
  }
102
+ exports.TreeViewProvider = TreeViewProvider;
@@ -0,0 +1,2 @@
1
+ import { IdObj, Node } from "../types";
2
+ export declare function enrichTree<T extends IdObj>(model: T, hideRoot?: boolean, getChildren?: (m: T) => T[] | undefined, getIsOpen?: (m: T) => boolean): Node<T>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.enrichTree = void 0;
4
+ var defaultGetIsOpen = function (m) { return m.isOpen; };
5
+ var defaultGetChildren = function (m) { return m.children; };
6
+ function createNode(model, level, parent, children, isOpen) {
7
+ return {
8
+ id: model.id,
9
+ level: level,
10
+ parent: parent,
11
+ children: children,
12
+ isOpen: isOpen,
13
+ model: model,
14
+ rowIndex: null,
15
+ };
16
+ }
17
+ function enrichTree(model, hideRoot, getChildren, getIsOpen) {
18
+ if (hideRoot === void 0) { hideRoot = false; }
19
+ if (getChildren === void 0) { getChildren = defaultGetChildren; }
20
+ if (getIsOpen === void 0) { getIsOpen = defaultGetIsOpen; }
21
+ function visitSelfAndChildren(model, level, parent) {
22
+ var isOpen = getIsOpen(model);
23
+ var node = createNode(model, level, parent, null, isOpen);
24
+ var children = getChildren(model);
25
+ if (children) {
26
+ node.children = children.map(function (child) {
27
+ return visitSelfAndChildren(child, level + 1, node);
28
+ });
29
+ }
30
+ return node;
31
+ }
32
+ return visitSelfAndChildren(model, hideRoot ? -1 : 0, null);
33
+ }
34
+ exports.enrichTree = enrichTree;