react-arborist 1.2.0 → 2.0.0-rc.1

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 (118) hide show
  1. package/README.md +606 -144
  2. package/dist/components/{drop-cursor.d.ts → cursor.d.ts} +0 -0
  3. package/dist/components/default-container.d.ts +7 -0
  4. package/dist/components/default-cursor.d.ts +3 -0
  5. package/dist/components/default-drag-preview.d.ts +3 -0
  6. package/dist/components/default-node.d.ts +4 -0
  7. package/dist/components/default-row.d.ts +4 -0
  8. package/dist/components/drag-preview-container.d.ts +2 -0
  9. package/dist/components/list-inner-element.d.ts +2 -0
  10. package/dist/components/provider.d.ts +11 -0
  11. package/dist/components/row-container.d.ts +8 -0
  12. package/dist/components/tree-container.d.ts +2 -0
  13. package/dist/components/tree.d.ts +5 -4
  14. package/dist/context.d.ts +20 -2
  15. package/dist/data/create-index.d.ts +5 -0
  16. package/dist/data/create-list.d.ts +4 -0
  17. package/dist/data/create-root.d.ts +5 -0
  18. package/dist/data/simple-tree.d.ts +43 -0
  19. package/dist/dnd/compute-drop.d.ts +4 -4
  20. package/dist/dnd/drag-hook.d.ts +3 -4
  21. package/dist/dnd/drop-hook.d.ts +2 -3
  22. package/dist/hooks/use-fresh-node.d.ts +2 -0
  23. package/dist/hooks/use-simple-tree.d.ts +13 -0
  24. package/dist/hooks/use-validated-props.d.ts +3 -0
  25. package/dist/index.d.ts +8 -4
  26. package/dist/index.js +1948 -973
  27. package/dist/index.js.map +1 -1
  28. package/dist/interfaces/node-api.d.ts +67 -0
  29. package/dist/interfaces/tree-api.d.ts +113 -0
  30. package/dist/module.js +1935 -979
  31. package/dist/module.js.map +1 -1
  32. package/dist/state/dnd-slice.d.ts +20 -0
  33. package/dist/state/drag-slice.d.ts +7 -0
  34. package/dist/state/edit-slice.d.ts +8 -0
  35. package/dist/state/focus-slice.d.ts +12 -0
  36. package/dist/state/initial.d.ts +3 -0
  37. package/dist/state/open-slice.d.ts +30 -0
  38. package/dist/state/root-reducer.d.ts +13 -0
  39. package/dist/state/selection-slice.d.ts +36 -0
  40. package/dist/types/dnd.d.ts +9 -0
  41. package/dist/types/handlers.d.ts +24 -0
  42. package/dist/types/renderers.d.ts +30 -0
  43. package/dist/types/state.d.ts +2 -0
  44. package/dist/types/tree-props.d.ts +44 -0
  45. package/dist/types/utils.d.ts +21 -0
  46. package/dist/utils.d.ts +15 -6
  47. package/package.json +11 -7
  48. package/src/components/cursor.tsx +15 -0
  49. package/src/components/default-container.tsx +238 -0
  50. package/src/components/{default-drop-cursor.tsx → default-cursor.tsx} +9 -8
  51. package/src/components/{preview.tsx → default-drag-preview.tsx} +25 -41
  52. package/src/components/default-node.tsx +50 -0
  53. package/src/components/default-row.tsx +21 -0
  54. package/src/components/drag-preview-container.tsx +26 -0
  55. package/src/components/list-inner-element.tsx +22 -0
  56. package/src/components/list-outer-element.tsx +26 -15
  57. package/src/components/provider.tsx +97 -0
  58. package/src/components/row-container.tsx +82 -0
  59. package/src/components/tree-container.tsx +13 -0
  60. package/src/components/tree.tsx +16 -44
  61. package/src/context.ts +36 -0
  62. package/src/data/create-index.ts +9 -0
  63. package/src/data/create-list.ts +56 -0
  64. package/src/data/create-root.ts +54 -0
  65. package/src/data/simple-tree.ts +103 -0
  66. package/src/dnd/compute-drop.ts +16 -16
  67. package/src/dnd/drag-hook.ts +25 -19
  68. package/src/dnd/drop-hook.ts +31 -17
  69. package/src/dnd/outer-drop-hook.ts +1 -1
  70. package/src/hooks/use-fresh-node.ts +16 -0
  71. package/src/hooks/use-simple-tree.ts +55 -0
  72. package/src/hooks/use-validated-props.ts +35 -0
  73. package/src/index.ts +9 -19
  74. package/src/interfaces/node-api.ts +187 -0
  75. package/src/interfaces/tree-api.ts +557 -0
  76. package/src/state/dnd-slice.ts +36 -0
  77. package/src/state/drag-slice.ts +31 -0
  78. package/src/state/edit-slice.ts +19 -0
  79. package/src/state/focus-slice.ts +28 -0
  80. package/src/state/initial.ts +14 -0
  81. package/src/state/open-slice.ts +53 -0
  82. package/src/state/root-reducer.ts +21 -0
  83. package/src/state/selection-slice.ts +75 -0
  84. package/src/types/dnd.ts +10 -0
  85. package/src/types/handlers.ts +24 -0
  86. package/src/types/renderers.ts +34 -0
  87. package/src/types/state.ts +3 -0
  88. package/src/types/tree-props.ts +64 -0
  89. package/src/types/utils.ts +26 -0
  90. package/src/utils.ts +125 -11
  91. package/tsconfig.json +1 -1
  92. package/dist/components/default-drop-cursor.d.ts +0 -3
  93. package/dist/components/list.d.ts +0 -4
  94. package/dist/components/preview.d.ts +0 -2
  95. package/dist/components/row.d.ts +0 -8
  96. package/dist/data/enrich-tree.d.ts +0 -2
  97. package/dist/data/flatten-tree.d.ts +0 -2
  98. package/dist/provider.d.ts +0 -3
  99. package/dist/reducer.d.ts +0 -46
  100. package/dist/selection/range.d.ts +0 -13
  101. package/dist/selection/selection-hook.d.ts +0 -4
  102. package/dist/selection/selection.d.ts +0 -33
  103. package/dist/tree-api.d.ts +0 -50
  104. package/dist/types.d.ts +0 -122
  105. package/src/components/drop-cursor.tsx +0 -12
  106. package/src/components/list.tsx +0 -25
  107. package/src/components/row.tsx +0 -112
  108. package/src/context.tsx +0 -13
  109. package/src/data/enrich-tree.ts +0 -74
  110. package/src/data/flatten-tree.ts +0 -17
  111. package/src/provider.tsx +0 -41
  112. package/src/reducer.ts +0 -161
  113. package/src/selection/range.ts +0 -41
  114. package/src/selection/selection-hook.ts +0 -25
  115. package/src/selection/selection.test.ts +0 -111
  116. package/src/selection/selection.ts +0 -186
  117. package/src/tree-api.ts +0 -230
  118. package/src/types.ts +0 -148
