react-arborist 2.0.0-rc → 2.0.0-rc.1

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 ADDED
@@ -0,0 +1,683 @@
1
+ ![Logo](https://user-images.githubusercontent.com/3460638/161630636-3512fe81-41c2-4ee5-8f7e-adaad07033b6.svg)
2
+
3
+ <h1>React Arborist</h1>
4
+
5
+ The tree view is ubiquitous in software applications. This library provides the React ecosystem with complete solution to build the equivalent of the VSCode sidebar, the Mac Finder, the Windows Explorer, or the Sketch/Figma layers panel.
6
+
7
+ _New Link To Demo_
8
+
9
+ ## Features
10
+
11
+ - Drag and drop sorting
12
+ - Open/close folders
13
+ - Inline renaming
14
+ - Virtualized rendering
15
+ - Custom styling
16
+
17
+ **New Features in Version 2**
18
+
19
+ - Keyboard navigation
20
+ - Aria attributes
21
+ - Tree filtering
22
+ - Selection synchronization
23
+ - More callbacks (onScroll, onActivate, onSelect)
24
+ - Controlled or uncontrolled trees
25
+
26
+ _NEW DEMO GIF HERE_
27
+
28
+ > These docs are for version 2. It contains breaking changes. Here is the [v1.2.0 README](https://github.com/brimdata/react-arborist/tree/4fe9659d2c4cbd57582294330863d4fd7e7af74b).
29
+
30
+ ## Installation
31
+
32
+ ```
33
+ yarn add react-arborist
34
+ ```
35
+
36
+ ```
37
+ npm install react-arborist
38
+ ```
39
+
40
+ ## Examples
41
+
42
+ Assume our data is this:
43
+
44
+ ```js
45
+ const data = [
46
+ { id: "1", name: "Unread" },
47
+ { id: "2", name: "Threads" },
48
+ {
49
+ id: "3",
50
+ name: "Chat Rooms",
51
+ children: [
52
+ { id: "c1", name: "General" },
53
+ { id: "c2", name: "Random" },
54
+ { id: "c3", name: "Open Source Projects" },
55
+ ],
56
+ },
57
+ {
58
+ id: "4",
59
+ name: "Direct Messages",
60
+ children: [
61
+ { id: "d1", name: "Alice" },
62
+ { id: "d2", name: "Bob" },
63
+ { id: "d3", name: "Charlie" },
64
+ ],
65
+ },
66
+ ];
67
+ ```
68
+
69
+ ### The Simplest Tree
70
+
71
+ Use all the defaults. The _initialData_ prop makes the tree an uncontrolled component. Create, move, rename, and delete will be handled internally.
72
+
73
+ ```jsx
74
+ function App() {
75
+ return <Tree initialData={data} />;
76
+ }
77
+ ```
78
+
79
+ ### Customize the Appearance
80
+
81
+ We provide our own dimensions and our own `Node` component.
82
+
83
+ ```jsx
84
+ function App() {
85
+ return (
86
+ <Tree
87
+ initialData={data}
88
+ openByDefault={false}
89
+ width={600}
90
+ height={1000}
91
+ indent={24}
92
+ rowHeight={36}
93
+ paddingTop={30}
94
+ paddingBottom={10}
95
+ padding={25 /* sets both */}
96
+ >
97
+ {Node}
98
+ </Tree>
99
+ );
100
+ }
101
+
102
+ function Node({ node, style, dragHandle }) {
103
+ /* This node instance can do many things. See the API reference. */
104
+ return (
105
+ <div style={style} ref={dragHandle}>
106
+ {node.isLeaf ? "🍁" : "🗀"}
107
+ {node.data.name}
108
+ </div>
109
+ );
110
+ }
111
+ ```
112
+
113
+ ### Control the Tree data
114
+
115
+ Here we use the _data_ prop to make the tree a controlled component. We must handle all the data modifications ourselves using the props below.
116
+
117
+ ```jsx
118
+ function App() {
119
+ /* Handle the data modifications outside the tree component */
120
+ const onCreate = ({ parentId, index, type }) => {};
121
+ const onRename = ({ id, name }) => {};
122
+ const onMove = ({ dragIds, parentId, index }) => {};
123
+ const onDelete = ({ ids }) => {};
124
+
125
+ return (
126
+ <Tree
127
+ data={data}
128
+ onCreate={onCreate}
129
+ onRename={onRename}
130
+ onMove={onMove}
131
+ onDelete={onDelete}
132
+ />
133
+ );
134
+ }
135
+ ```
136
+
137
+ ### Tree Filtering
138
+
139
+ Providing a non-empty _searchTerm_ will only show nodes that match. If a child matches, all its parents also match. Internal nodes are opened when filtering. You can provide your own _searchMatch_ function, or use the default.
140
+
141
+ ```jsx
142
+ function App() {
143
+ const term = useSearchTermString()
144
+ <Tree
145
+ data={data}
146
+ searchTerm={term}
147
+ searchMatch={
148
+ (node, term) => node.data.name.toLowerCase().includes(term.toLowerCase())
149
+ }
150
+ />
151
+ }
152
+ ```
153
+
154
+ ### Sync the Selection
155
+
156
+ It's common to open something elsewhere in the app, but have the tree reflect the new selection.
157
+
158
+ Passing an id to the _selection_ prop will select and scroll to that node whenever that id changes.
159
+
160
+ ```jsx
161
+ function App() {
162
+ const chatId = useCurrentChatId();
163
+
164
+ /*
165
+ Whenever the currentChatRoomId changes,
166
+ the tree will automatically select it and scroll to it.
167
+ */
168
+
169
+ return <Tree initialData={data} selection={chatId} />;
170
+ }
171
+ ```
172
+
173
+ ### Use the Tree Api Instance
174
+
175
+ You can access the Tree Api in the parent component by giving a ref to the tree.
176
+
177
+ ```jsx
178
+ function App() {
179
+ const treeRef = useRef();
180
+
181
+ useEffect(() => {
182
+ const tree = treeRef.current;
183
+ tree.selectAll();
184
+ /* See the Tree API reference for all you can do with it. */
185
+ }, []);
186
+
187
+ return <Tree initialData={data} ref={treeRef} />;
188
+ }
189
+ ```
190
+
191
+ ### Data with Different Property Names
192
+
193
+ The _idAccessor_ and _childrenAccessor_ props allow you to specify the children and id fields in your data.
194
+
195
+ ```jsx
196
+ function App() {
197
+ const data = [
198
+ {
199
+ category: "Food",
200
+ subCategories: [{ category: "Restaurants" }, { category: "Groceries" }],
201
+ },
202
+ ];
203
+ return (
204
+ <Tree
205
+ data={data}
206
+ /* An accessor can provide a string property name */
207
+ idAccessor="category"
208
+ /* or a function with the data as the argument */
209
+ childrenAccessor={(d) => d.subCategories}
210
+ />
211
+ );
212
+ }
213
+ ```
214
+
215
+ ### Custom Rendering
216
+
217
+ Render every single piece of the tree yourself. See the API reference for the props passed to each renderer.
218
+
219
+ ```jsx
220
+ function App() {
221
+ return (
222
+ <Tree
223
+ data={data}
224
+ /* The outer most element in the list */
225
+ renderRow={MyRow}
226
+ /* The "ghost" element that follows the mouse as you drag */
227
+ renderDragPreview={MyDragPreview}
228
+ /* The line that shows where an element will be dropped */
229
+ renderCursor={MyCursor}
230
+ >
231
+ {/* The inner element that shows the indentation and data */}
232
+ {MyNode}
233
+ </Tree>
234
+ );
235
+ }
236
+ ```
237
+
238
+ ## API Reference
239
+
240
+ - Components
241
+ - [Tree Component Props](#tree-component-props)
242
+ - [Row Component Props](#row-component-props)
243
+ - [Node Component Props](#node-component-props)
244
+ - [DragPreview Component Props](#dragpreview-component-props)
245
+ - [Cursor Component Props](#cursor-component-props)
246
+ - Interfaces
247
+ - [Node API](#node-api-reference)
248
+ - [Tree API](#tree-api-reference)
249
+
250
+ ## Tree Component Props
251
+
252
+ These are all the props you can pass to the Tree component.
253
+
254
+ ```ts
255
+ interface TreeProps<T extends IdObj> {
256
+ /* Data Options */
257
+ data?: T[];
258
+ initialData?: T[];
259
+
260
+ /* Data Handlers */
261
+ onCreate?: handlers.CreateHandler;
262
+ onMove?: handlers.MoveHandler;
263
+ onRename?: handlers.RenameHandler;
264
+ onDelete?: handlers.DeleteHandler;
265
+
266
+ /* Renderers*/
267
+ children?: ElementType<renderers.NodeRendererProps<T>>;
268
+ renderRow?: ElementType<renderers.RowRendererProps<T>>;
269
+ renderDragPreview?: ElementType<renderers.DragPreviewProps>;
270
+ renderCursor?: ElementType<renderers.DropCursorProps>;
271
+ renderContainer?: ElementType<{}>;
272
+
273
+ /* Sizes */
274
+ rowHeight?: number;
275
+ width?: number;
276
+ height?: number;
277
+ indent?: number;
278
+ paddingTop?: number;
279
+ paddingBottom?: number;
280
+ padding?: number;
281
+
282
+ /* Config */
283
+ openByDefault?: boolean;
284
+ selectionFollowsFocus?: boolean;
285
+ disableDrag?: string | boolean | BoolFunc<T>;
286
+ disableDrop?: string | boolean | BoolFunc<T>;
287
+ childrenAccessor?: string | ((d: T) => T[]);
288
+ idAccessor?: string | ((d: T) => string);
289
+
290
+ /* Event Handlers */
291
+ onActivate?: (node: NodeApi<T>) => void;
292
+ onSelect?: (nodes: NodeApi<T>[]) => void;
293
+ onScroll?: (props: ListOnScrollProps) => void;
294
+
295
+ /* Selection */
296
+ selection?: string;
297
+
298
+ /* Open State */
299
+ initialOpenState?: OpenMap;
300
+
301
+ /* Search */
302
+ searchTerm?: string;
303
+ searchMatch?: (node: NodeApi<T>, searchTerm: string) => boolean;
304
+
305
+ /* Extra */
306
+ className?: string | undefined;
307
+ dndRootElement?: globalThis.Node | null;
308
+ onClick?: MouseEventHandler;
309
+ onContextMenu?: MouseEventHandler;
310
+ }
311
+ ```
312
+
313
+ ## Row Component Props
314
+
315
+ The _\<RowRenderer\>_ is responsible for attaching the drop ref, the row style (top, height) and the aria-attributes. The default should work fine for most use cases, but it can be replaced by your own component if you need. See the _renderRow_ prop in the _\<Tree\>_ component.
316
+
317
+ ```ts
318
+ type RowRendererProps<T extends IdObj> = {
319
+ node: NodeApi<T>;
320
+ innerRef: (el: HTMLDivElement | null) => void;
321
+ attrs: HTMLAttributes<any>;
322
+ children: ReactElement;
323
+ };
324
+ ```
325
+
326
+ ## Node Component Props
327
+
328
+ The _\<NodeRenderer\>_ is responsible for attaching the drag ref, the node style (padding for indentation), the visual look of the node, the edit input of the node, and anything else you can dream up.
329
+
330
+ There is a default renderer, but it's only there as a placeholder to get started. You'll wan't to create your own component for this. It is passed as the _\<Tree\>_ components only child.
331
+
332
+ ```ts
333
+ export type NodeRendererProps<T extends IdObj> = {
334
+ style: CSSProperties;
335
+ node: NodeApi<T>;
336
+ tree: TreeApi<T>;
337
+ dragHandle?: (el: HTMLDivElement | null) => void;
338
+ preview?: boolean;
339
+ };
340
+ ```
341
+
342
+ ## DragPreview Component Props
343
+
344
+ The _\<DragPreview\>_ is responsible for showing a "ghost" version of the node being dragged. The default is a semi-transparent version of the NodeRenderer and should work fine for most people. To customize it, pass your new component to the _renderDragPreview_ prop.
345
+
346
+ ```ts
347
+ type DragPreviewProps = {
348
+ offset: XYCoord | null;
349
+ mouse: XYCoord | null;
350
+ id: string | null;
351
+ dragIds: string[];
352
+ isDragging: boolean;
353
+ };
354
+ ```
355
+
356
+ ## Cursor Component Props
357
+
358
+ The _\<Cursor\>_ is responsible for showing a line that indicates where the node will move to when it's dropped. The default is a blue line with circle on the left side. You may want to customize this. Pass your own component to the _renderCursor_ prop.
359
+
360
+ ```ts
361
+ export type DropCursorProps = {
362
+ top: number;
363
+ left: number;
364
+ indent: number;
365
+ };
366
+ ```
367
+
368
+ ## Node API Reference
369
+
370
+ #### State Properties
371
+
372
+ All these properties on the node instance return booleans related to the state of the node.
373
+
374
+ _node_.**isRoot**
375
+
376
+ Returns true if this is the root node. The root node is added internally by react-arborist and not shown in the UI.
377
+
378
+ _node_.**isLeaf**
379
+
380
+ Returns true if the children property is not an array.
381
+
382
+ _node_.**isInternal**
383
+
384
+ Returns true if the children property is an array.
385
+
386
+ _node_.**isOpen**
387
+
388
+ Returns true if node is internal and in an open state.
389
+
390
+ _node_.**isEditing**
391
+
392
+ Returns true if this node is currently being edited. Use this property in the NodeRenderer to render the rename form.
393
+
394
+ _node_.**isSelected**
395
+
396
+ Returns true if node is selected.
397
+
398
+ _node_.**isSelectedStart**
399
+
400
+ Returns true if node is the first of a contiguous group of selected nodes. Useful for styling.
401
+
402
+ _node_.**isSelectedEnd**
403
+
404
+ Returns true if node is the last of a contiguous group of selected nodes. Useful for styling.
405
+
406
+ _node_.**isFocused**
407
+
408
+ Returns true if node is focused.
409
+
410
+ _node_.**isDragging**
411
+
412
+ Returns true if node is being dragged.
413
+
414
+ _node_.**willReceiveDrop**
415
+
416
+ Returns true if node is internal and the user is hovering a dragged node over it.
417
+
418
+ _node_.**state**
419
+
420
+ Returns an object with all the above properties as keys and boolean values. Useful for adding class names to an element with a library like [clsx](https://github.com/lukeed/clsx) or [classnames](https://github.com/JedWatson/classnames).
421
+
422
+ ```ts
423
+ type NodeState = {
424
+ isEditing: boolean;
425
+ isDragging: boolean;
426
+ isSelected: boolean;
427
+ isSelectedStart: boolean;
428
+ isSelectedEnd: boolean;
429
+ isFocused: boolean;
430
+ isOpen: boolean;
431
+ willReceiveDrop: boolean;
432
+ };
433
+ ```
434
+
435
+ #### Accessors
436
+
437
+ _node_.**childIndex**
438
+
439
+ Returns the node's index in relation to its siblings.
440
+
441
+ _node_.**next**
442
+
443
+ Returns the next visible node. The node directly under this node in the tree component. Returns null if none exist.
444
+
445
+ _node_.**prev**
446
+
447
+ Returns the previous visible node. The node directly above this node in the tree component. Returns null if none exist.
448
+
449
+ _node_.**nextSibling**
450
+
451
+ Returns the next sibling in the data of this node. Returns null if none exist.
452
+
453
+ #### Selection Methods
454
+
455
+ _node_.**select**()
456
+
457
+ Select only this node.
458
+
459
+ _node_.**deselect**()
460
+
461
+ Deselect this node. Other nodes may still be selected.
462
+
463
+ _node_.**selectMulti**()
464
+
465
+ Select this node while maintaining all other selections.
466
+
467
+ _node_.**selectContiguous**()
468
+
469
+ Deselect all nodes from the anchor node to the last selected node, the select all nodes from the anchor node to this node. The anchor changes to the focused node after calling _select()_ or _selectMulti()_.
470
+
471
+ #### Activation Methods
472
+
473
+ _node_.**activate**()
474
+
475
+ Runs the Tree props' onActivate callback passing in this node.
476
+
477
+ _node_.**focus**()
478
+
479
+ Focus this node.
480
+
481
+ #### Open/Close Methods
482
+
483
+ _node_.**open**()
484
+
485
+ Opens the node if it is an internal node.
486
+
487
+ _node_.**close**()
488
+
489
+ Closes the node if it is an internal node.
490
+
491
+ _node_.**toggle**()
492
+
493
+ Toggles the open/closed state of the node if it is an internal node.
494
+
495
+ _node_.**openParents**()
496
+
497
+ Opens all the parents of this node.
498
+
499
+ _node_.**edit**()
500
+
501
+ Moves this node into the editing state. Calling node._isEditing_ will return true.
502
+
503
+ _node_.**submit**(_newName_)
504
+
505
+ Submits _newName_ string to the _onRename_ handler. Moves this node out of the editing state.
506
+
507
+ _node_.**reset**()
508
+
509
+ Moves this node out of the editing state without submitting a new name.
510
+
511
+ #### Event Handlers
512
+
513
+ _node_.**handleClick**(_event_)
514
+
515
+ Useful for using the standard selection methods when a node is clicked. If the meta key is down, call _multiSelect()_. If the shift key is down, call _selectContiguous()_. Otherwise, call _select()_ and _activate()_.
516
+
517
+ ## Tree API Reference
518
+
519
+ The tree api reference is stable across re-renders. It always has the most recent state and props.
520
+
521
+ #### Node Accessors
522
+
523
+ _tree_.**get**(_id_) : _NodeApi | null_
524
+
525
+ Get node by id from the _visibleNodes_ array.
526
+
527
+ _tree_.**at**(_index_) : _NodeApi | null_
528
+
529
+ Get node by index from the _visibleNodes_ array.
530
+
531
+ _tree_.**visibleNodes** : _NodeApi[]_
532
+
533
+ Returns an array of the visible nodes.
534
+
535
+ _tree_.**firstNode** : _NodeApi | null_
536
+
537
+ The first node in the _visibleNodes_ array.
538
+
539
+ _tree_.**lastNode** : _NodeApi | null_
540
+
541
+ The last node in the _visibleNodes_ array.
542
+
543
+ _tree_.**focusedNode** : _NodeApi | null_
544
+
545
+ The currently focused node.
546
+
547
+ _tree_.**mostRecentNode** : _NodeApi | null_
548
+
549
+ The most recently selected node.
550
+
551
+ _tree_.**nextNode** : _NodeApi | null_
552
+
553
+ The node directly after the _focusedNode_ in the _visibleNodes_ array.
554
+
555
+ _tree_.**prevNode** : _NodeApi | null_
556
+
557
+ The node directly before the _focusedNode_ in the _visibleNodes_ array.
558
+
559
+ #### Focus Methods
560
+
561
+ _tree_.**hasFocus** : _boolean_
562
+
563
+ Returns true if the the tree has focus somewhere within it.
564
+
565
+ _tree_.**focus**(_id_)
566
+
567
+ Focus on the node with _id_.
568
+
569
+ _tree_.**isFocused**(_id_) : _boolean_
570
+
571
+ Check if the node with _id_ is focused.
572
+
573
+ _tree_.**pageUp**()
574
+
575
+ Move focus up one page.
576
+
577
+ _tree_.**pageDown**()
578
+
579
+ Move focus down one page.
580
+
581
+ #### Selection Methods
582
+
583
+ _tree_.**selectedIds** : _Set\<string\>_
584
+
585
+ Returns a set of ids that are selected.
586
+
587
+ _tree_.**selectedNodes** : _NodeApi[]_
588
+
589
+ Returns an array of nodes that are selected.
590
+
591
+ _tree_.**isSelected**(_id_) : _boolean_
592
+
593
+ Returns true if the node with _id_ is selected.
594
+
595
+ _tree_.**select**(_id_)
596
+
597
+ Select only the node with _id_.
598
+
599
+ _tree_.**deselect**(_id_)
600
+
601
+ Deselect the node with _id_.
602
+
603
+ _tree_.**selectMulti**(_id_)
604
+
605
+ Add to the selection the node with _id_.
606
+
607
+ _tree_.**selectContiguous**(_id_)
608
+
609
+ Deselected nodes between the anchor and the last selected node, then select the nodes between the anchor and the node with _id_.
610
+
611
+ _tree_.**deselectAll**()
612
+
613
+ Deselect all nodes.
614
+
615
+ _tree_.**selectAll**()
616
+
617
+ Select all nodes.
618
+
619
+ #### Visibility
620
+
621
+ _tree_.**open**(_id_)
622
+
623
+ Open the node with _id_.
624
+
625
+ _tree_.**close**(_id_)
626
+
627
+ Close the node with _id_.
628
+
629
+ _tree_.**toggle**(_id_)
630
+
631
+ Toggle the open state of the node with _id_.
632
+
633
+ _tree_.**openParents**(_id_)
634
+
635
+ Open all parents of the node with _id_.
636
+
637
+ _tree_.**openSiblings**(_id_)
638
+
639
+ Open all siblings of the node with _id_.
640
+
641
+ _tree_.**isOpen**(_id_) : _boolean_
642
+
643
+ Returns true if the node with _id_ is open.
644
+
645
+ #### Drag and Drop
646
+
647
+ _tree_.**isDragging**(_id_) : _boolean_
648
+
649
+ Returns true if the node with _id_ is being dragged.
650
+
651
+ _tree_.**willReceiveDrop**(_id_) : _boolean_
652
+
653
+ Returns true if the node with _id_ is internal and is under the dragged node.
654
+
655
+ #### Scrolling
656
+
657
+ _tree_.**scrollTo**(_id_, _[align]_)
658
+
659
+ Scroll to the node with _id_. If this node is not visible, this method will open all its parents. The align argument can be _"auto" | "smart" | "center" | "end" | "start"_.
660
+
661
+ #### Properties
662
+
663
+ _tree_.**isEditing** : _boolean_
664
+
665
+ Returns true if the tree is editing a node.
666
+
667
+ _tree_.**isFiltered** : _boolean_
668
+
669
+ Returns true if the _searchTerm_ prop is not an empty string when trimmed.
670
+
671
+ _tree_.**props** : _TreeProps_
672
+
673
+ Returns all the props that were passed to the _\<Tree\>_ component.
674
+
675
+ _tree_.**root** : _NodeApi_
676
+
677
+ Returns the root _NodeApi_ instance. Its children are the Node representations of the _data_ prop array.
678
+
679
+ ## Author
680
+
681
+ This library was created by James Kerr while working at Brim Data on the [Zui desktop app](https://www.youtube.com/watch?v=I2y663n8d2A). Work with data? Check us out at [brimdata.io](https://www.brimdata.io)
682
+
683
+ [Follow me on Twitter](https://twitter.com/specialCaseDev) for react-arborist updates.
@@ -1,2 +1,7 @@
1
1
  /// <reference types="react" />
2
+ /**
3
+ * All these keyboard shortcuts seem like they should be configurable.
4
+ * Each operation should be a given a name and separated from
5
+ * the event handler. Future clean up welcome.
6
+ */
2
7
  export declare function DefaultContainer(): JSX.Element;
@@ -1,4 +1,4 @@
1
1
  /// <reference types="react" />
2
2
  import { NodeRendererProps } from "../types/renderers";
3
3
  import { IdObj } from "../types/utils";
4
- export declare function DefaultNode<T extends IdObj>({ style, node, dragHandle, }: NodeRendererProps<T>): JSX.Element;
4
+ export declare function DefaultNode<T extends IdObj>(props: NodeRendererProps<T>): JSX.Element;