react-arborist 2.0.0-rc.2 → 2.1.0
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 +11 -3
- package/dist/components/default-node.d.ts +1 -2
- package/dist/components/default-row.d.ts +1 -2
- package/dist/components/provider.d.ts +2 -3
- package/dist/components/row-container.d.ts +1 -2
- package/dist/components/tree.d.ts +1 -2
- package/dist/context.d.ts +1 -2
- package/dist/data/create-index.d.ts +1 -2
- package/dist/data/create-list.d.ts +1 -2
- package/dist/data/create-root.d.ts +1 -2
- package/dist/data/simple-tree.d.ts +5 -4
- package/dist/dnd/drag-hook.d.ts +1 -2
- package/dist/hooks/use-fresh-node.d.ts +1 -2
- package/dist/hooks/use-simple-tree.d.ts +5 -6
- package/dist/hooks/use-validated-props.d.ts +1 -2
- package/dist/index.js +21 -11
- package/dist/index.js.map +1 -1
- package/dist/interfaces/node-api.d.ts +2 -3
- package/dist/interfaces/tree-api.d.ts +2 -2
- package/dist/module.js +21 -11
- package/dist/module.js.map +1 -1
- package/dist/types/handlers.d.ts +10 -4
- package/dist/types/renderers.d.ts +2 -3
- package/dist/types/tree-props.d.ts +6 -6
- package/dist/types/utils.d.ts +1 -1
- package/package.json +1 -1
- package/src/components/default-drag-preview.tsx +1 -1
- package/src/components/default-node.tsx +3 -3
- package/src/components/default-row.tsx +1 -1
- package/src/components/provider.tsx +2 -2
- package/src/components/row-container.tsx +1 -1
- package/src/components/tree.tsx +1 -1
- package/src/context.ts +1 -1
- package/src/data/create-index.ts +1 -1
- package/src/data/create-list.ts +3 -3
- package/src/data/create-root.ts +1 -1
- package/src/data/simple-tree.ts +5 -5
- package/src/dnd/drag-hook.ts +5 -4
- package/src/hooks/use-fresh-node.ts +1 -1
- package/src/hooks/use-simple-tree.ts +12 -7
- package/src/hooks/use-validated-props.ts +1 -3
- package/src/interfaces/node-api.ts +2 -2
- package/src/interfaces/tree-api.ts +26 -15
- package/src/types/handlers.ts +12 -4
- package/src/types/renderers.ts +2 -2
- package/src/types/tree-props.ts +6 -6
- package/src/types/utils.ts +1 -1
package/dist/types/handlers.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
+
import { NodeApi } from "../interfaces/node-api";
|
|
1
2
|
import { IdObj } from "./utils";
|
|
2
|
-
export declare type CreateHandler = (args: {
|
|
3
|
+
export declare type CreateHandler<T> = (args: {
|
|
3
4
|
parentId: string | null;
|
|
5
|
+
parentNode: NodeApi<T> | null;
|
|
4
6
|
index: number;
|
|
5
7
|
type: "internal" | "leaf";
|
|
6
8
|
}) => (IdObj | null) | Promise<IdObj | null>;
|
|
7
|
-
export declare type MoveHandler = (args: {
|
|
9
|
+
export declare type MoveHandler<T> = (args: {
|
|
8
10
|
dragIds: string[];
|
|
11
|
+
dragNodes: NodeApi<T>[];
|
|
9
12
|
parentId: string | null;
|
|
13
|
+
parentNode: NodeApi<T> | null;
|
|
10
14
|
index: number;
|
|
11
15
|
}) => void | Promise<void>;
|
|
12
|
-
export declare type RenameHandler = (args: {
|
|
16
|
+
export declare type RenameHandler<T> = (args: {
|
|
13
17
|
id: string;
|
|
14
18
|
name: string;
|
|
19
|
+
node: NodeApi<T>;
|
|
15
20
|
}) => void | Promise<void>;
|
|
16
|
-
export declare type DeleteHandler = (args: {
|
|
21
|
+
export declare type DeleteHandler<T> = (args: {
|
|
17
22
|
ids: string[];
|
|
23
|
+
nodes: NodeApi<T>[];
|
|
18
24
|
}) => void | Promise<void>;
|
|
19
25
|
export declare type EditResult = {
|
|
20
26
|
cancelled: true;
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { CSSProperties, HTMLAttributes, ReactElement } from "react";
|
|
2
|
-
import { IdObj } from "./utils";
|
|
3
2
|
import { NodeApi } from "../interfaces/node-api";
|
|
4
3
|
import { TreeApi } from "../interfaces/tree-api";
|
|
5
4
|
import { XYCoord } from "react-dnd";
|
|
6
|
-
export declare type NodeRendererProps<T
|
|
5
|
+
export declare type NodeRendererProps<T> = {
|
|
7
6
|
style: CSSProperties;
|
|
8
7
|
node: NodeApi<T>;
|
|
9
8
|
tree: TreeApi<T>;
|
|
10
9
|
dragHandle?: (el: HTMLDivElement | null) => void;
|
|
11
10
|
preview?: boolean;
|
|
12
11
|
};
|
|
13
|
-
export declare type RowRendererProps<T
|
|
12
|
+
export declare type RowRendererProps<T> = {
|
|
14
13
|
node: NodeApi<T>;
|
|
15
14
|
innerRef: (el: HTMLDivElement | null) => void;
|
|
16
15
|
attrs: HTMLAttributes<any>;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { BoolFunc
|
|
1
|
+
import { BoolFunc } from "./utils";
|
|
2
2
|
import * as handlers from "./handlers";
|
|
3
3
|
import * as renderers from "./renderers";
|
|
4
4
|
import { ElementType, MouseEventHandler } from "react";
|
|
5
5
|
import { ListOnScrollProps } from "react-window";
|
|
6
6
|
import { NodeApi } from "../interfaces/node-api";
|
|
7
7
|
import { OpenMap } from "../state/open-slice";
|
|
8
|
-
export interface TreeProps<T
|
|
8
|
+
export interface TreeProps<T> {
|
|
9
9
|
data?: T[];
|
|
10
10
|
initialData?: T[];
|
|
11
|
-
onCreate?: handlers.CreateHandler
|
|
12
|
-
onMove?: handlers.MoveHandler
|
|
13
|
-
onRename?: handlers.RenameHandler
|
|
14
|
-
onDelete?: handlers.DeleteHandler
|
|
11
|
+
onCreate?: handlers.CreateHandler<T>;
|
|
12
|
+
onMove?: handlers.MoveHandler<T>;
|
|
13
|
+
onRename?: handlers.RenameHandler<T>;
|
|
14
|
+
onDelete?: handlers.DeleteHandler<T>;
|
|
15
15
|
children?: ElementType<renderers.NodeRendererProps<T>>;
|
|
16
16
|
renderRow?: ElementType<renderers.RowRendererProps<T>>;
|
|
17
17
|
renderDragPreview?: ElementType<renderers.DragPreviewProps>;
|
package/dist/types/utils.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -70,7 +70,7 @@ function Count(props: { count: number; mouse: XYCoord | null }) {
|
|
|
70
70
|
else return null;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
const PreviewNode = memo(function PreviewNode<T
|
|
73
|
+
const PreviewNode = memo(function PreviewNode<T>(props: {
|
|
74
74
|
id: string | null;
|
|
75
75
|
dragIds: string[];
|
|
76
76
|
}) {
|
|
@@ -2,7 +2,7 @@ import React, { useEffect, useRef } from "react";
|
|
|
2
2
|
import { NodeRendererProps } from "../types/renderers";
|
|
3
3
|
import { IdObj } from "../types/utils";
|
|
4
4
|
|
|
5
|
-
export function DefaultNode<T
|
|
5
|
+
export function DefaultNode<T>(props: NodeRendererProps<T>) {
|
|
6
6
|
return (
|
|
7
7
|
<div ref={props.dragHandle} style={props.style}>
|
|
8
8
|
<span
|
|
@@ -18,7 +18,7 @@ export function DefaultNode<T extends IdObj>(props: NodeRendererProps<T>) {
|
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function Show<T
|
|
21
|
+
function Show<T>(props: NodeRendererProps<T>) {
|
|
22
22
|
return (
|
|
23
23
|
<>
|
|
24
24
|
{/* @ts-ignore */}
|
|
@@ -27,7 +27,7 @@ function Show<T extends IdObj>(props: NodeRendererProps<T>) {
|
|
|
27
27
|
);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
function Edit<T
|
|
30
|
+
function Edit<T>({ node }: NodeRendererProps<T>) {
|
|
31
31
|
const input = useRef<any>();
|
|
32
32
|
|
|
33
33
|
useEffect(() => {
|
|
@@ -23,7 +23,7 @@ import { TreeProps } from "../types/tree-props";
|
|
|
23
23
|
import { createStore, Store } from "redux";
|
|
24
24
|
import { actions as visibility } from "../state/open-slice";
|
|
25
25
|
|
|
26
|
-
type Props<T
|
|
26
|
+
type Props<T> = {
|
|
27
27
|
treeProps: TreeProps<T>;
|
|
28
28
|
imperativeHandle: React.Ref<TreeApi<T> | undefined>;
|
|
29
29
|
children: ReactNode;
|
|
@@ -31,7 +31,7 @@ type Props<T extends IdObj> = {
|
|
|
31
31
|
|
|
32
32
|
const SERVER_STATE = initialState();
|
|
33
33
|
|
|
34
|
-
export function TreeProvider<T
|
|
34
|
+
export function TreeProvider<T>({
|
|
35
35
|
treeProps,
|
|
36
36
|
imperativeHandle,
|
|
37
37
|
children,
|
package/src/components/tree.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import { TreeProps } from "../types/tree-props";
|
|
|
8
8
|
import { IdObj } from "../types/utils";
|
|
9
9
|
import { useValidatedProps } from "../hooks/use-validated-props";
|
|
10
10
|
|
|
11
|
-
export const Tree = forwardRef(function Tree<T
|
|
11
|
+
export const Tree = forwardRef(function Tree<T>(
|
|
12
12
|
props: TreeProps<T>,
|
|
13
13
|
ref: React.Ref<TreeApi<T> | undefined>
|
|
14
14
|
) {
|
package/src/context.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { IdObj } from "./types/utils";
|
|
|
5
5
|
|
|
6
6
|
export const TreeApiContext = createContext<TreeApi<any> | null>(null);
|
|
7
7
|
|
|
8
|
-
export function useTreeApi<T
|
|
8
|
+
export function useTreeApi<T>() {
|
|
9
9
|
const value = useContext<TreeApi<T> | null>(
|
|
10
10
|
TreeApiContext as unknown as React.Context<TreeApi<T> | null>
|
|
11
11
|
);
|
package/src/data/create-index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NodeApi } from "../interfaces/node-api";
|
|
2
2
|
import { IdObj } from "../types/utils";
|
|
3
3
|
|
|
4
|
-
export const createIndex = <T
|
|
4
|
+
export const createIndex = <T>(nodes: NodeApi<T>[]) => {
|
|
5
5
|
return nodes.reduce<{ [id: string]: number }>((map, node, index) => {
|
|
6
6
|
map[node.id] = index;
|
|
7
7
|
return map;
|
package/src/data/create-list.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { NodeApi } from "../interfaces/node-api";
|
|
|
2
2
|
import { TreeApi } from "../interfaces/tree-api";
|
|
3
3
|
import { IdObj } from "../types/utils";
|
|
4
4
|
|
|
5
|
-
export function createList<T
|
|
5
|
+
export function createList<T>(tree: TreeApi<T>) {
|
|
6
6
|
if (tree.isFiltered) {
|
|
7
7
|
return flattenAndFilterTree(tree.root, tree.isMatch.bind(tree));
|
|
8
8
|
} else {
|
|
@@ -10,7 +10,7 @@ export function createList<T extends IdObj>(tree: TreeApi<T>) {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
function flattenTree<T
|
|
13
|
+
function flattenTree<T>(root: NodeApi<T>): NodeApi<T>[] {
|
|
14
14
|
const list: NodeApi<T>[] = [];
|
|
15
15
|
function collect(node: NodeApi<T>) {
|
|
16
16
|
if (node.level >= 0) {
|
|
@@ -25,7 +25,7 @@ function flattenTree<T extends IdObj>(root: NodeApi<T>): NodeApi<T>[] {
|
|
|
25
25
|
return list;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
function flattenAndFilterTree<T
|
|
28
|
+
function flattenAndFilterTree<T>(
|
|
29
29
|
root: NodeApi<T>,
|
|
30
30
|
isMatch: (n: NodeApi<T>) => boolean
|
|
31
31
|
): NodeApi<T>[] {
|
package/src/data/create-root.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { TreeApi } from "../interfaces/tree-api";
|
|
|
4
4
|
|
|
5
5
|
export const ROOT_ID = "__REACT_ARBORIST_INTERNAL_ROOT__";
|
|
6
6
|
|
|
7
|
-
export function createRoot<T
|
|
7
|
+
export function createRoot<T>(tree: TreeApi<T>): NodeApi<T> {
|
|
8
8
|
function visitSelfAndChildren(
|
|
9
9
|
data: T,
|
|
10
10
|
level: number,
|
package/src/data/simple-tree.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
type
|
|
1
|
+
type SimpleData = { id: string; name: string; children?: SimpleData[] };
|
|
2
2
|
|
|
3
|
-
export class SimpleTree<T extends
|
|
3
|
+
export class SimpleTree<T extends SimpleData> {
|
|
4
4
|
root: SimpleNode<T>;
|
|
5
5
|
constructor(data: T[]) {
|
|
6
6
|
this.root = createRoot<T>(data);
|
|
@@ -48,20 +48,20 @@ export class SimpleTree<T extends IdObj> {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
function createRoot<T extends
|
|
51
|
+
function createRoot<T extends SimpleData>(data: T[]) {
|
|
52
52
|
const root = new SimpleNode<T>({ id: "ROOT" } as T, null);
|
|
53
53
|
root.children = data.map((d) => createNode(d as T, root));
|
|
54
54
|
return root;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function createNode<T extends
|
|
57
|
+
function createNode<T extends SimpleData>(data: T, parent: SimpleNode<T>) {
|
|
58
58
|
const node = new SimpleNode<T>(data, parent);
|
|
59
59
|
if (data.children)
|
|
60
60
|
node.children = data.children.map((d) => createNode<T>(d as T, node));
|
|
61
61
|
return node;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
class SimpleNode<T extends
|
|
64
|
+
class SimpleNode<T extends SimpleData> {
|
|
65
65
|
id: string;
|
|
66
66
|
children?: SimpleNode<T>[];
|
|
67
67
|
constructor(public data: T, public parent: SimpleNode<T> | null) {
|
package/src/dnd/drag-hook.ts
CHANGED
|
@@ -10,9 +10,7 @@ import { actions as dnd } from "../state/dnd-slice";
|
|
|
10
10
|
import { safeRun } from "../utils";
|
|
11
11
|
import { ROOT_ID } from "../data/create-root";
|
|
12
12
|
|
|
13
|
-
export function useDragHook<T
|
|
14
|
-
node: NodeApi<T>
|
|
15
|
-
): ConnectDragSource {
|
|
13
|
+
export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
|
|
16
14
|
const tree = useTreeApi();
|
|
17
15
|
const ids = tree.selectedIds;
|
|
18
16
|
const [_, ref, preview] = useDrag<DragItem, DropResult, void>(
|
|
@@ -33,10 +31,13 @@ export function useDragHook<T extends IdObj>(
|
|
|
33
31
|
// If they held down meta, we need to create a copy
|
|
34
32
|
// if (drop.dropEffect === "copy")
|
|
35
33
|
if (drop && drop.parentId) {
|
|
34
|
+
const parentId = drop.parentId === ROOT_ID ? null : drop.parentId;
|
|
36
35
|
safeRun(tree.props.onMove, {
|
|
37
36
|
dragIds: item.dragIds,
|
|
38
|
-
parentId
|
|
37
|
+
parentId,
|
|
39
38
|
index: drop.index,
|
|
39
|
+
dragNodes: item.dragIds.map((id) => tree.get(id)!),
|
|
40
|
+
parentNode: tree.get(drop.parentId),
|
|
40
41
|
});
|
|
41
42
|
tree.open(drop.parentId);
|
|
42
43
|
}
|
|
@@ -2,7 +2,7 @@ import { useMemo } from "react";
|
|
|
2
2
|
import { useTreeApi } from "../context";
|
|
3
3
|
import { IdObj } from "../types/utils";
|
|
4
4
|
|
|
5
|
-
export function useFreshNode<T
|
|
5
|
+
export function useFreshNode<T>(index: number) {
|
|
6
6
|
const tree = useTreeApi<T>();
|
|
7
7
|
const original = tree.at(index);
|
|
8
8
|
if (!original) throw new Error(`Could not find node for index: ${index}`);
|
|
@@ -16,11 +16,16 @@ export type SimpleTreeData = {
|
|
|
16
16
|
|
|
17
17
|
let nextId = 0;
|
|
18
18
|
|
|
19
|
-
export function useSimpleTree<T
|
|
19
|
+
export function useSimpleTree<T>(initialData: T[]) {
|
|
20
20
|
const [data, setData] = useState(initialData);
|
|
21
|
-
const tree = useMemo(
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const tree = useMemo(
|
|
22
|
+
() =>
|
|
23
|
+
new SimpleTree<// @ts-ignore
|
|
24
|
+
T>(data),
|
|
25
|
+
[data]
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const onMove: MoveHandler<T> = (args: {
|
|
24
29
|
dragIds: string[];
|
|
25
30
|
parentId: null | string;
|
|
26
31
|
index: number;
|
|
@@ -31,12 +36,12 @@ export function useSimpleTree<T extends IdObj>(initialData: T[]) {
|
|
|
31
36
|
setData(tree.data);
|
|
32
37
|
};
|
|
33
38
|
|
|
34
|
-
const onRename: RenameHandler = ({ name, id }) => {
|
|
39
|
+
const onRename: RenameHandler<T> = ({ name, id }) => {
|
|
35
40
|
tree.update({ id, changes: { name } as any });
|
|
36
41
|
setData(tree.data);
|
|
37
42
|
};
|
|
38
43
|
|
|
39
|
-
const onCreate: CreateHandler = ({ parentId, index, type }) => {
|
|
44
|
+
const onCreate: CreateHandler<T> = ({ parentId, index, type }) => {
|
|
40
45
|
const data = { id: `simple-tree-id-${nextId++}`, name: "" } as any;
|
|
41
46
|
if (type === "internal") data.children = [];
|
|
42
47
|
tree.create({ parentId, index, data });
|
|
@@ -44,7 +49,7 @@ export function useSimpleTree<T extends IdObj>(initialData: T[]) {
|
|
|
44
49
|
return data;
|
|
45
50
|
};
|
|
46
51
|
|
|
47
|
-
const onDelete: DeleteHandler = (args: { ids: string[] }) => {
|
|
52
|
+
const onDelete: DeleteHandler<T> = (args: { ids: string[] }) => {
|
|
48
53
|
args.ids.forEach((id) => tree.drop({ id }));
|
|
49
54
|
setData(tree.data);
|
|
50
55
|
};
|
|
@@ -2,9 +2,7 @@ import { TreeProps } from "../types/tree-props";
|
|
|
2
2
|
import { IdObj } from "../types/utils";
|
|
3
3
|
import { SimpleTreeData, useSimpleTree } from "./use-simple-tree";
|
|
4
4
|
|
|
5
|
-
export function useValidatedProps<T
|
|
6
|
-
props: TreeProps<T>
|
|
7
|
-
): TreeProps<T> {
|
|
5
|
+
export function useValidatedProps<T>(props: TreeProps<T>): TreeProps<T> {
|
|
8
6
|
if (props.initialData && props.data) {
|
|
9
7
|
throw new Error(
|
|
10
8
|
`React Arborist Tree => Provide either a data or initialData prop, but not both.`
|
|
@@ -3,7 +3,7 @@ import { TreeApi } from "./tree-api";
|
|
|
3
3
|
import { IdObj } from "../types/utils";
|
|
4
4
|
import { ROOT_ID } from "../data/create-root";
|
|
5
5
|
|
|
6
|
-
type Params<T
|
|
6
|
+
type Params<T> = {
|
|
7
7
|
id: string;
|
|
8
8
|
data: T;
|
|
9
9
|
level: number;
|
|
@@ -15,7 +15,7 @@ type Params<T extends IdObj> = {
|
|
|
15
15
|
tree: TreeApi<T>;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
export class NodeApi<T
|
|
18
|
+
export class NodeApi<T = any> {
|
|
19
19
|
tree: TreeApi<T>;
|
|
20
20
|
id: string;
|
|
21
21
|
data: T;
|
|
@@ -23,7 +23,7 @@ import { createList } from "../data/create-list";
|
|
|
23
23
|
import { createIndex } from "../data/create-index";
|
|
24
24
|
|
|
25
25
|
const { safeRun, identify, identifyNull } = utils;
|
|
26
|
-
export class TreeApi<T
|
|
26
|
+
export class TreeApi<T> {
|
|
27
27
|
static editPromise: null | ((args: EditResult) => void);
|
|
28
28
|
root: NodeApi<T>;
|
|
29
29
|
visibleNodes: NodeApi<T>[];
|
|
@@ -91,7 +91,9 @@ export class TreeApi<T extends IdObj> {
|
|
|
91
91
|
const match =
|
|
92
92
|
this.props.searchMatch ??
|
|
93
93
|
((node, term) => {
|
|
94
|
-
const string = JSON.stringify(
|
|
94
|
+
const string = JSON.stringify(
|
|
95
|
+
Object.values(node.data as { [k: string]: unknown })
|
|
96
|
+
);
|
|
95
97
|
return string.toLocaleLowerCase().includes(term.toLocaleLowerCase());
|
|
96
98
|
});
|
|
97
99
|
return (node: NodeApi<T>) => match(node, this.searchTerm);
|
|
@@ -190,13 +192,17 @@ export class TreeApi<T extends IdObj> {
|
|
|
190
192
|
index?: null | number;
|
|
191
193
|
} = {}
|
|
192
194
|
) {
|
|
195
|
+
const parentId =
|
|
196
|
+
opts.parentId === undefined
|
|
197
|
+
? utils.getInsertParentId(this)
|
|
198
|
+
: opts.parentId;
|
|
199
|
+
const index = opts.index ?? utils.getInsertIndex(this);
|
|
200
|
+
const type = opts.type ?? "leaf";
|
|
193
201
|
const data = await safeRun(this.props.onCreate, {
|
|
194
|
-
type
|
|
195
|
-
parentId
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
: opts.parentId,
|
|
199
|
-
index: opts.index ?? utils.getInsertIndex(this),
|
|
202
|
+
type,
|
|
203
|
+
parentId,
|
|
204
|
+
index,
|
|
205
|
+
parentNode: this.get(parentId),
|
|
200
206
|
});
|
|
201
207
|
if (data) {
|
|
202
208
|
this.focus(data);
|
|
@@ -211,9 +217,10 @@ export class TreeApi<T extends IdObj> {
|
|
|
211
217
|
|
|
212
218
|
async delete(node: string | IdObj | null | string[] | IdObj[]) {
|
|
213
219
|
if (!node) return;
|
|
214
|
-
const
|
|
215
|
-
const ids =
|
|
216
|
-
|
|
220
|
+
const idents = Array.isArray(node) ? node : [node];
|
|
221
|
+
const ids = idents.map(identify);
|
|
222
|
+
const nodes = ids.map((id) => this.get(id)!).filter((n) => !!n);
|
|
223
|
+
await safeRun(this.props.onDelete, { nodes, ids });
|
|
217
224
|
}
|
|
218
225
|
|
|
219
226
|
edit(node: string | IdObj): Promise<EditResult> {
|
|
@@ -226,10 +233,14 @@ export class TreeApi<T extends IdObj> {
|
|
|
226
233
|
});
|
|
227
234
|
}
|
|
228
235
|
|
|
229
|
-
async submit(
|
|
230
|
-
if (!
|
|
231
|
-
const id = identify(
|
|
232
|
-
await safeRun(this.props.onRename, {
|
|
236
|
+
async submit(identity: Identity, value: string) {
|
|
237
|
+
if (!identity) return;
|
|
238
|
+
const id = identify(identity);
|
|
239
|
+
await safeRun(this.props.onRename, {
|
|
240
|
+
id,
|
|
241
|
+
name: value,
|
|
242
|
+
node: this.get(id)!,
|
|
243
|
+
});
|
|
233
244
|
this.dispatch(edit(null));
|
|
234
245
|
this.resolveEdit({ cancelled: false, value });
|
|
235
246
|
setTimeout(() => this.onFocus()); // Return focus to element;
|
package/src/types/handlers.ts
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
|
+
import { NodeApi } from "../interfaces/node-api";
|
|
1
2
|
import { IdObj } from "./utils";
|
|
2
3
|
|
|
3
|
-
export type CreateHandler = (args: {
|
|
4
|
+
export type CreateHandler<T> = (args: {
|
|
4
5
|
parentId: string | null;
|
|
6
|
+
parentNode: NodeApi<T> | null;
|
|
5
7
|
index: number;
|
|
6
8
|
type: "internal" | "leaf";
|
|
7
9
|
}) => (IdObj | null) | Promise<IdObj | null>;
|
|
8
10
|
|
|
9
|
-
export type MoveHandler = (args: {
|
|
11
|
+
export type MoveHandler<T> = (args: {
|
|
10
12
|
dragIds: string[];
|
|
13
|
+
dragNodes: NodeApi<T>[];
|
|
11
14
|
parentId: string | null;
|
|
15
|
+
parentNode: NodeApi<T> | null;
|
|
12
16
|
index: number;
|
|
13
17
|
}) => void | Promise<void>;
|
|
14
18
|
|
|
15
|
-
export type RenameHandler = (args: {
|
|
19
|
+
export type RenameHandler<T> = (args: {
|
|
16
20
|
id: string;
|
|
17
21
|
name: string;
|
|
22
|
+
node: NodeApi<T>;
|
|
18
23
|
}) => void | Promise<void>;
|
|
19
24
|
|
|
20
|
-
export type DeleteHandler = (args: {
|
|
25
|
+
export type DeleteHandler<T> = (args: {
|
|
26
|
+
ids: string[];
|
|
27
|
+
nodes: NodeApi<T>[];
|
|
28
|
+
}) => void | Promise<void>;
|
|
21
29
|
|
|
22
30
|
export type EditResult =
|
|
23
31
|
| { cancelled: true }
|
package/src/types/renderers.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { NodeApi } from "../interfaces/node-api";
|
|
|
4
4
|
import { TreeApi } from "../interfaces/tree-api";
|
|
5
5
|
import { XYCoord } from "react-dnd";
|
|
6
6
|
|
|
7
|
-
export type NodeRendererProps<T
|
|
7
|
+
export type NodeRendererProps<T> = {
|
|
8
8
|
style: CSSProperties;
|
|
9
9
|
node: NodeApi<T>;
|
|
10
10
|
tree: TreeApi<T>;
|
|
@@ -12,7 +12,7 @@ export type NodeRendererProps<T extends IdObj> = {
|
|
|
12
12
|
preview?: boolean;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
export type RowRendererProps<T
|
|
15
|
+
export type RowRendererProps<T> = {
|
|
16
16
|
node: NodeApi<T>;
|
|
17
17
|
innerRef: (el: HTMLDivElement | null) => void;
|
|
18
18
|
attrs: HTMLAttributes<any>;
|
package/src/types/tree-props.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BoolFunc
|
|
1
|
+
import { BoolFunc } from "./utils";
|
|
2
2
|
import * as handlers from "./handlers";
|
|
3
3
|
import * as renderers from "./renderers";
|
|
4
4
|
import { ElementType, MouseEventHandler } from "react";
|
|
@@ -6,16 +6,16 @@ import { ListOnScrollProps } from "react-window";
|
|
|
6
6
|
import { NodeApi } from "../interfaces/node-api";
|
|
7
7
|
import { OpenMap, OpenSlice } from "../state/open-slice";
|
|
8
8
|
|
|
9
|
-
export interface TreeProps<T
|
|
9
|
+
export interface TreeProps<T> {
|
|
10
10
|
/* Data Options */
|
|
11
11
|
data?: T[];
|
|
12
12
|
initialData?: T[];
|
|
13
13
|
|
|
14
14
|
/* Data Handlers */
|
|
15
|
-
onCreate?: handlers.CreateHandler
|
|
16
|
-
onMove?: handlers.MoveHandler
|
|
17
|
-
onRename?: handlers.RenameHandler
|
|
18
|
-
onDelete?: handlers.DeleteHandler
|
|
15
|
+
onCreate?: handlers.CreateHandler<T>;
|
|
16
|
+
onMove?: handlers.MoveHandler<T>;
|
|
17
|
+
onRename?: handlers.RenameHandler<T>;
|
|
18
|
+
onDelete?: handlers.DeleteHandler<T>;
|
|
19
19
|
|
|
20
20
|
/* Renderers*/
|
|
21
21
|
children?: ElementType<renderers.NodeRendererProps<T>>;
|
package/src/types/utils.ts
CHANGED