react-arborist 0.2.0 → 1.0.3
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/README.md +197 -0
- package/dist/index.js +54 -27
- package/dist/index.js.map +1 -1
- package/dist/module.js +54 -27
- package/dist/module.js.map +1 -1
- package/dist/tree-api.d.ts +8 -3
- package/dist/types.d.ts +12 -4
- package/package.json +4 -3
- package/src/components/preview.tsx +3 -3
- package/src/components/row.tsx +10 -15
- package/src/tree-api.ts +36 -9
- package/src/types.ts +8 -4
package/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
<h1><img alt="React Arborist Logo" src="https://user-images.githubusercontent.com/3460638/159076412-1231a1e6-14ca-4fa6-9c90-3b61a2a1bc56.png" width="100px" height="100px" /> React Arborist</h1>
|
|
2
|
+
|
|
3
|
+
A full-featured tree component for React.
|
|
4
|
+
|
|
5
|
+
The tree UI is ubiquitous in software applications. There are many tree component libraries for React, but none were full-featured enough to stand on their own.
|
|
6
|
+
|
|
7
|
+
This library provides all the common features expected in a tree. 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, and provide your own node renderer to control the style.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
yarn add react-arborist
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
npm install react-arborist
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Example
|
|
22
|
+
|
|
23
|
+
Render the tree data structure.
|
|
24
|
+
|
|
25
|
+
```jsx
|
|
26
|
+
const data = {
|
|
27
|
+
id: "The Root",
|
|
28
|
+
children: [{id: "Node A"}, {id: "Node B"}]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function App() {
|
|
32
|
+
return (
|
|
33
|
+
<Tree data={data}>
|
|
34
|
+
{Node}
|
|
35
|
+
</Tree>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function Node({ ref, styles, data}) {
|
|
40
|
+
return (
|
|
41
|
+
<div ref={ref} style={styles.row}>
|
|
42
|
+
<div style={styles.indent}>
|
|
43
|
+
{data.name}
|
|
44
|
+
</div>
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### Contents
|
|
51
|
+
* [Expected Data Structure](#expected-data-structure)
|
|
52
|
+
* [Tree Component API](#tree-component)
|
|
53
|
+
* [Node Renderer API](#node-renderer-component)
|
|
54
|
+
* [Styles Prop](#styles-prop)
|
|
55
|
+
* [State Prop](#state-prop)
|
|
56
|
+
* [Handlers Prop](#handlers-prop)
|
|
57
|
+
* [Tree Prop](#tree-prop)
|
|
58
|
+
|
|
59
|
+
## Expected Data Structure
|
|
60
|
+
|
|
61
|
+
The Tree component expects the data prop to be a tree-like data structure with the following type:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
type Data = {
|
|
65
|
+
id: string, /* Required */
|
|
66
|
+
children?: Data[]
|
|
67
|
+
isOpen?: boolean
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
If your data does not look like this, you can provide a `childrenAccessor` prop. You can also provide `isOpenAccessor`. The value can be a string or a function.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
<Tree childrenAccessor="items" ...
|
|
75
|
+
// Or
|
|
76
|
+
<Tree childrenAccessor={(node) => node.items} ...
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Tree Component
|
|
80
|
+
|
|
81
|
+
Unlike other Tree Components, react-arborist is designed as a [controlled component](https://reactjs.org/docs/forms.html#controlled-components). This means the consumer will provide the tree data and the handlers to update it. The only state managed internally is for drag and drop, selection, and editing.
|
|
82
|
+
|
|
83
|
+
| Prop | Default | Description |
|
|
84
|
+
| --- | --- | --- |
|
|
85
|
+
| data | (required) | The tree data structure to render as described above. |
|
|
86
|
+
| width | 300 | The width of the tree.
|
|
87
|
+
| height | 500 | The height of the tree. To dynamically fill it's container, use a [hook](https://github.com/ZeeCoder/use-resize-observer) or [component](https://github.com/bvaughn/react-virtualized-auto-sizer) to gather the width and height of the Tree's parent.
|
|
88
|
+
| rowHeight | 24 | The height of each row.
|
|
89
|
+
| indent | 24 | The number of pixels to indent child nodes.
|
|
90
|
+
| hideRoot | false | Hide the root node so that the first set of children appear as the roots.
|
|
91
|
+
| onToggle | noop | Handler called when a node is opened or closed. This and the subsequent functions should update the `data` prop for the tree to re-render.
|
|
92
|
+
| onMove | noop | Handler called when a user moves one or more nodes by dragging and dropping.
|
|
93
|
+
| onEdit | noop | Handler called when a user performs an inline edit of the node.
|
|
94
|
+
| childrenAccessor | "children" | Used to get a node's children if they exist on a property other than "children".
|
|
95
|
+
| isOpenAccessor | "isOpen" | Used to get a node's openness state if it exists on a property other than "isOpen".
|
|
96
|
+
| openByDefault | true | Choose if the node should be open or closed when it has an undefined openness state.
|
|
97
|
+
| className | undefined | Adds a class to the containing div.
|
|
98
|
+
|
|
99
|
+
The only child of the Tree Component must be a NodeRenderer function as described below.
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
const NodeRenderer = ({
|
|
103
|
+
innerRef, data, styles, handlers, state, tree
|
|
104
|
+
}) => ...
|
|
105
|
+
|
|
106
|
+
const MyApp = () =>
|
|
107
|
+
<Tree>
|
|
108
|
+
{NodeRenderer}
|
|
109
|
+
</Tree>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Node Renderer Component
|
|
113
|
+
|
|
114
|
+
The Node Renderer is where you get to make the tree your own. You completely own the style and functionality. The props passed to it should enable you to do whatever you need.
|
|
115
|
+
|
|
116
|
+
The most basic node renderer will look like this:
|
|
117
|
+
|
|
118
|
+
```jsx
|
|
119
|
+
function NodeRenderer({
|
|
120
|
+
innerRef,
|
|
121
|
+
styles,
|
|
122
|
+
data,
|
|
123
|
+
state,
|
|
124
|
+
handlers,
|
|
125
|
+
tree
|
|
126
|
+
}) {
|
|
127
|
+
return (
|
|
128
|
+
<div ref={innerRef} style={styles.row}>
|
|
129
|
+
<div style={styles.indent}>{data.id}</div>
|
|
130
|
+
</div>
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The function above is passed data for this individual node, the DOM ref used for drag and drop, the styles to position the row in the virtualized list and the styles to indent the current node according to its level in the tree.
|
|
136
|
+
|
|
137
|
+
| Prop | Type | Description |
|
|
138
|
+
| ---- | ---- | ----------- |
|
|
139
|
+
| data | Node | A single node from the tree data structure provided. |
|
|
140
|
+
| innerRef | Ref | Must be attached to the root element returned by the NodeRenderer. This is needed for drag and drop to work.
|
|
141
|
+
| [styles](#styles-prop) | object | This is an object that contains styles for the position of the row, and the level of indentation. Each key is described below.
|
|
142
|
+
| [state](#state-prop) | object | An handful of boolean values that indicate the current state of this node. See below for details.
|
|
143
|
+
| [handlers](#handlers-prop) | object | A collection of handlers to attach to the DOM, that provide selectable and toggle-able behaviors. Each handler is described below.
|
|
144
|
+
| [tree](#tree-prop) | TreeMonitor | This object can be used to get at the internal state of the whole tree component. For example, `tree.getSelectedNodes()`. All the methods are listed below in the Tree Prop section.
|
|
145
|
+
|
|
146
|
+
### Styles Prop
|
|
147
|
+
|
|
148
|
+
These are the properties on the styles object passed to the NodeRenderer.
|
|
149
|
+
|
|
150
|
+
| Name | Type | Description |
|
|
151
|
+
| ---- | ---- | ----------- |
|
|
152
|
+
| row | CSSProperties | Since the tree only renders the rows that are currently visible, all the rows are absolutely positioned with a fixed top and left. Those styles are in this property.
|
|
153
|
+
| indent | CSSProperties | This is simply a left padding set to the level of the tree multiplied by the tree indent prop.
|
|
154
|
+
|
|
155
|
+
### State Prop
|
|
156
|
+
|
|
157
|
+
These are the properties on the state object passed to the NodeRenderer.
|
|
158
|
+
|
|
159
|
+
| Name | Type | Description |
|
|
160
|
+
| ---- | ---- | ----------- |
|
|
161
|
+
| isOpen | boolean | True if this node has children and the children are visible. Use this to display some type a open or closed icon.
|
|
162
|
+
| isSelected | boolean | True if this node is selected. Use this to show a "selected" state. Maybe a different background?
|
|
163
|
+
| isHoveringOverChild | boolean | True if the user is dragging n node, and the node is hovering over one of this node's direct children. This can be used to indicate which folder the user is dragging an item into.
|
|
164
|
+
| isDragging | boolean | True if this node is being dragged.
|
|
165
|
+
| isSelectedStart | boolean | True if this is the first of a contiguous group of selected rows. This can be used to tastefully style a group of selected items. Maybe a different border radius on the first and last rows?
|
|
166
|
+
| isSelectedEnd | boolean | True if this is the last of a contiguous group of selected rows.
|
|
167
|
+
| isEditing | boolean | True if this row is being edited. When true, the renderer should return some type of form input.
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
### Handlers Prop
|
|
171
|
+
|
|
172
|
+
These are the properties on the handlers object passed to the NodeRenderer.
|
|
173
|
+
|
|
174
|
+
| Name | Type | Description |
|
|
175
|
+
| ---- | ---- | ----------- |
|
|
176
|
+
| select | MouseEventHandler | Attach this to the element that tiggers selection. Maybe you want to add it to the outermost div. <br /> `<div onClick={handlers.select}>`
|
|
177
|
+
| toggle | MouseEventHandler | Attach this to the element that opens and closes the node. Maybe you want to add it to the `+`/`-` icon. <br />`<icon onClick={handlers.toggle}>`
|
|
178
|
+
| edit | `() => void` | Makes this node editable. This will re-render the Node with the `state.isEditing` prop set to `true`.
|
|
179
|
+
| submit | `(update: string) => void` | Sends the update to the `onEdit` handler in the Tree component, and sets the `state.isEditing` prop to `false`.
|
|
180
|
+
| reset | `() => void` | Re-renders with the `state.isEditing` prop set to `false`.
|
|
181
|
+
|
|
182
|
+
### Tree Prop
|
|
183
|
+
|
|
184
|
+
The tree monitor provides methods to get the tree's internal state. A use case might be in a right click menu.
|
|
185
|
+
|
|
186
|
+
```jsx
|
|
187
|
+
// In your node renderer
|
|
188
|
+
onContextMenu={() => {
|
|
189
|
+
// Do something with all the selected nodes
|
|
190
|
+
tree.getSelectedIds()
|
|
191
|
+
}}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
| Methods | Returns | Description |
|
|
195
|
+
| ---- | ---- | ----------- |
|
|
196
|
+
| `getSelectedIds()` | `string[]` | Get the the ids of all currently selected nodes.
|
|
197
|
+
| `edit(id: string)` | `void` | Edit a node programatically.
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var $foSVk$reactdnd = require("react-dnd");
|
|
|
4
4
|
var $foSVk$reactdndhtml5backend = require("react-dnd-html5-backend");
|
|
5
5
|
var $foSVk$reactwindow = require("react-window");
|
|
6
6
|
var $foSVk$memoizeone = require("memoize-one");
|
|
7
|
+
var $foSVk$reactdom = require("react-dom");
|
|
7
8
|
|
|
8
9
|
function $parcel$export(e, n, v, s) {
|
|
9
10
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
@@ -685,12 +686,14 @@ function $d5c6a0d3e116bf17$export$79f9fa345a841d8b(root) {
|
|
|
685
686
|
|
|
686
687
|
|
|
687
688
|
|
|
689
|
+
|
|
688
690
|
class $f02bc7cefcb30793$export$e2da3477247342d1 {
|
|
689
691
|
constructor(dispatch, state, props, list){
|
|
690
692
|
this.dispatch = dispatch;
|
|
691
693
|
this.state = state;
|
|
692
694
|
this.props = props;
|
|
693
695
|
this.list = list;
|
|
696
|
+
this.edits = new Map();
|
|
694
697
|
}
|
|
695
698
|
assign(dispatch, state, props, list) {
|
|
696
699
|
this.dispatch = dispatch;
|
|
@@ -706,11 +709,43 @@ class $f02bc7cefcb30793$export$e2da3477247342d1 {
|
|
|
706
709
|
return this.state.selection.ids;
|
|
707
710
|
}
|
|
708
711
|
edit(id) {
|
|
709
|
-
|
|
712
|
+
const sid = id.toString();
|
|
713
|
+
this.resolveEdit(sid, {
|
|
714
|
+
cancelled: true
|
|
715
|
+
});
|
|
716
|
+
this.scrollToId(sid);
|
|
717
|
+
this.dispatch($da0693b023d53dfe$export$e324594224ef24da.edit(sid));
|
|
718
|
+
return new Promise((resolve)=>this.edits.set(sid, resolve)
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
submit(id, value) {
|
|
722
|
+
const sid = id.toString();
|
|
723
|
+
this.props.onEdit(sid, value);
|
|
724
|
+
this.dispatch($da0693b023d53dfe$export$e324594224ef24da.edit(null));
|
|
725
|
+
this.resolveEdit(sid, {
|
|
726
|
+
cancelled: false,
|
|
727
|
+
value: value
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
reset(id) {
|
|
731
|
+
const sid = id.toString();
|
|
732
|
+
this.dispatch($da0693b023d53dfe$export$e324594224ef24da.edit(null));
|
|
733
|
+
this.resolveEdit(sid, {
|
|
734
|
+
cancelled: true
|
|
735
|
+
});
|
|
710
736
|
}
|
|
711
|
-
|
|
737
|
+
resolveEdit(id, value) {
|
|
738
|
+
const resolve = this.edits.get(id.toString());
|
|
739
|
+
if (resolve) resolve(value);
|
|
740
|
+
this.edits.delete(id);
|
|
741
|
+
}
|
|
742
|
+
select(index, meta = false, shift = false) {
|
|
712
743
|
this.dispatch($da0693b023d53dfe$export$e324594224ef24da.select(index, meta, shift));
|
|
713
744
|
}
|
|
745
|
+
selectById(id, meta = false, shift = false) {
|
|
746
|
+
const index = this.idToIndex[id];
|
|
747
|
+
this.select(index, meta, shift);
|
|
748
|
+
}
|
|
714
749
|
selectUpwards(shiftKey) {
|
|
715
750
|
this.dispatch($da0693b023d53dfe$export$e324594224ef24da.stepUp(shiftKey, this.visibleIds));
|
|
716
751
|
}
|
|
@@ -731,11 +766,7 @@ class $f02bc7cefcb30793$export$e2da3477247342d1 {
|
|
|
731
766
|
if (index1) this.list.scrollToItem(index1, "start");
|
|
732
767
|
else {
|
|
733
768
|
this.openParents(id);
|
|
734
|
-
|
|
735
|
-
// But I've only tested it in the console and
|
|
736
|
-
// not in an event handler which will be batched...
|
|
737
|
-
// We may need to wrap this in a timeout or trigger an effect somehow
|
|
738
|
-
setTimeout(()=>{
|
|
769
|
+
($parcel$interopDefault($foSVk$reactdom)).flushSync(()=>{
|
|
739
770
|
const index = this.idToIndex[id];
|
|
740
771
|
if (index) this.list?.scrollToItem(index, "start");
|
|
741
772
|
});
|
|
@@ -1019,14 +1050,16 @@ const $8317ba945ccd0ce3$var$PreviewNode = /*#__PURE__*/ $foSVk$react.memo(functi
|
|
|
1019
1050
|
isDragging: false,
|
|
1020
1051
|
isEditing: false,
|
|
1021
1052
|
isSelected: false,
|
|
1022
|
-
|
|
1023
|
-
|
|
1053
|
+
isSelectedStart: false,
|
|
1054
|
+
isSelectedEnd: false,
|
|
1024
1055
|
isHoveringOverChild: false,
|
|
1025
1056
|
isOpen: node.isOpen
|
|
1026
1057
|
},
|
|
1027
1058
|
handlers: {
|
|
1028
|
-
edit: ()=>{
|
|
1029
|
-
|
|
1059
|
+
edit: ()=>Promise.resolve({
|
|
1060
|
+
cancelled: true
|
|
1061
|
+
})
|
|
1062
|
+
,
|
|
1030
1063
|
select: ()=>{
|
|
1031
1064
|
},
|
|
1032
1065
|
toggle: ()=>{
|
|
@@ -1170,8 +1203,8 @@ const $9a2860bfaab93091$export$b59bdbef9ce70de2 = /*#__PURE__*/ ($parcel$interop
|
|
|
1170
1203
|
return {
|
|
1171
1204
|
isEditing: isEditing,
|
|
1172
1205
|
isDragging: isDragging,
|
|
1173
|
-
|
|
1174
|
-
|
|
1206
|
+
isSelectedStart: isSelected && !prevSelected,
|
|
1207
|
+
isSelectedEnd: isSelected && !nextSelected,
|
|
1175
1208
|
isSelected: isSelected,
|
|
1176
1209
|
isHoveringOverChild: isHoveringOverChild,
|
|
1177
1210
|
isOpen: isOpen,
|
|
@@ -1187,10 +1220,6 @@ const $9a2860bfaab93091$export$b59bdbef9ce70de2 = /*#__PURE__*/ ($parcel$interop
|
|
|
1187
1220
|
isDragging,
|
|
1188
1221
|
isOverFolder,
|
|
1189
1222
|
]);
|
|
1190
|
-
if (isSelected) console.log({
|
|
1191
|
-
id: node.id,
|
|
1192
|
-
state: state
|
|
1193
|
-
});
|
|
1194
1223
|
const ref = $foSVk$react.useCallback((n)=>{
|
|
1195
1224
|
el.current = n;
|
|
1196
1225
|
dragRef(dropRef(n));
|
|
@@ -1212,25 +1241,23 @@ const $9a2860bfaab93091$export$b59bdbef9ce70de2 = /*#__PURE__*/ ($parcel$interop
|
|
|
1212
1241
|
]);
|
|
1213
1242
|
const handlers = $foSVk$react.useMemo(()=>{
|
|
1214
1243
|
return {
|
|
1215
|
-
select: (e,
|
|
1244
|
+
select: (e, args = {
|
|
1245
|
+
selectOnClick: true
|
|
1246
|
+
})=>{
|
|
1216
1247
|
if (node.rowIndex === null) return;
|
|
1217
|
-
if (selectOnClick || e.metaKey || e.shiftKey) tree.api.select(node.rowIndex, e.metaKey, e.shiftKey);
|
|
1248
|
+
if (args.selectOnClick || e.metaKey || e.shiftKey) tree.api.select(node.rowIndex, e.metaKey, e.shiftKey);
|
|
1218
1249
|
else tree.api.select(null, false, false);
|
|
1219
1250
|
},
|
|
1220
1251
|
toggle: (e)=>{
|
|
1221
1252
|
e.stopPropagation();
|
|
1222
1253
|
tree.onToggle(node.id, !node.isOpen);
|
|
1223
1254
|
},
|
|
1224
|
-
edit: ()=>
|
|
1225
|
-
|
|
1226
|
-
},
|
|
1255
|
+
edit: ()=>tree.api.edit(node.id)
|
|
1256
|
+
,
|
|
1227
1257
|
submit: (name)=>{
|
|
1228
|
-
|
|
1229
|
-
tree.api.edit(null);
|
|
1258
|
+
name.trim() ? tree.api.submit(node.id, name) : tree.api.reset(node.id);
|
|
1230
1259
|
},
|
|
1231
|
-
reset: ()=>
|
|
1232
|
-
tree.api.edit(null);
|
|
1233
|
-
}
|
|
1260
|
+
reset: ()=>tree.api.reset(node.id)
|
|
1234
1261
|
};
|
|
1235
1262
|
}, [
|
|
1236
1263
|
tree,
|