react-arborist 2.0.0-rc → 2.0.0-rc.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.
@@ -21,7 +21,7 @@ export interface TreeProps<T extends IdObj> {
21
21
  children?: ElementType<renderers.NodeRendererProps<T>>;
22
22
  renderRow?: ElementType<renderers.RowRendererProps<T>>;
23
23
  renderDragPreview?: ElementType<renderers.DragPreviewProps>;
24
- renderCursor?: ElementType<renderers.DropCursorProps>;
24
+ renderCursor?: ElementType<renderers.CursorProps>;
25
25
  renderContainer?: ElementType<{}>;
26
26
 
27
27
  /* Sizes */
@@ -38,12 +38,15 @@ export interface TreeProps<T extends IdObj> {
38
38
  selectionFollowsFocus?: boolean;
39
39
  disableDrag?: string | boolean | BoolFunc<T>;
40
40
  disableDrop?: string | boolean | BoolFunc<T>;
41
- getChildren?: string | ((d: T) => T[]);
41
+ childrenAccessor?: string | ((d: T) => T[]);
42
+ idAccessor?: string | ((d: T) => string);
42
43
 
43
44
  /* Event Handlers */
44
45
  onActivate?: (node: NodeApi<T>) => void;
45
46
  onSelect?: (nodes: NodeApi<T>[]) => void;
46
47
  onScroll?: (props: ListOnScrollProps) => void;
48
+ onToggle?: (id: string) => void;
49
+ onFocus?: (node: NodeApi<T>) => void;
47
50
 
48
51
  /* Selection */
49
52
  selection?: string;
@@ -57,6 +60,8 @@ export interface TreeProps<T extends IdObj> {
57
60
 
58
61
  /* Extra */
59
62
  className?: string | undefined;
63
+ rowClassName?: string | undefined;
64
+
60
65
  dndRootElement?: globalThis.Node | null;
61
66
  onClick?: MouseEventHandler;
62
67
  onContextMenu?: MouseEventHandler;
package/src/utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { NodeApi } from "./interfaces/node-api";
2
+ import { TreeApi } from "./interfaces/tree-api";
2
3
  import { IdObj } from "./types/utils";
3
4
 
4
5
  export function bound(n: number, min: number, max: number) {
@@ -44,6 +45,18 @@ export function dfs(node: NodeApi<any>, id: string): NodeApi<any> | null {
44
45
  return null;
45
46
  }
46
47
 
48
+ export function walk(
49
+ node: NodeApi<any>,
50
+ fn: (node: NodeApi<any>) => void
51
+ ): void {
52
+ fn(node);
53
+ if (node.children) {
54
+ for (let child of node.children) {
55
+ walk(child, fn);
56
+ }
57
+ }
58
+ }
59
+
47
60
  export function focusNextElement(target: HTMLElement) {
48
61
  const elements = getFocusable(target);
49
62
 
@@ -147,3 +160,19 @@ export function waitFor(fn: () => boolean) {
147
160
  check();
148
161
  });
149
162
  }
163
+
164
+ export function getInsertIndex(tree: TreeApi<any>) {
165
+ const focus = tree.focusedNode;
166
+ if (!focus) return tree.root.children?.length ?? 0;
167
+ if (focus.isOpen) return 0;
168
+ if (focus.parent) return focus.childIndex + 1;
169
+ return 0;
170
+ }
171
+
172
+ export function getInsertParentId(tree: TreeApi<any>) {
173
+ const focus = tree.focusedNode;
174
+ if (!focus) return null;
175
+ if (focus.isOpen) return focus.id;
176
+ if (focus.parent) return focus.parent.id;
177
+ return null;
178
+ }
package/tsconfig.json CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  /* Language and Environment */
7
7
  "target": "ES5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
8
- "jsx": "preserve", /* Specify what JSX code is generated. */
8
+ "jsx": "react-jsx", /* Specify what JSX code is generated. */
9
9
 
10
10
  /* Modules */
11
11
  "module": "CommonJS", /* Specify what module code is generated. */
@@ -1,4 +0,0 @@
1
- import { NodeApi } from "../interfaces/node-api";
2
- import { IdObj } from "../types/utils";
3
- export declare function flattenTree<T extends IdObj>(root: NodeApi<T>): NodeApi<T>[];
4
- export declare function filterTree<T extends IdObj>(root: NodeApi<T>, isMatch: (n: NodeApi<T>) => boolean): NodeApi<T>[];
@@ -1,24 +0,0 @@
1
- declare type Data = {
2
- id: string;
3
- name: string;
4
- children?: Data[];
5
- };
6
- export declare function useUncontrolledTree(initialData: Data[]): readonly [Data[], {
7
- move: (args: {
8
- dragIds: string[];
9
- parentId: string;
10
- index: number;
11
- }) => void;
12
- rename: (args: {
13
- id: string;
14
- name: string;
15
- }) => void;
16
- create: ({ parentId, index }: {
17
- parentId: string;
18
- index: number;
19
- }) => Data;
20
- drop: (args: {
21
- id: string;
22
- }) => void;
23
- }];
24
- export {};
@@ -1,3 +0,0 @@
1
- import { TreeProps } from "../types/tree-props";
2
- import { IdObj } from "../types/utils";
3
- export declare function validateProps<T extends IdObj>(props: TreeProps<T>): TreeProps<T>;
@@ -1,8 +0,0 @@
1
- import { TreeProps } from "../types/tree-props";
2
- import { IdObj } from "../types/utils";
3
-
4
- export function validateProps<T extends IdObj>(
5
- props: TreeProps<T>
6
- ): TreeProps<T> {
7
- return props;
8
- }