1
1
# React Folder Tree
2
- A versatile and customizable react treeview library. It supports:
3
- - ✅ customize icons
4
- - ✅ customize event handlers
5
- - ✅ inline add, modify, and delete tree nodes
6
- - ✅ checkbox with half check (indeterminate check)
2
+ A versatile and customizable react treeview library. Features:
3
+ ✅ custom icons
4
+ ✅ custom event handlers
5
+ ✅ inline add, modify, and delete tree nodes
6
+ ✅ checkbox with half check (indeterminate check)
7
+ ✅ read-only mode
8
+
9
+ It uses [ use-tree-state] ( https://www.npmjs.com/package/use-tree-state ) hook internally for convenient state management.
7
10
### Quick Preview
8
11
![ folder-tree-demo] ( /assets/folder-tree-demo.gif )
9
12
@@ -20,9 +23,10 @@ $ npm install react-folder-tree --save
20
23
### 🌀 basic tree
21
24
``` tsx
22
25
import FolderTree , { testData } from ' react-folder-tree' ;
26
+ import ' react-folder-tree/dist/style.css' ;
23
27
24
28
const BasicTree = () => {
25
- const onTreeStateChange = state => console .log (' tree state: ' , state );
29
+ const onTreeStateChange = ( state , event ) => console .log (state , event );
26
30
27
31
return (
28
32
<FolderTree
@@ -34,20 +38,26 @@ const BasicTree = () => {
34
38
```
35
39
36
40
### 🌀 custom initial state
37
- tree state is an object that looks like:
41
+ Initial tree state is an object that describes a nested tree node structure, which looks like:
38
42
``` jsx
39
43
{
40
- // reserved keys
41
- name: ' Goku ' ,
44
+ // reserved keys, can customize initial value
45
+ name: ' root node ' ,
42
46
checked (optional): 0 (unchecked, default) | 0.5 (half checked) | 1 (checked),
43
- isOpen (optional): false (default) | true ,
47
+ isOpen (optional): true (default) | false ,
44
48
children (optional): [array of treenode],
45
49
46
- // not reserved
47
- key1 (optional): ' what ever data you need' ,
48
- url (optional): ' url of this node for example' ,
50
+ // internal keys, don't customize plz
51
+ path: [], // path is an array of indexes to this node from root node
52
+ _id: 0 ,
53
+
54
+ // not reserved, can carry any extra info about this node
55
+ nickname (optional): ' pikachu' ,
56
+ url (optional): ' url of this node' ,
49
57
}
50
58
```
59
+ ` checked ` and ` isOpen ` status could be auto initialized by props ` initCheckedStatus ` and ` initOpenStatus ` . We can also provide data with custom ` checked ` and ` isOpen ` status, and set ` initCheckedStatus ` and ` initOpenStatus ` to ` 'custom' ` .
60
+
51
61
This example shows how to render a tree with custom initial state
52
62
``` tsx
53
63
const treeState = {
@@ -93,6 +103,15 @@ const CustomInitState = () => (
93
103
/ >
94
104
```
95
105
106
+ ### 🌀 read only?
107
+ we can use it as a classical view-only tree
108
+ ``` jsx
109
+ < FolderTree
110
+ data= { treeState }
111
+ showCheckbox= { false } // hiding checkbox is not required but recommended for better UX
112
+ readOnly // <== here!!
113
+ / >
114
+ ```
96
115
---
97
116
## Advanced Usage
98
117
### 🔥 sync tree state
@@ -102,7 +121,7 @@ This example shows how to download all selected files.
102
121
``` jsx
103
122
const SuperApp = () => {
104
123
const [treeState , setTreeState ] = useState (initState);
105
- const onTreeStateChange = state => setTreeState (state);
124
+ const onTreeStateChange = ( state , event ) => setTreeState (state);
106
125
107
126
const onDownload = () => downloadAllSelected (treeState);
108
127
@@ -167,10 +186,25 @@ const BitcoinApp = () => {
167
186
};
168
187
```
169
188
170
- ### 🔥 disable icons
171
- This usage is a subset of custom icons. For example, to hide ` FileIcon ` we can simply pass in a dummy custom icon, so nothing will be rendered.
189
+ ### 🔥 hide icons / disable interaction
190
+ This usage is a subset of custom icons.
191
+
192
+ For example, if we want to disable editing, we can hide ` EditIcon ` by passing in a dummy custom icon, so nothing will be rendered.
172
193
``` tsx
173
- const FileIcon = (... args ) => null ;
194
+ const EditIcon = (... args ) => null ;
195
+ ```
196
+
197
+ A little more complex but more flexible way is to have extra node data, say ` editable ` , then build a custom icon that utilize this data
198
+ ``` ts
199
+ const EditIcon = ({ onClick : defaultOnClick , nodeData }) => {
200
+ const { editable } = nodeData ;
201
+
202
+ // if this node is editable, render an EditIcon, otherwise render air
203
+ return editable ? (<FaEdit onClick ={ defaultOnClick } />) : null ;
204
+
205
+ // or render a 'not editable' icon
206
+ return editable ? (<FaEdit onClick ={ defaultOnClick } />) : (<DontEdit />));
207
+ };
174
208
```
175
209
176
210
### 🔥 custom ` onClick ` for node names
@@ -182,7 +216,7 @@ const dataWithUrl = {
182
216
// ...
183
217
};
184
218
185
- const onNameClick = (defaultOnClick , nodeData ) => {
219
+ const onNameClick = ({ defaultOnClick , nodeData } ) => {
186
220
defaultOnClick ();
187
221
188
222
const {
@@ -209,13 +243,14 @@ const Downloader = () => (
209
243
| prop | description | type | options |
210
244
| -------------------| -----------------------------------------| ----------| ------------------------------------------------|
211
245
| data | initial tree state data (required) | object | N/A |
212
- | onChange | callback when tree state changes | function | console.log (default) |
213
246
| initCheckedStatus | initial check status of all nodes | string | 'unchecked' (default) \| 'checked' \| 'custom' |
214
- | initOpenStatus | initial open status of all treenodes | string | 'open' (default) \| 'close ' \| 'custom' |
247
+ | initOpenStatus | initial open status of all treenodes | string | 'open' (default) \| 'closed ' \| 'custom' |
215
248
| iconComponents | custom icon components | object | ant design icons (default) |
249
+ | onChange | callback when tree state changes | function | console.log (default) |
250
+ | onNameClick | callback when click treenode name | function | open treenode inline toolbox (default) |
216
251
| indentPixels | ident pixels of 1x level treenode | number | 30 (default) |
217
252
| showCheckbox | show check box? | bool | true (default) | false |
218
- | onNameClick | callback when click treenode name | function | open treenode inline toolbox (default) |
253
+ | readOnly | readOnly mode? can't click/check node | bool | false (default) | true |
219
254
220
255
---
221
256
## Bugs? Questions? Contributions?
0 commit comments