@@ -0,0 +1,187 @@
1
+ import React from "react";
2
+ import { TreeApi } from "./tree-api";
3
+ import { IdObj } from "../types/utils";
4
+ import { ROOT_ID } from "../data/create-root";
5
+
6
+ type Params<T extends IdObj> = {
7
+ id: string;
8
+ data: T;
9
+ level: number;
10
+ children: NodeApi<T>[] | null;
11
+ parent: NodeApi<T> | null;
12
+ isDraggable: boolean;
13
+ isDroppable: boolean;
14
+ rowIndex: number | null;
15
+ tree: TreeApi<T>;
16
+ };
17
+
18
+ export class NodeApi<T extends IdObj = IdObj> {
19
+ tree: TreeApi<T>;
20
+ id: string;
21
+ data: T;
22
+ level: number;
23
+ children: NodeApi<T>[] | null;
24
+ parent: NodeApi<T> | null;
25
+ isDraggable: boolean;
26
+ isDroppable: boolean;
27
+ rowIndex: number | null;
28
+
29
+ constructor(params: Params<T>) {
30
+ this.tree = params.tree;
31
+ this.id = params.id;
32
+ this.data = params.data;
33
+ this.level = params.level;
34
+ this.children = params.children;
35
+ this.parent = params.parent;
36
+ this.isDraggable = params.isDraggable;
37
+ this.isDroppable = params.isDroppable;
38
+ this.rowIndex = params.rowIndex;
39
+ }
40
+
41
+ get isRoot() {
42
+ return this.id === ROOT_ID;
43
+ }
44
+
45
+ get isLeaf() {
46
+ return !Array.isArray(this.children);
47
+ }
48
+
49
+ get isInternal() {
50
+ return !this.isLeaf;
51
+ }
52
+
53
+ get isOpen() {
54
+ return this.isLeaf ? false : this.tree.isOpen(this.id);
55
+ }
56
+
57
+ get isEditing() {
58
+ return this.tree.editingId === this.id;
59
+ }
60
+
61
+ get isSelected() {
62
+ return this.tree.isSelected(this.id);
63
+ }
64
+
65
+ get isSelectedStart() {
66
+ return this.isSelected && !this.prev?.isSelected;
67
+ }
68
+
69
+ get isSelectedEnd() {
70
+ return this.isSelected && !this.next?.isSelected;
71
+ }
72
+
73
+ get isFocused() {
74
+ return this.tree.isFocused(this.id);
75
+ }
76
+
77
+ get isDragging() {
78
+ return this.tree.isDragging(this.id);
79
+ }
80
+
81
+ get willReceiveDrop() {
82
+ return this.tree.willReceiveDrop(this.id);
83
+ }
84
+
85
+ get state() {
86
+ return {
87
+ isEditing: this.isEditing,
88
+ isDragging: this.isDragging,
89
+ isSelected: this.isSelected,
90
+ isSelectedStart: this.isSelectedStart,
91
+ isSelectedEnd: this.isSelectedEnd,
92
+ isFocused: this.isFocused,
93
+ isOpen: this.isOpen,
94
+ willReceiveDrop: this.willReceiveDrop,
95
+ };
96
+ }
97
+
98
+ get childIndex() {
99
+ if (this.parent && this.parent.children) {
100
+ return this.parent.children.findIndex((child) => child.id === this.id);
101
+ } else {
102
+ return -1;
103
+ }
104
+ }
105
+
106
+ get next(): NodeApi<T> | null {
107
+ if (this.rowIndex === null) return null;
108
+ return this.tree.at(this.rowIndex + 1);
109
+ }
110
+
111
+ get prev(): NodeApi<T> | null {
112
+ if (this.rowIndex === null) return null;
113
+ return this.tree.at(this.rowIndex - 1);
114
+ }
115
+
116
+ get nextSibling(): NodeApi<T> | null {
117
+ const i = this.childIndex;
118
+ return this.parent?.children![i + 1] ?? null;
119
+ }
120
+
121
+ select() {
122
+ this.tree.select(this);
123
+ }
124
+
125
+ deselect() {
126
+ this.tree.deselect(this);
127
+ }
128
+
129
+ selectMulti() {
130
+ this.tree.selectMulti(this);
131
+ }
132
+
133
+ selectContiguous() {
134
+ this.tree.selectContiguous(this);
135
+ }
136
+
137
+ activate() {
138
+ this.tree.activate(this);
139
+ }
140
+
141
+ focus() {
142
+ this.tree.focus(this);
143
+ }
144
+
145
+ toggle() {
146
+ this.tree.toggle(this);
147
+ }
148
+
149
+ open() {
150
+ this.tree.open(this);
151
+ }
152
+
153
+ openParents() {
154
+ this.tree.openParents(this);
155
+ }
156
+
157
+ close() {
158
+ this.tree.close(this);
159
+ }
160
+
161
+ submit(value: string) {
162
+ this.tree.submit(this, value);
163
+ }
164
+
165
+ reset() {
166
+ this.tree.reset();
167
+ }
168
+
169
+ clone() {
170
+ return new NodeApi<T>({ ...this });
171
+ }
172
+
173
+ edit() {
174
+ return this.tree.edit(this);
175
+ }
176
+
177
+ handleClick = (e: React.MouseEvent) => {
178
+ if (e.metaKey) {
179
+ this.isSelected ? this.deselect() : this.selectMulti();
180
+ } else if (e.shiftKey) {
181
+ this.selectContiguous();
182
+ } else {
183
+ this.select();
184
+ this.activate();
185
+ }
186
+ };
187
+ }