react-arborist 1.1.0 → 2.0.0-rc
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/dist/components/{drop-cursor.d.ts → cursor.d.ts} +0 -0
- package/dist/components/default-container.d.ts +2 -0
- package/dist/components/default-cursor.d.ts +3 -0
- package/dist/components/default-drag-preview.d.ts +3 -0
- package/dist/components/default-node.d.ts +4 -0
- package/dist/components/default-row.d.ts +4 -0
- package/dist/components/drag-preview-container.d.ts +2 -0
- package/dist/components/list-inner-element.d.ts +2 -0
- package/dist/components/list-outer-element.d.ts +2 -0
- package/dist/components/outer-drop.d.ts +4 -0
- package/dist/components/provider.d.ts +11 -0
- package/dist/components/row-container.d.ts +8 -0
- package/dist/components/tree-container.d.ts +2 -0
- package/dist/components/tree.d.ts +5 -4
- package/dist/context.d.ts +23 -18
- package/dist/data/create-index.d.ts +5 -0
- package/dist/data/create-list.d.ts +4 -0
- package/dist/data/create-root.d.ts +5 -0
- package/dist/data/flatten-tree.d.ts +4 -2
- package/dist/data/simple-tree.d.ts +43 -0
- package/dist/dnd/compute-drop.d.ts +4 -4
- package/dist/dnd/drag-hook.d.ts +3 -4
- package/dist/dnd/drop-hook.d.ts +2 -3
- package/dist/hooks/use-fresh-node.d.ts +2 -0
- package/dist/hooks/use-simple-tree.d.ts +13 -0
- package/dist/hooks/use-uncontrolled-tree.d.ts +24 -0
- package/dist/hooks/use-validated-props.d.ts +3 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.js +2093 -1184
- package/dist/index.js.map +1 -1
- package/dist/interfaces/node-api.d.ts +67 -0
- package/dist/interfaces/tree-api.d.ts +112 -0
- package/dist/module.js +2082 -1192
- package/dist/module.js.map +1 -1
- package/dist/state/dnd-slice.d.ts +20 -0
- package/dist/state/drag-slice.d.ts +7 -0
- package/dist/state/edit-slice.d.ts +8 -0
- package/dist/state/focus-slice.d.ts +12 -0
- package/dist/state/initial.d.ts +3 -0
- package/dist/state/open-slice.d.ts +30 -0
- package/dist/state/root-reducer.d.ts +13 -0
- package/dist/state/selection-slice.d.ts +36 -0
- package/dist/types/dnd.d.ts +9 -0
- package/dist/types/handlers.d.ts +24 -0
- package/dist/types/renderers.d.ts +30 -0
- package/dist/types/state.d.ts +2 -0
- package/dist/types/tree-props.d.ts +43 -0
- package/dist/types/utils.d.ts +21 -0
- package/dist/utils/props.d.ts +3 -0
- package/dist/utils.d.ts +15 -6
- package/package.json +10 -7
- package/src/components/cursor.tsx +15 -0
- package/src/components/default-container.tsx +229 -0
- package/src/components/{drop-cursor.tsx → default-cursor.tsx} +15 -20
- package/src/components/default-drag-preview.tsx +92 -0
- package/src/components/default-node.tsx +15 -0
- package/src/components/default-row.tsx +21 -0
- package/src/components/drag-preview-container.tsx +26 -0
- package/src/components/list-inner-element.tsx +22 -0
- package/src/components/list-outer-element.tsx +45 -0
- package/src/components/outer-drop.ts +7 -0
- package/src/components/provider.tsx +97 -0
- package/src/components/row-container.tsx +82 -0
- package/src/components/tree-container.tsx +13 -0
- package/src/components/tree.tsx +17 -126
- package/src/context.ts +36 -0
- package/src/data/create-index.ts +9 -0
- package/src/data/create-list.ts +56 -0
- package/src/data/create-root.ts +53 -0
- package/src/data/simple-tree.ts +103 -0
- package/src/dnd/compute-drop.ts +16 -16
- package/src/dnd/drag-hook.ts +28 -23
- package/src/dnd/drop-hook.ts +35 -21
- package/src/dnd/outer-drop-hook.ts +6 -6
- package/src/hooks/use-fresh-node.ts +16 -0
- package/src/hooks/use-simple-tree.ts +55 -0
- package/src/hooks/use-validated-props.ts +35 -0
- package/src/index.ts +9 -5
- package/src/interfaces/node-api.ts +187 -0
- package/src/interfaces/tree-api.ts +552 -0
- package/src/state/dnd-slice.ts +36 -0
- package/src/state/drag-slice.ts +31 -0
- package/src/state/edit-slice.ts +19 -0
- package/src/state/focus-slice.ts +28 -0
- package/src/state/initial.ts +14 -0
- package/src/state/open-slice.ts +53 -0
- package/src/state/root-reducer.ts +21 -0
- package/src/state/selection-slice.ts +75 -0
- package/src/types/dnd.ts +10 -0
- package/src/types/handlers.ts +24 -0
- package/src/types/renderers.ts +34 -0
- package/src/types/state.ts +3 -0
- package/src/types/tree-props.ts +63 -0
- package/src/types/utils.ts +26 -0
- package/src/utils/props.ts +8 -0
- package/src/utils.ts +125 -11
- package/README.md +0 -220
- package/dist/components/preview.d.ts +0 -2
- package/dist/components/row.d.ts +0 -7
- package/dist/data/enrich-tree.d.ts +0 -2
- package/dist/provider.d.ts +0 -3
- package/dist/reducer.d.ts +0 -46
- package/dist/selection/range.d.ts +0 -13
- package/dist/selection/selection-hook.d.ts +0 -3
- package/dist/selection/selection.d.ts +0 -33
- package/dist/tree-api-hook.d.ts +0 -6
- package/dist/tree-api.d.ts +0 -34
- package/dist/types.d.ts +0 -131
- package/src/components/preview.tsx +0 -108
- package/src/components/row.tsx +0 -114
- package/src/context.tsx +0 -52
- package/src/data/enrich-tree.ts +0 -74
- package/src/data/flatten-tree.ts +0 -17
- package/src/provider.tsx +0 -61
- package/src/reducer.ts +0 -161
- package/src/selection/range.ts +0 -41
- package/src/selection/selection-hook.ts +0 -24
- package/src/selection/selection.test.ts +0 -111
- package/src/selection/selection.ts +0 -186
- package/src/tree-api-hook.ts +0 -34
- package/src/tree-api.ts +0 -156
- package/src/types.ts +0 -155
package/README.md
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-

