react-arborist 1.0.0 → 1.0.2

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 (2) hide show
  1. package/README.md +197 -0
  2. package/package.json +3 -1
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
+ ![Demo](https://user-images.githubusercontent.com/3460638/131920177-c47c34e5-d3e3-4826-937d-b366f527cdfe.gif)
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/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "react-arborist",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "license": "MIT",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/module.js",
8
8
  "types": "dist/index.d.ts",
9
+ "repository": "github:brimdata/react-arborist",
10
+ "homepage": "https://github.com/brimdata/react-arborist",
9
11
  "dependencies": {
10
12
  "memoize-one": "^6.0.0",
11
13
  "react-dnd": "^14.0.3",