@react-stately/data 3.11.7 → 3.12.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.
@@ -28,11 +28,11 @@ interface TreeNode<T extends object> {
28
28
  /** A unique key for the tree node. */
29
29
  key: Key,
30
30
  /** The key of the parent node. */
31
- parentKey: Key,
31
+ parentKey?: Key | null,
32
32
  /** The value object for the tree node. */
33
33
  value: T,
34
34
  /** Children of the tree node. */
35
- children: TreeNode<T>[]
35
+ children: TreeNode<T>[] | null
36
36
  }
37
37
 
38
38
  export interface TreeData<T extends object> {
@@ -49,7 +49,7 @@ export interface TreeData<T extends object> {
49
49
  * Gets a node from the tree by key.
50
50
  * @param key - The key of the item to retrieve.
51
51
  */
52
- getItem(key: Key): TreeNode<T>,
52
+ getItem(key: Key): TreeNode<T> | undefined,
53
53
 
54
54
  /**
55
55
  * Inserts an item into a parent node as a child.
@@ -154,7 +154,7 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
154
154
  };
155
155
  }
156
156
 
157
- function updateTree(items: TreeNode<T>[], key: Key, update: (node: TreeNode<T>) => TreeNode<T>, originalMap: Map<Key, TreeNode<T>>) {
157
+ function updateTree(items: TreeNode<T>[], key: Key, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {
158
158
  let node = originalMap.get(key);
159
159
  if (!node) {
160
160
  return {items, nodeMap: originalMap};
@@ -170,8 +170,8 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
170
170
  }
171
171
 
172
172
  // Walk up the tree and update each parent to refer to the new children.
173
- while (node.parentKey) {
174
- let nextParent = map.get(node.parentKey);
173
+ while (node && node.parentKey) {
174
+ let nextParent = map.get(node.parentKey)!;
175
175
  let copy: TreeNode<T> = {
176
176
  key: nextParent.key,
177
177
  parentKey: nextParent.parentKey,
@@ -180,17 +180,18 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
180
180
  };
181
181
 
182
182
  let children = nextParent.children;
183
- if (newNode == null) {
183
+ if (newNode == null && children) {
184
184
  children = children.filter(c => c !== node);
185
185
  }
186
186
 
187
- copy.children = children.map(child => {
187
+ copy.children = children?.map(child => {
188
188
  if (child === node) {
189
- return newNode;
189
+ // newNode cannot be null here due to the above filter.
190
+ return newNode!;
190
191
  }
191
192
 
192
193
  return child;
193
- });
194
+ }) ?? null;
194
195
 
195
196
  map.set(copy.key, copy);
196
197
 
@@ -205,7 +206,8 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
205
206
  return {
206
207
  items: items.map(item => {
207
208
  if (item === node) {
208
- return newNode;
209
+ // newNode cannot be null here due to the above filter.
210
+ return newNode!;
209
211
  }
210
212
 
211
213
  return item;
@@ -216,15 +218,19 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
216
218
 
217
219
  function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {
218
220
  map.set(node.key, node);
219
- for (let child of node.children) {
220
- addNode(child, map);
221
+ if (node.children) {
222
+ for (let child of node.children) {
223
+ addNode(child, map);
224
+ }
221
225
  }
222
226
  }
223
227
 
224
228
  function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {
225
229
  map.delete(node.key);
226
- for (let child of node.children) {
227
- deleteNode(child, map);
230
+ if (node.children) {
231
+ for (let child of node.children) {
232
+ deleteNode(child, map);
233
+ }
228
234
  }
229
235
  }
230
236
 
@@ -257,9 +263,9 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
257
263
  parentKey: parentNode.parentKey,
258
264
  value: parentNode.value,
259
265
  children: [
260
- ...parentNode.children.slice(0, index),
266
+ ...parentNode.children!.slice(0, index),
261
267
  ...newNodes,
262
- ...parentNode.children.slice(index)
268
+ ...parentNode.children!.slice(index)
263
269
  ]
264
270
  }), newMap);
265
271
  });
@@ -270,10 +276,10 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
270
276
  return;
271
277
  }
272
278
 
273
- let parentNode = nodeMap.get(node.parentKey);
279
+ let parentNode = nodeMap.get(node.parentKey!);
274
280
  let nodes = parentNode ? parentNode.children : items;
275
- let index = nodes.indexOf(node);
276
- this.insert(parentNode?.key, index, ...values);
281
+ let index = nodes!.indexOf(node);
282
+ this.insert(parentNode?.key ?? null, index, ...values);
277
283
  },
278
284
  insertAfter(key: Key, ...values: T[]): void {
279
285
  let node = nodeMap.get(key);
@@ -281,10 +287,10 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
281
287
  return;
282
288
  }
283
289
 
284
- let parentNode = nodeMap.get(node.parentKey);
290
+ let parentNode = nodeMap.get(node.parentKey!);
285
291
  let nodes = parentNode ? parentNode.children : items;
286
- let index = nodes.indexOf(node);
287
- this.insert(parentNode?.key, index + 1, ...values);
292
+ let index = nodes!.indexOf(node);
293
+ this.insert(parentNode?.key ?? null, index + 1, ...values);
288
294
  },
289
295
  prepend(parentKey: Key | null, ...values: T[]) {
290
296
  this.insert(parentKey, 0, ...values);
@@ -298,7 +304,7 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
298
304
  return;
299
305
  }
300
306
 
301
- this.insert(parentKey, parentNode.children.length, ...values);
307
+ this.insert(parentKey, parentNode.children!.length, ...values);
302
308
  }
303
309
  },
304
310
  remove(...keys: Key[]) {
@@ -360,9 +366,9 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
360
366
  parentKey: parentNode.parentKey,
361
367
  value: parentNode.value,
362
368
  children: [
363
- ...parentNode.children.slice(0, index),
369
+ ...parentNode.children!.slice(0, index),
364
370
  movedNode,
365
- ...parentNode.children.slice(index)
371
+ ...parentNode.children!.slice(index)
366
372
  ]
367
373
  }), newMap);
368
374
  });