Skip to content

Commit 7a6d6a2

Browse files
committed
update README
1 parent 8b37c7f commit 7a6d6a2

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

README.md

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# React Folder Tree
22
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)
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]() hook internally for convenient state management.
710
### Quick Preview
811
![folder-tree-demo](/assets/folder-tree-demo.gif)
912

@@ -20,9 +23,10 @@ $ npm install react-folder-tree --save
2023
### 🌀 basic tree
2124
```tsx
2225
import FolderTree, { testData } from 'react-folder-tree';
26+
import 'react-folder-tree/dist/style.css';
2327

2428
const BasicTree = () => {
25-
const onTreeStateChange = state => console.log('tree state: ', state);
29+
const onTreeStateChange = (state, event) => console.log(state, event);
2630

2731
return (
2832
<FolderTree
@@ -34,20 +38,26 @@ const BasicTree = () => {
3438
```
3539

3640
### 🌀 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:
3842
```jsx
3943
{
40-
// reserved keys
41-
name: 'Goku',
44+
// reserved keys, can customize initial value
45+
name: 'root node',
4246
checked (optional): 0 (unchecked, default) | 0.5 (half checked) | 1(checked),
43-
isOpen (optional): false (default) | true,
47+
isOpen (optional): true (default) | false,
4448
children (optional): [array of treenode],
4549

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',
4957
}
5058
```
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+
5161
This example shows how to render a tree with custom initial state
5262
```tsx
5363
const treeState = {
@@ -93,6 +103,15 @@ const CustomInitState = () => (
93103
/>
94104
```
95105

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+
```
96115
---
97116
## Advanced Usage
98117
### 🔥 sync tree state
@@ -102,7 +121,7 @@ This example shows how to download all selected files.
102121
```jsx
103122
const SuperApp = () => {
104123
const [treeState, setTreeState] = useState(initState);
105-
const onTreeStateChange = state => setTreeState(state);
124+
const onTreeStateChange = (state, event) => setTreeState(state);
106125

107126
const onDownload = () => downloadAllSelected(treeState);
108127

@@ -167,10 +186,25 @@ const BitcoinApp = () => {
167186
};
168187
```
169188

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.
172193
```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+
};
174208
```
175209

176210
### 🔥 custom `onClick` for node names
@@ -209,13 +243,14 @@ const Downloader = () => (
209243
| prop | description | type | options |
210244
|-------------------|-----------------------------------------|----------|------------------------------------------------|
211245
| data | initial tree state data (required) | object | N/A |
212-
| onChange | callback when tree state changes | function | console.log (default) |
213246
| 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' |
215248
| 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) |
216251
| indentPixels | ident pixels of 1x level treenode | number | 30 (default) |
217252
| 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 |
219254

220255
---
221256
## Bugs? Questions? Contributions?

0 commit comments

Comments
 (0)