|
|
2
|
-
|
|
3
|
-
<h1>React Arborist</h1>
|
|
4
|
-
|
|
5
|
-
A full-featured tree component for React.
|
|
6
|
-
|
|
7
|
-
Demo: https://react-arborist.netlify.app/
|
|
8
|
-
|
|
9
|
-
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.
|
|
10
|
-
|
|
11
|
-
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.
|
|
12
|
-
|
|
13
|
-

|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
yarn add react-arborist
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
```
|
|
22
|
-
npm install react-arborist
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Example
|
|
26
|
-
|
|
27
|
-
Render the tree data structure.
|
|
28
|
-
|
|
29
|
-
```jsx
|
|
30
|
-
const data = {
|
|
31
|
-
id: "A",
|
|
32
|
-
name: "Root"
|
|
33
|
-
children: [
|
|
34
|
-
{ id: "B", name: "Node 1" },
|
|
35
|
-
{ id: "C", name: "Node 2" },
|
|
36
|
-
],
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
function App() {
|
|
40
|
-
return <Tree data={data}>{Node}</Tree>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function Node({ ref, styles, data }) {
|
|
44
|
-
return (
|
|
45
|
-
<div ref={ref} style={styles.row}>
|
|
46
|
-
<div style={styles.indent}>{data.name}</div>
|
|
47
|
-
</div>
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
#### Contents
|
|
53
|
-
|
|
54
|
-
- [Expected Data Structure](#expected-data-structure)
|
|
55
|
-
- [Tree Component API](#tree-component)
|
|
56
|
-
- [Node Renderer API](#node-renderer-component)
|
|
57
|
-
- [Styles Prop](#styles-prop)
|
|
58
|
-
- [State Prop](#state-prop)
|
|
59
|
-
- [Handlers Prop](#handlers-prop)
|
|
60
|
-
- [Tree Prop](#tree-prop)
|
|
61
|
-
- [Accessing the Tree Api from the Parent](#accessing-the-tree-api-from-the-parent)
|
|
62
|
-
|
|
63
|
-
## Expected Data Structure
|
|
64
|
-
|
|
65
|
-
The Tree component expects the data prop to be a tree-like data structure with the following type:
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
type Data = {
|
|
69
|
-
id: string, /* Required */
|
|
70
|
-
children?: Data[]
|
|
71
|
-
isOpen?: boolean
|
|
72
|
-
...rest /* Any other data you wish to provide */
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
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.
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
<Tree childrenAccessor="items" ...
|
|
80
|
-
// Or
|
|
81
|
-
<Tree childrenAccessor={(node) => node.items} ...
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
## Tree Component
|
|
85
|
-
|
|
86
|
-
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.
|
|
87
|
-
|
|
88
|
-
| Prop | Default | Description |
|
|
89
|
-
| ---------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
90
|
-
| data | (required) | The tree data structure to render as described above. |
|
|
91
|
-
| width | 300 | The width of the tree. |
|
|
92
|
-
| 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. |
|
|
93
|
-
| rowHeight | 24 | The height of each row. |
|
|
94
|
-
| indent | 24 | The number of pixels to indent child nodes. |
|
|
95
|
-
| hideRoot | false | Hide the root node so that the first set of children appear as the roots. |
|
|
96
|
-
| 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. |
|
|
97
|
-
| onMove | noop | Handler called when a user moves one or more nodes by dragging and dropping. |
|
|
98
|
-
| onEdit | noop | Handler called when a user performs an inline edit of the node. |
|
|
99
|
-
| childrenAccessor | "children" | Used to get a node's children if they exist on a property other than "children". |
|
|
100
|
-
| isOpenAccessor | "isOpen" | Used to get a node's openness state if it exists on a property other than "isOpen". |
|
|
101
|
-
| openByDefault | true | Choose if the node should be open or closed when it has an undefined openness state. |
|
|
102
|
-
| className | undefined | Adds a class to the containing div. |
|
|
103
|
-
| dndRootElement | undefined | The element for react-dnd to bind it's events to. Defaults to window. See https://github.com/brimdata/react-arborist/pull/33 |
|
|
104
|
-
|
|
105
|
-
The only child of the Tree Component must be a NodeRenderer function as described below.
|
|
106
|
-
|
|
107
|
-
```jsx
|
|
108
|
-
const NodeRenderer = ({
|
|
109
|
-
innerRef, data, styles, handlers, state, tree
|
|
110
|
-
}) => ...
|
|
111
|
-
|
|
112
|
-
const MyApp = () =>
|
|
113
|
-
<Tree>
|
|
114
|
-
{NodeRenderer}
|
|
115
|
-
</Tree>
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
## Node Renderer Component
|
|
119
|
-
|
|
120
|
-
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.
|
|
121
|
-
|
|
122
|
-
The most basic node renderer will look like this:
|
|
123
|
-
|
|
124
|
-
```jsx
|
|
125
|
-
function NodeRenderer({ innerRef, styles, data, state, handlers, tree }) {
|
|
126
|
-
return (
|
|
127
|
-
<div ref={innerRef} style={styles.row}>
|
|
128
|
-
<div style={styles.indent}>{data.id}</div>
|
|
129
|
-
</div>
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
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.
|
|
135
|
-
|
|
136
|
-
| Prop | Type | Description |
|
|
137
|
-
| -------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
138
|
-
| data | Node | A single node from the tree data structure provided. |
|
|
139
|
-
| innerRef | Ref | Must be attached to the root element returned by the NodeRenderer. This is needed for drag and drop to work. |
|
|
140
|
-
| [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. |
|
|
141
|
-
| [state](#state-prop) | object | An handful of boolean values that indicate the current state of this node. See below for details. |
|
|
142
|
-
| [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. |
|
|
143
|
-
| [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. |
|
|
144
|
-
|
|
145
|
-
### Styles Prop
|
|
146
|
-
|
|
147
|
-
These are the properties on the styles object passed to the NodeRenderer.
|
|
148
|
-
|
|
149
|
-
| Name | Type | Description |
|
|
150
|
-
| ------ | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
151
|
-
| 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. |
|
|
152
|
-
| indent | CSSProperties | This is simply a left padding set to the level of the tree multiplied by the tree indent prop. |
|
|
153
|
-
|
|
154
|
-
### State Prop
|
|
155
|
-
|
|
156
|
-
These are the properties on the state object passed to the NodeRenderer.
|
|
157
|
-
|
|
158
|
-
| Name | Type | Description |
|
|
159
|
-
| ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
160
|
-
| isOpen | boolean | True if this node has children and the children are visible. Use this to display some type a open or closed icon. |
|
|
161
|
-
| isSelected | boolean | True if this node is selected. Use this to show a "selected" state. Maybe a different background? |
|
|
162
|
-
| 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. |
|
|
163
|
-
| isDragging | boolean | True if this node is being dragged. |
|
|
164
|
-
| 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? |
|
|
165
|
-
| isSelectedEnd | boolean | True if this is the last of a contiguous group of selected rows. |
|
|
166
|
-
| isEditing | boolean | True if this row is being edited. When true, the renderer should return some type of form input. |
|
|
167
|
-
|
|
168
|
-
### Handlers Prop
|
|
169
|
-
|
|
170
|
-
These are the properties on the handlers object passed to the NodeRenderer.
|
|
171
|
-
|
|
172
|
-
| Name | Type | Description |
|
|
173
|
-
| ------ | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
174
|
-
| 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}>` |
|
|
175
|
-
| 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}>` |
|
|
176
|
-
| edit | `() => void` | Makes this node editable. This will re-render the Node with the `state.isEditing` prop set to `true`. |
|
|
177
|
-
| submit | `(update: string) => void` | Sends the update to the `onEdit` handler in the Tree component, and sets the `state.isEditing` prop to `false`. |
|
|
178
|
-
| reset | `() => void` | Re-renders with the `state.isEditing` prop set to `false`. |
|
|
179
|
-
|
|
180
|
-
### Tree Prop
|
|
181
|
-
|
|
182
|
-
The tree monitor provides methods to get and change the tree's internal state. A use case might be in a right click menu.
|
|
183
|
-
|
|
184
|
-
```jsx
|
|
185
|
-
// In your node renderer
|
|
186
|
-
onContextMenu={() => {
|
|
187
|
-
// Do something with all the selected nodes
|
|
188
|
-
tree.getSelectedIds()
|
|
189
|
-
}}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
| Methods | Returns | Description |
|
|
193
|
-
| ----------------------------------------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
194
|
-
| `getSelectedIds()` | `string[]` | Get the the ids of all currently selected nodes. |
|
|
195
|
-
| `edit(id: string)` | `Promise<{cancelled: boolean, value: string \| undefined}>` | Edit a node programatically. Resolves when the edit is finished or cancelled. |
|
|
196
|
-
| `submit(id: string, value: string)` | void | Submit the edit. |
|
|
197
|
-
| `reset(id: string)` | void | Cancel the edit. |
|
|
198
|
-
| `select(index: number, meta = false, shift = false)` | `void` | Select a node by it's visible index in the tree. The meta flag, when true, will allow multiple items to be selected. The shift flag, when true, will select all nodes between the last one selected, and this one. |
|
|
199
|
-
| `selectById(id: string, meta = false, shift = false)` | `void` | Same as above but selects by id. If the id is not present (in a collapsed folder), nothing will happen. First use the `scrollToId` function which will open all the parents and scroll to the id. |
|
|
200
|
-
| `scrollToId(id: string)` | void | Scroll to the id opening all it's parents if needed. |
|
|
201
|
-
|
|
202
|
-
[Full Tree API Implementation](https://github.com/brimdata/react-arborist/blob/main/packages/react-arborist/src/tree-api.ts)
|
|
203
|
-
|
|
204
|
-
### Accessing the Tree API from the Parent
|
|
205
|
-
|
|
206
|
-
You may need to access the tree from the parent component. This can be done by passing a ref.
|
|
207
|
-
|
|
208
|
-
```jsx
|
|
209
|
-
function MySection() {
|
|
210
|
-
const tree = useRef(null)
|
|
211
|
-
|
|
212
|
-
useEffect(() => {
|
|
213
|
-
tree.current.scrollToId("B")
|
|
214
|
-
}, [])
|
|
215
|
-
|
|
216
|
-
return <Tree ref={ref} ... />
|
|
217
|
-
}
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Authored by [James Kerr](https://twitter.com/specialCaseDev)
|
package/dist/components/row.d.ts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { TreeProps, IdObj, Node } from "../types";
|
|
2
|
-
export declare function enrichTree<T extends IdObj>(model: T, hideRoot?: boolean, getChildren?: TreeProps<T>["getChildren"], isOpen?: TreeProps<T>["isOpen"], disableDrag?: TreeProps<T>["disableDrag"], disableDrop?: TreeProps<T>["disableDrop"], openByDefault?: boolean): Node<T>;
|
package/dist/provider.d.ts
DELETED
package/dist/reducer.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { Cursor } from "./dnd/compute-drop";
|
|
2
|
-
import { StateContext } from "./types";
|
|
3
|
-
export declare const initState: () => StateContext;
|
|
4
|
-
export declare const actions: {
|
|
5
|
-
setCursorLocation: (cursor: Cursor) => {
|
|
6
|
-
type: "SET_CURSOR_LOCATION";
|
|
7
|
-
cursor: Cursor;
|
|
8
|
-
};
|
|
9
|
-
setVisibleIds: (ids: string[], idMap: {
|
|
10
|
-
[id: string]: number;
|
|
11
|
-
}) => {
|
|
12
|
-
type: "SET_VISIBLE_IDS";
|
|
13
|
-
ids: string[];
|
|
14
|
-
idMap: {
|
|
15
|
-
[id: string]: number;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
select: (index: number | null, meta: boolean, shift: boolean) => {
|
|
19
|
-
type: "SELECT";
|
|
20
|
-
index: number | null;
|
|
21
|
-
meta: boolean;
|
|
22
|
-
shift: boolean;
|
|
23
|
-
};
|
|
24
|
-
selectId: (id: string) => {
|
|
25
|
-
type: "SELECT_ID";
|
|
26
|
-
id: string;
|
|
27
|
-
};
|
|
28
|
-
edit: (id: string | null) => {
|
|
29
|
-
type: "EDIT";
|
|
30
|
-
id: string | null;
|
|
31
|
-
};
|
|
32
|
-
stepUp: (shift: boolean, ids: string[]) => {
|
|
33
|
-
type: "STEP_UP";
|
|
34
|
-
shift: boolean;
|
|
35
|
-
};
|
|
36
|
-
stepDown: (shift: boolean, ids: string[]) => {
|
|
37
|
-
type: "STEP_DOWN";
|
|
38
|
-
shift: boolean;
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
declare type ActionObj = {
|
|
42
|
-
[Prop in keyof typeof actions]: ReturnType<typeof actions[Prop]>;
|
|
43
|
-
};
|
|
44
|
-
export declare type Action = ActionObj[keyof ActionObj];
|
|
45
|
-
export declare function reducer(state: StateContext, action: Action): StateContext;
|
|
46
|
-
export {};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare class Range {
|
|
2
|
-
start: number;
|
|
3
|
-
end: number;
|
|
4
|
-
constructor(start: number, end: number);
|
|
5
|
-
serialize(): [number, number];
|
|
6
|
-
contains(n: number): boolean;
|
|
7
|
-
overlaps(r: Range): boolean;
|
|
8
|
-
combine(r: Range): void;
|
|
9
|
-
get size(): number;
|
|
10
|
-
clone(): Range;
|
|
11
|
-
map(fn: (index: any) => string): any;
|
|
12
|
-
isEqual(other: Range): boolean;
|
|
13
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Range } from "./range";
|
|
2
|
-
declare type SelectionDirection = "forward" | "backward" | "none";
|
|
3
|
-
export declare type SelectionData = {
|
|
4
|
-
ranges: [number, number][];
|
|
5
|
-
currentIndex: number | null;
|
|
6
|
-
direction: SelectionDirection;
|
|
7
|
-
};
|
|
8
|
-
export declare class Selection {
|
|
9
|
-
ranges: Range[];
|
|
10
|
-
currentIndex: number | null;
|
|
11
|
-
direction: SelectionDirection;
|
|
12
|
-
items: any[];
|
|
13
|
-
static parse(data: SelectionData | null, items: any[]): Selection;
|
|
14
|
-
constructor(ranges?: [number, number][], currentIndex?: number | null, direction?: SelectionDirection, items?: any[]);
|
|
15
|
-
get current(): Range | null;
|
|
16
|
-
select(n: number): void;
|
|
17
|
-
multiSelect(n: number): void;
|
|
18
|
-
deselect(n: number): void;
|
|
19
|
-
getSelectedItems<T>(): T[];
|
|
20
|
-
extend(n: number): void;
|
|
21
|
-
contains(n: number | null): boolean;
|
|
22
|
-
getRanges(): [number, number][];
|
|
23
|
-
clear(): void;
|
|
24
|
-
serialize(): SelectionData;
|
|
25
|
-
isEqual(other: Selection): boolean;
|
|
26
|
-
private addRange;
|
|
27
|
-
private removeRange;
|
|
28
|
-
private isEmpty;
|
|
29
|
-
getAnchor(): number | null;
|
|
30
|
-
getFocus(): number;
|
|
31
|
-
private compact;
|
|
32
|
-
}
|
|
33
|
-
export {};
|
package/dist/tree-api-hook.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Dispatch } from "react";
|
|
2
|
-
import { FixedSizeList } from "react-window";
|
|
3
|
-
import { Action } from "./reducer";
|
|
4
|
-
import { TreeApi } from "./tree-api";
|
|
5
|
-
import { StateContext, TreeProviderProps } from "./types";
|
|
6
|
-
export declare function useTreeApi<T>(state: StateContext, dispatch: Dispatch<Action>, props: TreeProviderProps<T>, list: FixedSizeList | undefined): TreeApi<T>;
|
package/dist/tree-api.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Dispatch } from "react";
|
|
2
|
-
import { FixedSizeList } from "react-window";
|
|
3
|
-
import { Cursor } from "./dnd/compute-drop";
|
|
4
|
-
import { Action } from "./reducer";
|
|
5
|
-
import { Node, StateContext, TreeProviderProps, EditResult } from "./types";
|
|
6
|
-
export declare class TreeApi<T = unknown> {
|
|
7
|
-
dispatch: Dispatch<Action>;
|
|
8
|
-
state: StateContext;
|
|
9
|
-
props: TreeProviderProps<T>;
|
|
10
|
-
list: FixedSizeList | undefined;
|
|
11
|
-
private edits;
|
|
12
|
-
constructor(dispatch: Dispatch<Action>, state: StateContext, props: TreeProviderProps<T>, list: FixedSizeList | undefined);
|
|
13
|
-
assign(dispatch: Dispatch<Action>, state: StateContext, props: TreeProviderProps<T>, list: FixedSizeList | undefined): void;
|
|
14
|
-
getNode(id: string): Node<unknown> | null;
|
|
15
|
-
getSelectedIds(): string[];
|
|
16
|
-
edit(id: string | number): Promise<EditResult>;
|
|
17
|
-
submit(id: string | number, value: string): void;
|
|
18
|
-
reset(id: string | number): void;
|
|
19
|
-
private resolveEdit;
|
|
20
|
-
select(index: number | null, meta?: boolean, shift?: boolean): void;
|
|
21
|
-
selectById(id: string | number, meta?: boolean, shift?: boolean): void;
|
|
22
|
-
selectUpwards(shiftKey: boolean): void;
|
|
23
|
-
selectDownwards(shiftKey: boolean): void;
|
|
24
|
-
hideCursor(): void;
|
|
25
|
-
showCursor(cursor: Cursor): void;
|
|
26
|
-
scrollToId(id: string): void;
|
|
27
|
-
open(id: string): void;
|
|
28
|
-
openParents(id: string): void;
|
|
29
|
-
get visibleIds(): string[];
|
|
30
|
-
get idToIndex(): {
|
|
31
|
-
[id: string]: number;
|
|
32
|
-
};
|
|
33
|
-
get visibleNodes(): Node<unknown>[];
|
|
34
|
-
}
|
package/dist/types.d.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import React, { ComponentType, CSSProperties, MouseEvent, MouseEventHandler, MutableRefObject, ReactElement, Ref } from "react";
|
|
2
|
-
import { FixedSizeList } from "react-window";
|
|
3
|
-
import { Cursor } from "./dnd/compute-drop";
|
|
4
|
-
import { SelectionData } from "./selection/selection";
|
|
5
|
-
import { TreeApi } from "./tree-api";
|
|
6
|
-
declare module "react" {
|
|
7
|
-
function forwardRef<T, P = {}>(render: (props: P, ref: React.Ref<T>) => React.ReactElement | null): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
|
|
8
|
-
}
|
|
9
|
-
export declare type Node<T = unknown> = {
|
|
10
|
-
id: string;
|
|
11
|
-
model: T;
|
|
12
|
-
level: number;
|
|
13
|
-
children: Node<T>[] | null;
|
|
14
|
-
parent: Node<T> | null;
|
|
15
|
-
isOpen: boolean;
|
|
16
|
-
isDraggable: boolean;
|
|
17
|
-
isDroppable: boolean;
|
|
18
|
-
rowIndex: number | null;
|
|
19
|
-
};
|
|
20
|
-
export declare type NodesById<T> = {
|
|
21
|
-
[id: string]: Node<T>;
|
|
22
|
-
};
|
|
23
|
-
export interface IdObj {
|
|
24
|
-
id: string;
|
|
25
|
-
}
|
|
26
|
-
export declare type NodeRendererProps<T> = {
|
|
27
|
-
innerRef: (el: HTMLDivElement | null) => void;
|
|
28
|
-
styles: {
|
|
29
|
-
row: CSSProperties;
|
|
30
|
-
indent: CSSProperties;
|
|
31
|
-
};
|
|
32
|
-
data: T;
|
|
33
|
-
state: NodeState;
|
|
34
|
-
handlers: NodeHandlers;
|
|
35
|
-
tree: TreeApi<T>;
|
|
36
|
-
preview: boolean;
|
|
37
|
-
};
|
|
38
|
-
export declare type NodeState = {
|
|
39
|
-
isOpen: boolean;
|
|
40
|
-
isSelected: boolean;
|
|
41
|
-
isHoveringOverChild: boolean;
|
|
42
|
-
isDragging: boolean;
|
|
43
|
-
isSelectedStart: boolean;
|
|
44
|
-
isSelectedEnd: boolean;
|
|
45
|
-
isEditing: boolean;
|
|
46
|
-
};
|
|
47
|
-
export declare type NodeHandlers = {
|
|
48
|
-
toggle: MouseEventHandler;
|
|
49
|
-
select: (e: MouseEvent, args: {
|
|
50
|
-
selectOnClick: boolean;
|
|
51
|
-
}) => void;
|
|
52
|
-
edit: () => Promise<EditResult>;
|
|
53
|
-
submit: (name: string) => void;
|
|
54
|
-
reset: () => void;
|
|
55
|
-
};
|
|
56
|
-
export declare type NodeRenderer<T> = ComponentType<NodeRendererProps<T>>;
|
|
57
|
-
export declare type MoveHandler = (dragIds: string[], parentId: string | null, index: number) => void;
|
|
58
|
-
export declare type ToggleHandler = (id: string, isOpen: boolean) => void;
|
|
59
|
-
export declare type EditHandler = (id: string, name: string) => void;
|
|
60
|
-
export declare type NodeClickHandler<T> = (e: MouseEvent, n: Node<T>) => void;
|
|
61
|
-
export declare type IndexClickHandler = (e: MouseEvent, index: number) => void;
|
|
62
|
-
export declare type SelectedCheck = (index: number) => boolean;
|
|
63
|
-
export declare type CursorLocation = {
|
|
64
|
-
index: number | null;
|
|
65
|
-
level: number | null;
|
|
66
|
-
parentId: string | null;
|
|
67
|
-
};
|
|
68
|
-
export declare type DragItem = {
|
|
69
|
-
dragIds: string[];
|
|
70
|
-
id: string;
|
|
71
|
-
};
|
|
72
|
-
export declare type SelectionState = {
|
|
73
|
-
data: SelectionData | null;
|
|
74
|
-
ids: string[];
|
|
75
|
-
};
|
|
76
|
-
export declare type StateContext = {
|
|
77
|
-
cursor: Cursor;
|
|
78
|
-
editingId: string | null;
|
|
79
|
-
selection: SelectionState;
|
|
80
|
-
visibleIds: string[];
|
|
81
|
-
};
|
|
82
|
-
declare type BoolFunc<T> = (data: T) => boolean;
|
|
83
|
-
export interface TreeProps<T> {
|
|
84
|
-
children: NodeRenderer<T>;
|
|
85
|
-
className?: string | undefined;
|
|
86
|
-
data: T;
|
|
87
|
-
disableDrag?: string | boolean | BoolFunc<T>;
|
|
88
|
-
disableDrop?: string | boolean | BoolFunc<T>;
|
|
89
|
-
dndRootElement?: globalThis.Node | null;
|
|
90
|
-
getChildren?: string | ((d: T) => T[]);
|
|
91
|
-
handle?: Ref<TreeApi<T>>;
|
|
92
|
-
height?: number;
|
|
93
|
-
hideRoot?: boolean;
|
|
94
|
-
indent?: number;
|
|
95
|
-
isOpen?: string | BoolFunc<T>;
|
|
96
|
-
onClick?: MouseEventHandler;
|
|
97
|
-
onContextMenu?: MouseEventHandler;
|
|
98
|
-
onEdit?: EditHandler;
|
|
99
|
-
onMove?: MoveHandler;
|
|
100
|
-
onToggle?: ToggleHandler;
|
|
101
|
-
openByDefault?: boolean;
|
|
102
|
-
rowHeight?: number;
|
|
103
|
-
width?: number;
|
|
104
|
-
}
|
|
105
|
-
export declare type TreeProviderProps<T> = {
|
|
106
|
-
imperativeHandle: React.Ref<TreeApi<T>> | undefined;
|
|
107
|
-
children: ReactElement;
|
|
108
|
-
height: number;
|
|
109
|
-
indent: number;
|
|
110
|
-
listEl: MutableRefObject<HTMLDivElement | null>;
|
|
111
|
-
onToggle: ToggleHandler;
|
|
112
|
-
onMove: MoveHandler;
|
|
113
|
-
onEdit: EditHandler;
|
|
114
|
-
onClick?: MouseEventHandler;
|
|
115
|
-
onContextMenu?: MouseEventHandler;
|
|
116
|
-
renderer: NodeRenderer<any>;
|
|
117
|
-
rowHeight: number;
|
|
118
|
-
root: Node<T>;
|
|
119
|
-
width: number;
|
|
120
|
-
};
|
|
121
|
-
export declare type StaticContext<T> = TreeProviderProps<T> & {
|
|
122
|
-
api: TreeApi<T>;
|
|
123
|
-
list: MutableRefObject<FixedSizeList | undefined>;
|
|
124
|
-
};
|
|
125
|
-
export declare type EditResult = {
|
|
126
|
-
cancelled: true;
|
|
127
|
-
} | {
|
|
128
|
-
cancelled: false;
|
|
129
|
-
value: string;
|
|
130
|
-
};
|
|
131
|
-
export {};
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import React, { CSSProperties, memo } from "react";
|
|
2
|
-
import { useDragLayer, XYCoord } from "react-dnd";
|
|
3
|
-
import { useStaticContext } from "../context";
|
|
4
|
-
import { DragItem } from "../types";
|
|
5
|
-
|
|
6
|
-
const layerStyles: CSSProperties = {
|
|
7
|
-
position: "fixed",
|
|
8
|
-
pointerEvents: "none",
|
|
9
|
-
zIndex: 100,
|
|
10
|
-
left: 0,
|
|
11
|
-
top: 0,
|
|
12
|
-
width: "100%",
|
|
13
|
-
height: "100%",
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const getStyle = (offset: XYCoord | null) => {
|
|
17
|
-
if (!offset) return { display: "none" };
|
|
18
|
-
const { x, y } = offset;
|
|
19
|
-
return { transform: `translate(${x}px, ${y}px)` };
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const getCountStyle = (offset: XYCoord | null) => {
|
|
23
|
-
if (!offset) return { display: "none" };
|
|
24
|
-
const { x, y } = offset;
|
|
25
|
-
return { transform: `translate(${x + 10}px, ${y + 10}px)` };
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export function Preview() {
|
|
29
|
-
const { offset, mouse, item, isDragging } = useDragLayer((m) => ({
|
|
30
|
-
offset: m.getSourceClientOffset(),
|
|
31
|
-
mouse: m.getClientOffset(),
|
|
32
|
-
item: m.getItem(),
|
|
33
|
-
isDragging: m.isDragging(),
|
|
34
|
-
}));
|
|
35
|
-
|
|
36
|
-
return (
|
|
37
|
-
<Overlay isDragging={isDragging}>
|
|
38
|
-
<Position offset={offset}>
|
|
39
|
-
<PreviewNode item={item} />
|
|
40
|
-
</Position>
|
|
41
|
-
<Count mouse={mouse} item={item} />
|
|
42
|
-
</Overlay>
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const Overlay = memo(function Overlay(props: {
|
|
47
|
-
children: JSX.Element[];
|
|
48
|
-
isDragging: boolean;
|
|
49
|
-
}) {
|
|
50
|
-
if (!props.isDragging) return null;
|
|
51
|
-
return <div style={layerStyles}>{props.children}</div>;
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
function Position(props: { children: JSX.Element; offset: XYCoord | null }) {
|
|
55
|
-
return (
|
|
56
|
-
<div className="row preview" style={getStyle(props.offset)}>
|
|
57
|
-
{props.children}
|
|
58
|
-
</div>
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function Count(props: { item: DragItem; mouse: XYCoord | null }) {
|
|
63
|
-
const { item, mouse } = props;
|
|
64
|
-
if (item?.dragIds?.length > 1)
|
|
65
|
-
return (
|
|
66
|
-
<div className="selected-count" style={getCountStyle(mouse)}>
|
|
67
|
-
{item.dragIds.length}
|
|
68
|
-
</div>
|
|
69
|
-
);
|
|
70
|
-
else return null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const PreviewNode = memo(function PreviewNode(props: {
|
|
74
|
-
item: DragItem | null;
|
|
75
|
-
}) {
|
|
76
|
-
const tree = useStaticContext();
|
|
77
|
-
if (!props.item) return null;
|
|
78
|
-
const node = tree.api.getNode(props.item.id);
|
|
79
|
-
if (!node) return null;
|
|
80
|
-
return (
|
|
81
|
-
<tree.renderer
|
|
82
|
-
preview
|
|
83
|
-
innerRef={() => {}}
|
|
84
|
-
data={node.model}
|
|
85
|
-
styles={{
|
|
86
|
-
row: {},
|
|
87
|
-
indent: { paddingLeft: node.level * tree.indent },
|
|
88
|
-
}}
|
|
89
|
-
tree={tree.api}
|
|
90
|
-
state={{
|
|
91
|
-
isDragging: false,
|
|
92
|
-
isEditing: false,
|
|
93
|
-
isSelected: false,
|
|
94
|
-
isSelectedStart: false,
|
|
95
|
-
isSelectedEnd: false,
|
|
96
|
-
isHoveringOverChild: false,
|
|
97
|
-
isOpen: node.isOpen,
|
|
98
|
-
}}
|
|
99
|
-
handlers={{
|
|
100
|
-
edit: () => Promise.resolve({ cancelled: true }),
|
|
101
|
-
select: () => {},
|
|
102
|
-
toggle: () => {},
|
|
103
|
-
submit: () => {},
|
|
104
|
-
reset: () => {},
|
|
105
|
-
}}
|
|
106
|
-
/>
|
|
107
|
-
);
|
|
108
|
-
});
|