react-arborist 3.10.3 → 3.10.5

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 (35) hide show
  1. package/dist/main/data/simple-tree.d.ts +14 -8
  2. package/dist/main/data/simple-tree.js +34 -15
  3. package/dist/main/data/simple-tree.test.d.ts +1 -0
  4. package/dist/main/data/simple-tree.test.js +63 -0
  5. package/dist/main/hooks/use-simple-tree.d.ts +2 -1
  6. package/dist/main/hooks/use-simple-tree.js +19 -4
  7. package/dist/main/hooks/use-simple-tree.test.d.ts +1 -0
  8. package/dist/main/hooks/use-simple-tree.test.js +32 -0
  9. package/dist/main/hooks/use-validated-props.js +4 -1
  10. package/dist/main/interfaces/tree-api.d.ts +33 -20
  11. package/dist/main/interfaces/tree-api.js +48 -23
  12. package/dist/main/interfaces/tree-api.test.js +48 -0
  13. package/dist/main/types/handlers.d.ts +1 -1
  14. package/dist/module/data/simple-tree.d.ts +14 -8
  15. package/dist/module/data/simple-tree.js +34 -15
  16. package/dist/module/data/simple-tree.test.d.ts +1 -0
  17. package/dist/module/data/simple-tree.test.js +61 -0
  18. package/dist/module/hooks/use-simple-tree.d.ts +2 -1
  19. package/dist/module/hooks/use-simple-tree.js +19 -4
  20. package/dist/module/hooks/use-simple-tree.test.d.ts +1 -0
  21. package/dist/module/hooks/use-simple-tree.test.js +30 -0
  22. package/dist/module/hooks/use-validated-props.js +4 -1
  23. package/dist/module/interfaces/tree-api.d.ts +33 -20
  24. package/dist/module/interfaces/tree-api.js +48 -23
  25. package/dist/module/interfaces/tree-api.test.js +48 -0
  26. package/dist/module/types/handlers.d.ts +1 -1
  27. package/package.json +1 -1
  28. package/src/data/simple-tree.test.ts +68 -0
  29. package/src/data/simple-tree.ts +53 -16
  30. package/src/hooks/use-simple-tree.test.ts +39 -0
  31. package/src/hooks/use-simple-tree.ts +26 -8
  32. package/src/hooks/use-validated-props.ts +4 -1
  33. package/src/interfaces/tree-api.test.ts +46 -0
  34. package/src/interfaces/tree-api.ts +68 -41
  35. package/src/types/handlers.ts +3 -1
