react-arborist 1.1.0 → 2.0.0-rc
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.
- package/dist/components/{drop-cursor.d.ts → cursor.d.ts} +0 -0
- package/dist/components/default-container.d.ts +2 -0
- package/dist/components/default-cursor.d.ts +3 -0
- package/dist/components/default-drag-preview.d.ts +3 -0
- package/dist/components/default-node.d.ts +4 -0
- package/dist/components/default-row.d.ts +4 -0
- package/dist/components/drag-preview-container.d.ts +2 -0
- package/dist/components/list-inner-element.d.ts +2 -0
- package/dist/components/list-outer-element.d.ts +2 -0
- package/dist/components/outer-drop.d.ts +4 -0
- package/dist/components/provider.d.ts +11 -0
- package/dist/components/row-container.d.ts +8 -0
- package/dist/components/tree-container.d.ts +2 -0
- package/dist/components/tree.d.ts +5 -4
- package/dist/context.d.ts +23 -18
- package/dist/data/create-index.d.ts +5 -0
- package/dist/data/create-list.d.ts +4 -0
- package/dist/data/create-root.d.ts +5 -0
- package/dist/data/flatten-tree.d.ts +4 -2
- package/dist/data/simple-tree.d.ts +43 -0
- package/dist/dnd/compute-drop.d.ts +4 -4
- package/dist/dnd/drag-hook.d.ts +3 -4
- package/dist/dnd/drop-hook.d.ts +2 -3
- package/dist/hooks/use-fresh-node.d.ts +2 -0
- package/dist/hooks/use-simple-tree.d.ts +13 -0
- package/dist/hooks/use-uncontrolled-tree.d.ts +24 -0
- package/dist/hooks/use-validated-props.d.ts +3 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.js +2093 -1184
- package/dist/index.js.map +1 -1
- package/dist/interfaces/node-api.d.ts +67 -0
- package/dist/interfaces/tree-api.d.ts +112 -0
- package/dist/module.js +2082 -1192
- package/dist/module.js.map +1 -1
- package/dist/state/dnd-slice.d.ts +20 -0
- package/dist/state/drag-slice.d.ts +7 -0
- package/dist/state/edit-slice.d.ts +8 -0
- package/dist/state/focus-slice.d.ts +12 -0
- package/dist/state/initial.d.ts +3 -0
- package/dist/state/open-slice.d.ts +30 -0
- package/dist/state/root-reducer.d.ts +13 -0
- package/dist/state/selection-slice.d.ts +36 -0
- package/dist/types/dnd.d.ts +9 -0
- package/dist/types/handlers.d.ts +24 -0
- package/dist/types/renderers.d.ts +30 -0
- package/dist/types/state.d.ts +2 -0
- package/dist/types/tree-props.d.ts +43 -0
- package/dist/types/utils.d.ts +21 -0
- package/dist/utils/props.d.ts +3 -0
- package/dist/utils.d.ts +15 -6
- package/package.json +10 -7
- package/src/components/cursor.tsx +15 -0
- package/src/components/default-container.tsx +229 -0
- package/src/components/{drop-cursor.tsx → default-cursor.tsx} +15 -20
- package/src/components/default-drag-preview.tsx +92 -0
- package/src/components/default-node.tsx +15 -0
- package/src/components/default-row.tsx +21 -0
- package/src/components/drag-preview-container.tsx +26 -0
- package/src/components/list-inner-element.tsx +22 -0
- package/src/components/list-outer-element.tsx +45 -0
- package/src/components/outer-drop.ts +7 -0
- package/src/components/provider.tsx +97 -0
- package/src/components/row-container.tsx +82 -0
- package/src/components/tree-container.tsx +13 -0
- package/src/components/tree.tsx +17 -126
- package/src/context.ts +36 -0
- package/src/data/create-index.ts +9 -0
- package/src/data/create-list.ts +56 -0
- package/src/data/create-root.ts +53 -0
- package/src/data/simple-tree.ts +103 -0
- package/src/dnd/compute-drop.ts +16 -16
- package/src/dnd/drag-hook.ts +28 -23
- package/src/dnd/drop-hook.ts +35 -21
- package/src/dnd/outer-drop-hook.ts +6 -6
- package/src/hooks/use-fresh-node.ts +16 -0
- package/src/hooks/use-simple-tree.ts +55 -0
- package/src/hooks/use-validated-props.ts +35 -0
- package/src/index.ts +9 -5
- package/src/interfaces/node-api.ts +187 -0
- package/src/interfaces/tree-api.ts +552 -0
- package/src/state/dnd-slice.ts +36 -0
- package/src/state/drag-slice.ts +31 -0
- package/src/state/edit-slice.ts +19 -0
- package/src/state/focus-slice.ts +28 -0
- package/src/state/initial.ts +14 -0
- package/src/state/open-slice.ts +53 -0
- package/src/state/root-reducer.ts +21 -0
- package/src/state/selection-slice.ts +75 -0
- package/src/types/dnd.ts +10 -0
- package/src/types/handlers.ts +24 -0
- package/src/types/renderers.ts +34 -0
- package/src/types/state.ts +3 -0
- package/src/types/tree-props.ts +63 -0
- package/src/types/utils.ts +26 -0
- package/src/utils/props.ts +8 -0
- package/src/utils.ts +125 -11
- package/README.md +0 -220
- package/dist/components/preview.d.ts +0 -2
- package/dist/components/row.d.ts +0 -7
- package/dist/data/enrich-tree.d.ts +0 -2
- package/dist/provider.d.ts +0 -3
- package/dist/reducer.d.ts +0 -46
- package/dist/selection/range.d.ts +0 -13
- package/dist/selection/selection-hook.d.ts +0 -3
- package/dist/selection/selection.d.ts +0 -33
- package/dist/tree-api-hook.d.ts +0 -6
- package/dist/tree-api.d.ts +0 -34
- package/dist/types.d.ts +0 -131
- package/src/components/preview.tsx +0 -108
- package/src/components/row.tsx +0 -114
- package/src/context.tsx +0 -52
- package/src/data/enrich-tree.ts +0 -74
- package/src/data/flatten-tree.ts +0 -17
- package/src/provider.tsx +0 -61
- package/src/reducer.ts +0 -161
- package/src/selection/range.ts +0 -41
- package/src/selection/selection-hook.ts +0 -24
- package/src/selection/selection.test.ts +0 -111
- package/src/selection/selection.ts +0 -186
- package/src/tree-api-hook.ts +0 -34
- package/src/tree-api.ts +0 -156
- package/src/types.ts +0 -155
|
@@ -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 next(): NodeApi<T> | null {
|
|
42
|
+
if (this.rowIndex === null) return null;
|
|
43
|
+
return this.tree.at(this.rowIndex + 1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get prev(): NodeApi<T> | null {
|
|
47
|
+
if (this.rowIndex === null) return null;
|
|
48
|
+
return this.tree.at(this.rowIndex - 1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get nextSibling(): NodeApi<T> | null {
|
|
52
|
+
const i = this.childIndex;
|
|
53
|
+
return this.parent?.children![i + 1] ?? null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get isRoot() {
|
|
57
|
+
return this.id === ROOT_ID;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get isLeaf() {
|
|
61
|
+
return !Array.isArray(this.children);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get isInternal() {
|
|
65
|
+
return !this.isLeaf;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get isOpen() {
|
|
69
|
+
return this.isLeaf ? false : this.tree.isOpen(this.id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get isEditing() {
|
|
73
|
+
return this.tree.editingId === this.id;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get isSelected() {
|
|
77
|
+
return this.tree.isSelected(this.id);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get isSelectedStart() {
|
|
81
|
+
return this.isSelected && !this.prev?.isSelected;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get isSelectedEnd() {
|
|
85
|
+
return this.isSelected && !this.next?.isSelected;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get isFocused() {
|
|
89
|
+
return this.tree.isFocused(this.id);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get childIndex() {
|
|
93
|
+
if (this.parent && this.parent.children) {
|
|
94
|
+
return this.parent.children.findIndex((child) => child.id === this.id);
|
|
95
|
+
} else {
|
|
96
|
+
return -1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get isDragging() {
|
|
101
|
+
return this.tree.isDragging(this.id);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get willReceiveDrop() {
|
|
105
|
+
return this.tree.willReceiveDrop(this.id);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get state() {
|
|
109
|
+
return {
|
|
110
|
+
isEditing: this.isEditing,
|
|
111
|
+
isDragging: this.isDragging,
|
|
112
|
+
isSelected: this.isSelected,
|
|
113
|
+
isSelectedStart: this.isSelectedStart,
|
|
114
|
+
isSelectedEnd: this.isSelectedEnd,
|
|
115
|
+
isFocused: this.isFocused,
|
|
116
|
+
isOpen: this.isOpen,
|
|
117
|
+
willReceiveDrop: this.willReceiveDrop,
|
|
118
|
+
};
|
|
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
|
+
}
|