@@ -1,11 +1,16 @@
1
- type SimpleData = {
2
- id: string;
3
- name: string;
4
- children?: SimpleData[];
1
+ export type SimpleTreeOptions<T> = {
2
+ idAccessor?: string | ((d: T) => string);
3
+ childrenAccessor?: string | ((d: T) => readonly T[] | null | undefined);
4
+ };
5
+ type Accessors<T> = {
6
+ getId: (data: T) => string;
7
+ getChildren: (data: T) => readonly T[] | null | undefined;
8
+ childrenKey: string;
5
9
  };
6
- export declare class SimpleTree<T extends SimpleData> {
10
+ export declare class SimpleTree<T> {
7
11
  root: SimpleNode<T>;
8
- constructor(data: T[]);
12
+ private accessors;
13
+ constructor(data: T[], options?: SimpleTreeOptions<T>);
9
14
  get data(): T[];
10
15
  create(args: {
11
16
  parentId: string | null;
@@ -26,12 +31,13 @@ export declare class SimpleTree<T extends SimpleData> {
26
31
  }): void;
27
32
  find(id: string, node?: SimpleNode<T>): SimpleNode<T> | null;
28
33
  }
29
- declare class SimpleNode<T extends SimpleData> {
34
+ declare class SimpleNode<T> {
30
35
  data: T;
31
36
  parent: SimpleNode<T> | null;
37
+ private accessors;
32
38
  id: string;
33
39
  children?: SimpleNode<T>[];
34
- constructor(data: T, parent: SimpleNode<T> | null);
40
+ constructor(data: T, parent: SimpleNode<T> | null, accessors: Accessors<T>, id?: string);
35
41
  hasParent(): this is this & {
36
42
  parent: SimpleNode<T>;
37
43
  };
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SimpleTree = void 0;
4
+ function resolveAccessors(options = {}) {
5
+ var _a, _b;
6
+ const id = (_a = options.idAccessor) !== null && _a !== void 0 ? _a : "id";
7
+ const children = (_b = options.childrenAccessor) !== null && _b !== void 0 ? _b : "children";
8
+ return {
9
+ getId: typeof id === "function" ? id : (data) => data[id],
10
+ getChildren: typeof children === "function" ? children : (data) => data[children],
11
+ childrenKey: typeof children === "string" ? children : "children",
12
+ };
13
+ }
4
14
  class SimpleTree {
5
- constructor(data) {
6
- this.root = createRoot(data);
15
+ constructor(data, options = {}) {
16
+ this.accessors = resolveAccessors(options);
17
+ this.root = createRoot(data, this.accessors);
7
18
  }
8
19
  get data() {
9
20
  var _a, _b;
@@ -50,22 +61,27 @@ class SimpleTree {
50
61
  }
51
62
  }
52
63
  exports.SimpleTree = SimpleTree;
53
- function createRoot(data) {
54
- const root = new SimpleNode({ id: "ROOT" }, null);
55
- root.children = data.map((d) => createNode(d, root));
64
+ function createRoot(data, accessors) {
65
+ // The synthetic root has no real data, so it gets an explicit id rather than
66
+ // running the user's accessor on `{}` — a function accessor that reaches into
67
+ // the data (e.g. `d => d.meta.id`) would otherwise throw during construction.
68
+ const root = new SimpleNode({}, null, accessors, "ROOT");
69
+ root.children = data.map((d) => createNode(d, root, accessors));
56
70
  return root;
57
71
  }
58
- function createNode(data, parent) {
59
- const node = new SimpleNode(data, parent);
60
- if (data.children)
61
- node.children = data.children.map((d) => createNode(d, node));
72
+ function createNode(data, parent, accessors) {
73
+ const node = new SimpleNode(data, parent, accessors);
74
+ const children = accessors.getChildren(data);
75
+ if (children)
76
+ node.children = children.map((d) => createNode(d, node, accessors));
62
77
  return node;
63
78
  }
64
79
  class SimpleNode {
65
- constructor(data, parent) {
80
+ constructor(data, parent, accessors, id) {
66
81
  this.data = data;
67
82
  this.parent = parent;
68
- this.id = data.id;
83
+ this.accessors = accessors;
84
+ this.id = id !== null && id !== void 0 ? id : accessors.getId(data);
69
85
  }
70
86
  hasParent() {
71
87
  return !!this.parent;
@@ -75,16 +91,19 @@ class SimpleNode {
75
91
  }
76
92
  addChild(data, index) {
77
93
  var _a, _b;
78
- const node = createNode(data, this);
94
+ const node = createNode(data, this, this.accessors);
79
95
  this.children = (_a = this.children) !== null && _a !== void 0 ? _a : [];
80
96
  this.children.splice(index, 0, node);
81
- this.data.children = (_b = this.data.children) !== null && _b !== void 0 ? _b : [];
82
- this.data.children.splice(index, 0, data);
97
+ const key = this.accessors.childrenKey;
98
+ const raw = this.data;
99
+ raw[key] = (_b = raw[key]) !== null && _b !== void 0 ? _b : [];
100
+ raw[key].splice(index, 0, data);
83
101
  }
84
102
  removeChild(index) {
85
103
  var _a, _b;
86
104
  (_a = this.children) === null || _a === void 0 ? void 0 : _a.splice(index, 1);
87
- (_b = this.data.children) === null || _b === void 0 ? void 0 : _b.splice(index, 1);
105
+ const raw = this.data;
106
+ (_b = raw[this.accessors.childrenKey]) === null || _b === void 0 ? void 0 : _b.splice(index, 1);
88
107
  }
89
108
  update(changes) {
90
109
  if (this.hasParent()) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const simple_tree_1 = require("./simple-tree");
4
+ describe("SimpleTree with default accessors", () => {
5
+ const data = () => [
6
+ { id: "1", name: "a", children: [{ id: "1a", name: "a-child" }] },
7
+ { id: "2", name: "b" },
8
+ ];
9
+ test("finds nodes by id, including nested ones", () => {
10
+ var _a, _b;
11
+ const tree = new simple_tree_1.SimpleTree(data());
12
+ expect((_a = tree.find("2")) === null || _a === void 0 ? void 0 : _a.data.name).toBe("b");
13
+ expect((_b = tree.find("1a")) === null || _b === void 0 ? void 0 : _b.data.name).toBe("a-child");
14
+ });
15
+ test("moves a node into a folder", () => {
16
+ const tree = new simple_tree_1.SimpleTree(data());
17
+ tree.move({ id: "2", parentId: "1", index: 1 });
18
+ expect(tree.data[0].children.map((c) => c.id)).toEqual(["1a", "2"]);
19
+ expect(tree.data).toHaveLength(1);
20
+ });
21
+ });
22
+ describe("SimpleTree honors custom accessors (issue #73, #170)", () => {
23
+ // Custom keys: `uuid` for the id, `elements` for the children.
24
+ const data = () => [
25
+ { uuid: "1", name: "a", elements: [{ uuid: "1a", name: "a-child" }] },
26
+ { uuid: "2", name: "b" },
27
+ ];
28
+ function tree() {
29
+ return new simple_tree_1.SimpleTree(data(), { idAccessor: "uuid", childrenAccessor: "elements" });
30
+ }
31
+ test("finds nodes by the custom id key, including nested ones", () => {
32
+ var _a, _b;
33
+ const t = tree();
34
+ expect((_a = t.find("2")) === null || _a === void 0 ? void 0 : _a.data.name).toBe("b");
35
+ expect((_b = t.find("1a")) === null || _b === void 0 ? void 0 : _b.data.name).toBe("a-child"); // read through `elements`
36
+ });
37
+ test("reorders a node, writing children back under the custom key (#170)", () => {
38
+ const t = tree();
39
+ t.move({ id: "2", parentId: "1", index: 1 });
40
+ expect(t.data).toHaveLength(1);
41
+ expect(t.data[0].elements.map((c) => c.uuid)).toEqual(["1a", "2"]);
42
+ });
43
+ test("moving a node with children into a childless node keeps its children (#73)", () => {
44
+ const t = tree();
45
+ // Put node "1" (which has children) inside node "2" (which has none).
46
+ t.move({ id: "1", parentId: "2", index: 0 });
47
+ expect(t.data.map((n) => n.uuid)).toEqual(["2"]);
48
+ const moved = t.find("1");
49
+ expect(moved === null || moved === void 0 ? void 0 : moved.data.elements.map((c) => c.uuid)).toEqual(["1a"]);
50
+ });
51
+ test("supports a function idAccessor", () => {
52
+ var _a;
53
+ const t = new simple_tree_1.SimpleTree(data(), { idAccessor: (d) => d.uuid, childrenAccessor: "elements" });
54
+ expect((_a = t.find("1a")) === null || _a === void 0 ? void 0 : _a.data.name).toBe("a-child");
55
+ t.move({ id: "2", parentId: "1", index: 1 });
56
+ expect(t.data[0].elements.map((c) => c.uuid)).toEqual(["1a", "2"]);
57
+ });
58
+ test("a function idAccessor that reaches into the data doesn't throw on construction", () => {
59
+ const nested = [{ meta: { id: "x" }, name: "x" }];
60
+ // The synthetic root must not run this accessor on its empty data.
61
+ expect(() => new simple_tree_1.SimpleTree(nested, { idAccessor: (d) => d.meta.id })).not.toThrow();
62
+ });
63
+ });
@@ -1,10 +1,11 @@
1
+ import { SimpleTreeOptions } from "../data/simple-tree";
1
2
  import { CreateHandler, DeleteHandler, MoveHandler, RenameHandler } from "../types/handlers";
2
3
  export type SimpleTreeData = {
3
4
  id: string;
4
5
  name: string;
5
6
  children?: SimpleTreeData[];
6
7
  };
7
- export declare function useSimpleTree<T>(initialData: readonly T[]): readonly [readonly T[], {
8
+ export declare function useSimpleTree<T>(initialData: readonly T[], options?: SimpleTreeOptions<T>): readonly [readonly T[], {
8
9
  onMove: MoveHandler<T>;
9
10
  onRename: RenameHandler<T>;
10
11
  onCreate: CreateHandler<T>;
@@ -4,9 +4,11 @@ exports.useSimpleTree = useSimpleTree;
4
4
  const react_1 = require("react");
5
5
  const simple_tree_1 = require("../data/simple-tree");
6
6
  let nextId = 0;
7
- function useSimpleTree(initialData) {
7
+ function useSimpleTree(initialData, options = {}) {
8
8
  const [data, setData] = (0, react_1.useState)(initialData);
9
- const tree = (0, react_1.useMemo)(() => new simple_tree_1.SimpleTree(data), [data]);
9
+ const idAccessor = options.idAccessor;
10
+ const childrenAccessor = options.childrenAccessor;
11
+ const tree = (0, react_1.useMemo)(() => new simple_tree_1.SimpleTree(data, { idAccessor, childrenAccessor }), [data, idAccessor, childrenAccessor]);
10
12
  const onMove = (args) => {
11
13
  for (const id of args.dragIds) {
12
14
  tree.move({ id, parentId: args.parentId, index: args.index });
@@ -17,10 +19,23 @@ function useSimpleTree(initialData) {
17
19
  tree.update({ id, changes: { name } });
18
20
  setData(tree.data);
19
21
  };
22
+ // New nodes must carry their id/children under the same keys the accessors
23
+ // read, or the controller (and the tree's own accessId) can't find them
24
+ // afterward (issue #73). A function accessor can't be inverted to a writable
25
+ // key, so node creation with one isn't supportable — fail fast instead of
26
+ // returning a node that throws deeper in the tree.
27
+ const idKey = typeof idAccessor === "string" ? idAccessor : "id";
28
+ const childrenKey = typeof childrenAccessor === "string" ? childrenAccessor : "children";
20
29
  const onCreate = ({ parentId, index, type }) => {
21
- const data = { id: `simple-tree-id-${nextId++}`, name: "" };
30
+ if (typeof idAccessor === "function") {
31
+ throw new Error(`React Arborist => initialData can't create nodes when idAccessor is a function: the generated id can't be written under a key the accessor reads. Use a string idAccessor, or the controlled \`data\` prop with your own onCreate.`);
32
+ }
33
+ if (type === "internal" && typeof childrenAccessor === "function") {
34
+ throw new Error(`React Arborist => initialData can't create folder nodes when childrenAccessor is a function: the new children array can't be written under a key the accessor reads. Use a string childrenAccessor, or the controlled \`data\` prop with your own onCreate.`);
35
+ }
36
+ const data = { [idKey]: `simple-tree-id-${nextId++}`, name: "" };
22
37
  if (type === "internal")
23
- data.children = [];
38
+ data[childrenKey] = [];
24
39
  tree.create({ parentId, index, data });
25
40
  setData(tree.data);
26
41
  return data;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("@testing-library/react");
4
+ const use_simple_tree_1 = require("./use-simple-tree");
5
+ /* onCreate has to write a new node's id (and a folder's children) under a key
6
+ the accessors will read back. A function accessor can't be inverted to a key,
7
+ so creation with one must fail fast rather than return an unusable node
8
+ (issue #73 review follow-up). */
9
+ describe("useSimpleTree onCreate guards function accessors", () => {
10
+ function controllerFor(data, options) {
11
+ const { result } = (0, react_1.renderHook)(() => (0, use_simple_tree_1.useSimpleTree)(data, options));
12
+ return result.current[1];
13
+ }
14
+ const create = { parentId: null, parentNode: null, index: 0 };
15
+ test("throws when idAccessor is a function", () => {
16
+ const controller = controllerFor([{ uuid: "1", name: "a" }], { idAccessor: (d) => d.uuid });
17
+ expect(() => controller.onCreate(Object.assign(Object.assign({}, create), { type: "leaf" }))).toThrow(/idAccessor is a function/);
18
+ });
19
+ test("throws when creating a folder with a function childrenAccessor", () => {
20
+ const controller = controllerFor([{ id: "1", name: "a" }], {
21
+ childrenAccessor: (d) => d.kids,
22
+ });
23
+ expect(() => controller.onCreate(Object.assign(Object.assign({}, create), { type: "internal" }))).toThrow(/childrenAccessor is a function/);
24
+ });
25
+ test("a leaf can still be created when only childrenAccessor is a function", () => {
26
+ const controller = controllerFor([{ id: "1", name: "a" }], {
27
+ childrenAccessor: (d) => d.kids,
28
+ });
29
+ // onCreate calls setData, so run it inside act to keep the suite warning-clean.
30
+ expect(() => (0, react_1.act)(() => void controller.onCreate(Object.assign(Object.assign({}, create), { type: "leaf" })))).not.toThrow();
31
+ });
32
+ });
@@ -18,7 +18,10 @@ Use the data prop if you want to provide your own handlers.`);
18
18
  *
19
19
  * We will provide the real data and the handlers to update it.
20
20
  * */
21
- const [data, controller] = (0, use_simple_tree_1.useSimpleTree)(props.initialData);
21
+ const [data, controller] = (0, use_simple_tree_1.useSimpleTree)(props.initialData, {
22
+ idAccessor: props.idAccessor,
23
+ childrenAccessor: props.childrenAccessor,
24
+ });
22
25
  return Object.assign(Object.assign(Object.assign({}, props), controller), { data });
23
26
  }
24
27
  else {
@@ -149,6 +149,19 @@ export declare class TreeApi<T> {
149
149
  get matchFn(): (node: NodeApi<T>) => boolean;
150
150
  accessChildren(data: T): readonly T[] | null;
151
151
  accessId(data: T): string;
152
+ /**
153
+ * Resolve an identifier to a node id. Public methods accept an id string, a
154
+ * NodeApi, or the raw row data; this is the one place that turns any of those
155
+ * into the string id used internally. Raw data is run through the configured
156
+ * `idAccessor` so a custom accessor (e.g. `uuid`) is honored everywhere, not
157
+ * just where nodes were built. A NodeApi already carries its accessor-derived
158
+ * `id`, so it is used directly rather than re-accessed (the accessor reads the
159
+ * underlying data, which a NodeApi does not expose under that key). Unlike
160
+ * `accessId`, an unresolved id comes back as `undefined` rather than throwing,
161
+ * preserving the previous behavior of the `id`-only lookup.
162
+ */
163
+ identify(identity: string | IdObj | T): string;
164
+ identifyNull(identity: Identity | T): string | null;
152
165
  get firstNode(): NodeApi<T>;
153
166
  get lastNode(): NodeApi<T>;
154
167
  get focusedNode(): NodeApi<T> | null;
@@ -158,7 +171,7 @@ export declare class TreeApi<T> {
158
171
  get(id: string | null): NodeApi<T> | null;
159
172
  at(index: number): NodeApi<T> | null;
160
173
  nodesBetween(startId: string | null, endId: string | null): NodeApi<T>[];
161
- indexOf(id: Identity): number | null;
174
+ indexOf(id: Identity | T): number | null;
162
175
  get editingId(): string | null;
163
176
  createInternal(): Promise<void>;
164
177
  createLeaf(): Promise<void>;
@@ -167,36 +180,36 @@ export declare class TreeApi<T> {
167
180
  parentId?: null | string;
168
181
  index?: null | number;
169
182
  }): Promise<void>;
170
- delete(node: Identity | string[] | IdObj[]): Promise<void>;
171
- edit(node: string | IdObj): Promise<EditResult>;
172
- submit(identity: Identity, value: string): Promise<void>;
183
+ delete(node: Identity | T | (string | IdObj | T)[]): Promise<void>;
184
+ edit(node: string | IdObj | T): Promise<EditResult>;
185
+ submit(identity: Identity | T, value: string): Promise<void>;
173
186
  reset(): void;
174
- activate(id: Identity): void;
187
+ activate(id: Identity | T): void;
175
188
  private resolveEdit;
176
189
  get selectedIds(): Set<string>;
177
190
  get selectedNodes(): NodeApi<T>[];
178
- focus(node: Identity, opts?: {
191
+ focus(node: Identity | T, opts?: {
179
192
  scroll?: boolean;
180
193
  }): void;
181
194
  pageUp(): void;
182
195
  pageDown(): void;
183
- select(node: Identity, opts?: {
196
+ select(node: Identity | T, opts?: {
184
197
  align?: Align;
185
198
  focus?: boolean;
186
199
  }): void;
187
- deselect(node: Identity): void;
188
- selectMulti(identity: Identity, opts?: {
200
+ deselect(node: Identity | T): void;
201
+ selectMulti(identity: Identity | T, opts?: {
189
202
  align?: Align;
190
203
  focus?: boolean;
191
204
  }): void;
192
- selectContiguous(identity: Identity): void;
205
+ selectContiguous(identity: Identity | T): void;
193
206
  deselectAll(): void;
194
207
  selectAll(): void;
195
208
  private filterSelectableNodes;
196
209
  setSelection(args: {
197
- ids: (IdObj | string)[] | null;
198
- anchor: Identity;
199
- mostRecent: Identity;
210
+ ids: (IdObj | string | T)[] | null;
211
+ anchor: Identity | T;
212
+ mostRecent: Identity | T;
200
213
  }): void;
201
214
  get cursorParentId(): string | null;
202
215
  get cursorOverFolder(): boolean;
@@ -208,14 +221,14 @@ export declare class TreeApi<T> {
208
221
  drop(): void;
209
222
  hideCursor(): void;
210
223
  showCursor(cursor: Cursor): void;
211
- open(identity: Identity, redraw?: boolean): void;
212
- close(identity: Identity, redraw?: boolean): void;
213
- toggle(identity: Identity): void;
214
- openParents(identity: Identity): void;
224
+ open(identity: Identity | T, redraw?: boolean): void;
225
+ close(identity: Identity | T, redraw?: boolean): void;
226
+ toggle(identity: Identity | T): void;
227
+ openParents(identity: Identity | T): void;
215
228
  openSiblings(node: NodeApi<T>): void;
216
229
  openAll(): void;
217
230
  closeAll(): void;
218
- scrollTo(identity: Identity, align?: Align): Promise<void> | undefined;
231
+ scrollTo(identity: Identity | T, align?: Align): Promise<void> | undefined;
219
232
  get isEditing(): boolean;
220
233
  get isFiltered(): boolean;
221
234
  get hasFocus(): boolean;
@@ -228,10 +241,10 @@ export declare class TreeApi<T> {
228
241
  isDraggable(data: T): boolean;
229
242
  isSelectable(data: T): boolean;
230
243
  private isActionPossible;
231
- isDragging(node: Identity): boolean;
244
+ isDragging(node: Identity | T): boolean;
232
245
  isFocused(id: string): boolean;
233
246
  isMatch(node: NodeApi<T>): boolean;
234
- willReceiveDrop(node: Identity): boolean;
247
+ willReceiveDrop(node: Identity | T): boolean;
235
248
  onFocus(): void;
236
249
  onBlur(): void;
237
250
  onItemsRendered(args: ListOnItemsRenderedProps): void;
@@ -37,6 +37,7 @@ const utils = __importStar(require("../utils"));
37
37
  const default_cursor_1 = require("../components/default-cursor");
38
38
  const default_row_1 = require("../components/default-row");
39
39
  const default_node_1 = require("../components/default-node");
40
+ const node_api_1 = require("./node-api");
40
41
  const edit_slice_1 = require("../state/edit-slice");
41
42
  const focus_slice_1 = require("../state/focus-slice");
42
43
  const create_root_1 = require("../data/create-root");
@@ -47,7 +48,7 @@ const default_drag_preview_1 = require("../components/default-drag-preview");
47
48
  const default_container_1 = require("../components/default-container");
48
49
  const create_list_1 = require("../data/create-list");
49
50
  const create_index_1 = require("../data/create-index");
50
- const { safeRun, identify, identifyNull } = utils;
51
+ const { safeRun } = utils;
51
52
  class TreeApi {
52
53
  constructor(store, props, list, listEl) {
53
54
  this.store = store;
@@ -189,6 +190,30 @@ class TreeApi {
189
190
  throw new Error("Data must contain an 'id' property or props.idAccessor must return a string");
190
191
  return id;
191
192
  }
193
+ /**
194
+ * Resolve an identifier to a node id. Public methods accept an id string, a
195
+ * NodeApi, or the raw row data; this is the one place that turns any of those
196
+ * into the string id used internally. Raw data is run through the configured
197
+ * `idAccessor` so a custom accessor (e.g. `uuid`) is honored everywhere, not
198
+ * just where nodes were built. A NodeApi already carries its accessor-derived
199
+ * `id`, so it is used directly rather than re-accessed (the accessor reads the
200
+ * underlying data, which a NodeApi does not expose under that key). Unlike
201
+ * `accessId`, an unresolved id comes back as `undefined` rather than throwing,
202
+ * preserving the previous behavior of the `id`-only lookup.
203
+ */
204
+ identify(identity) {
205
+ if (typeof identity === "string")
206
+ return identity;
207
+ if (identity instanceof node_api_1.NodeApi)
208
+ return identity.id;
209
+ const get = this.props.idAccessor || "id";
210
+ return utils.access(identity, get);
211
+ }
212
+ identifyNull(identity) {
213
+ if (identity === null || identity === undefined)
214
+ return null;
215
+ return this.identify(identity);
216
+ }
192
217
  /* Node Access */
193
218
  get firstNode() {
194
219
  var _a;
@@ -244,7 +269,7 @@ class TreeApi {
244
269
  return this.visibleNodes.slice(start, end + 1);
245
270
  }
246
271
  indexOf(id) {
247
- const key = utils.identifyNull(id);
272
+ const key = this.identifyNull(id);
248
273
  if (!key)
249
274
  return null;
250
275
  return this.idToIndex[key];
@@ -287,7 +312,7 @@ class TreeApi {
287
312
  if (!node)
288
313
  return;
289
314
  const idents = Array.isArray(node) ? node : [node];
290
- const ids = idents.map(identify);
315
+ const ids = idents.map((i) => this.identify(i));
291
316
  const nodes = ids.map((id) => this.get(id)).filter((n) => !!n);
292
317
  /* Guard against Math.min(...[]) === Infinity when no ids resolve to nodes. */
293
318
  const fromIndex = nodes.length ? Math.min(...nodes.map((n) => { var _a; return (_a = n.rowIndex) !== null && _a !== void 0 ? _a : 0; })) : 0;
@@ -297,7 +322,7 @@ class TreeApi {
297
322
  }
298
323
  edit(node) {
299
324
  var _a, _b;
300
- const id = identify(node);
325
+ const id = this.identify(node);
301
326
  this.resolveEdit({ cancelled: true });
302
327
  this.scrollTo(id);
303
328
  this.dispatch((0, edit_slice_1.edit)(id));
@@ -311,7 +336,7 @@ class TreeApi {
311
336
  var _a, _b;
312
337
  if (!identity)
313
338
  return;
314
- const id = identify(identity);
339
+ const id = this.identify(identity);
315
340
  yield safeRun(this.props.onRename, {
316
341
  id,
317
342
  name: value,
@@ -330,7 +355,7 @@ class TreeApi {
330
355
  setTimeout(() => this.onFocus()); // Return focus to element;
331
356
  }
332
357
  activate(id) {
333
- const node = this.get(identifyNull(id));
358
+ const node = this.get(this.identifyNull(id));
334
359
  if (!node)
335
360
  return;
336
361
  safeRun(this.props.onActivate, node);
@@ -364,7 +389,7 @@ class TreeApi {
364
389
  this.select(node);
365
390
  }
366
391
  else {
367
- this.dispatch((0, focus_slice_1.focus)(identify(node)));
392
+ this.dispatch((0, focus_slice_1.focus)(this.identify(node)));
368
393
  if (opts.scroll !== false)
369
394
  this.scrollTo(node);
370
395
  if (this.focusedNode)
@@ -404,7 +429,7 @@ class TreeApi {
404
429
  if (!node)
405
430
  return;
406
431
  const changeFocus = opts.focus !== false;
407
- const id = identify(node);
432
+ const id = this.identify(node);
408
433
  if (changeFocus)
409
434
  this.dispatch((0, focus_slice_1.focus)(id));
410
435
  if ((_a = this.get(id)) === null || _a === void 0 ? void 0 : _a.isSelectable) {
@@ -422,12 +447,12 @@ class TreeApi {
422
447
  deselect(node) {
423
448
  if (!node)
424
449
  return;
425
- const id = identify(node);
450
+ const id = this.identify(node);
426
451
  this.dispatch(selection_slice_1.actions.remove(id));
427
452
  safeRun(this.props.onSelect, this.selectedNodes);
428
453
  }
429
454
  selectMulti(identity, opts = {}) {
430
- const node = this.get(identifyNull(identity));
455
+ const node = this.get(this.identifyNull(identity));
431
456
  if (!node)
432
457
  return;
433
458
  const changeFocus = opts.focus !== false;
@@ -448,11 +473,11 @@ class TreeApi {
448
473
  var _a;
449
474
  if (!identity)
450
475
  return;
451
- const id = identify(identity);
476
+ const id = this.identify(identity);
452
477
  this.dispatch((0, focus_slice_1.focus)(id));
453
478
  if ((_a = this.get(id)) === null || _a === void 0 ? void 0 : _a.isSelectable) {
454
479
  const { anchor, mostRecent } = this.state.nodes.selection;
455
- const selectableNodes = this.filterSelectableNodes(this.nodesBetween(anchor, identifyNull(id)));
480
+ const selectableNodes = this.filterSelectableNodes(this.nodesBetween(anchor, this.identifyNull(id)));
456
481
  this.dispatch(selection_slice_1.actions.remove(this.nodesBetween(anchor, mostRecent)));
457
482
  this.dispatch(selection_slice_1.actions.add(selectableNodes));
458
483
  this.dispatch(selection_slice_1.actions.mostRecent(id));
@@ -481,14 +506,14 @@ class TreeApi {
481
506
  }
482
507
  filterSelectableNodes(nodes) {
483
508
  return nodes
484
- .map((n) => this.get(identify(n)))
509
+ .map((n) => this.get(this.identify(n)))
485
510
  .filter((n) => !!n && n.isSelectable);
486
511
  }
487
512
  setSelection(args) {
488
513
  var _a;
489
- const ids = new Set((_a = args.ids) === null || _a === void 0 ? void 0 : _a.map(identify));
490
- const anchor = identifyNull(args.anchor);
491
- const mostRecent = identifyNull(args.mostRecent);
514
+ const ids = new Set((_a = args.ids) === null || _a === void 0 ? void 0 : _a.map((i) => this.identify(i)));
515
+ const anchor = this.identifyNull(args.anchor);
516
+ const mostRecent = this.identifyNull(args.mostRecent);
492
517
  this.dispatch(selection_slice_1.actions.set({ ids, anchor, mostRecent }));
493
518
  safeRun(this.props.onSelect, this.selectedNodes);
494
519
  }
@@ -571,7 +596,7 @@ class TreeApi {
571
596
  /* Visibility */
572
597
  open(identity, redraw = true) {
573
598
  var _a, _b;
574
- const id = identifyNull(identity);
599
+ const id = this.identifyNull(identity);
575
600
  if (!id)
576
601
  return;
577
602
  if (this.isOpen(id))
@@ -583,7 +608,7 @@ class TreeApi {
583
608
  }
584
609
  close(identity, redraw = true) {
585
610
  var _a, _b;
586
- const id = identifyNull(identity);
611
+ const id = this.identifyNull(identity);
587
612
  if (!id)
588
613
  return;
589
614
  if (!this.isOpen(id))
@@ -594,13 +619,13 @@ class TreeApi {
594
619
  safeRun(this.props.onToggle, id);
595
620
  }
596
621
  toggle(identity) {
597
- const id = identifyNull(identity);
622
+ const id = this.identifyNull(identity);
598
623
  if (!id)
599
624
  return;
600
625
  return this.isOpen(id) ? this.close(id) : this.open(id);
601
626
  }
602
627
  openParents(identity) {
603
- const id = identifyNull(identity);
628
+ const id = this.identifyNull(identity);
604
629
  if (!id)
605
630
  return;
606
631
  const node = utils.dfs(this.root, id);
@@ -648,7 +673,7 @@ class TreeApi {
648
673
  scrollTo(identity, align = "smart") {
649
674
  if (!identity)
650
675
  return;
651
- const id = identify(identity);
676
+ const id = this.identify(identity);
652
677
  this.openParents(id);
653
678
  return utils
654
679
  .waitFor(() => id in this.idToIndex)
@@ -715,7 +740,7 @@ class TreeApi {
715
740
  return !utils.access(data, disabler);
716
741
  }
717
742
  isDragging(node) {
718
- const id = identifyNull(node);
743
+ const id = this.identifyNull(node);
719
744
  if (!id)
720
745
  return false;
721
746
  return this.state.nodes.drag.id === id;
@@ -727,7 +752,7 @@ class TreeApi {
727
752
  return this.matchFn(node);
728
753
  }
729
754
  willReceiveDrop(node) {
730
- const id = identifyNull(node);
755
+ const id = this.identifyNull(node);
731
756
  if (!id)
732
757
  return false;
733
758
  const { destinationParentId, destinationIndex } = this.state.nodes.drag;