@react-aria/collections 3.0.0-rc.6 → 3.0.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/src/Document.ts CHANGED
@@ -179,7 +179,7 @@ export class BaseNode<T> {
179
179
  }
180
180
 
181
181
  removeChild(child: ElementNode<T>): void {
182
- if (child.parentNode !== this || !this.ownerDocument.isMounted) {
182
+ if (child.parentNode !== this) {
183
183
  return;
184
184
  }
185
185
 
@@ -257,14 +257,14 @@ export class BaseNode<T> {
257
257
  */
258
258
  export class ElementNode<T> extends BaseNode<T> {
259
259
  nodeType = 8; // COMMENT_NODE (we'd use ELEMENT_NODE but React DevTools will fail to get its dimensions)
260
- private _node: CollectionNode<T> | null;
260
+ node: CollectionNode<T> | null;
261
261
  isMutated = true;
262
262
  private _index: number = 0;
263
263
  isHidden = false;
264
264
 
265
265
  constructor(type: string, ownerDocument: Document<T, any>) {
266
266
  super(ownerDocument);
267
- this._node = null;
267
+ this.node = null;
268
268
  }
269
269
 
270
270
  get index(): number {
@@ -284,23 +284,15 @@ export class ElementNode<T> extends BaseNode<T> {
284
284
  return 0;
285
285
  }
286
286
 
287
- get node(): CollectionNode<T> {
288
- if (this._node == null) {
289
- throw Error('Attempted to access node before it was defined. Check if setProps wasn\'t called before attempting to access the node.');
290
- }
291
-
292
- return this._node;
293
- }
294
-
295
- set node(node: CollectionNode<T>) {
296
- this._node = node;
297
- }
298
-
299
287
  /**
300
288
  * Lazily gets a mutable instance of a Node. If the node has already
301
289
  * been cloned during this update cycle, it just returns the existing one.
302
290
  */
303
- private getMutableNode(): Mutable<CollectionNode<T>> {
291
+ private getMutableNode(): Mutable<CollectionNode<T>> | null {
292
+ if (this.node == null) {
293
+ return null;
294
+ }
295
+
304
296
  if (!this.isMutated) {
305
297
  this.node = this.node.clone();
306
298
  this.isMutated = true;
@@ -313,22 +305,26 @@ export class ElementNode<T> extends BaseNode<T> {
313
305
  updateNode(): void {
314
306
  let nextSibling = this.nextVisibleSibling;
315
307
  let node = this.getMutableNode();
308
+ if (node == null) {
309
+ return;
310
+ }
311
+
316
312
  node.index = this.index;
317
313
  node.level = this.level;
318
- node.parentKey = this.parentNode instanceof ElementNode ? this.parentNode.node.key : null;
319
- node.prevKey = this.previousVisibleSibling?.node.key ?? null;
320
- node.nextKey = nextSibling?.node.key ?? null;
314
+ node.parentKey = this.parentNode instanceof ElementNode ? this.parentNode.node?.key ?? null : null;
315
+ node.prevKey = this.previousVisibleSibling?.node?.key ?? null;
316
+ node.nextKey = nextSibling?.node?.key ?? null;
321
317
  node.hasChildNodes = !!this.firstChild;
322
- node.firstChildKey = this.firstVisibleChild?.node.key ?? null;
323
- node.lastChildKey = this.lastVisibleChild?.node.key ?? null;
318
+ node.firstChildKey = this.firstVisibleChild?.node?.key ?? null;
319
+ node.lastChildKey = this.lastVisibleChild?.node?.key ?? null;
324
320
 
325
321
  // Update the colIndex of sibling nodes if this node has a colSpan.
326
322
  if ((node.colSpan != null || node.colIndex != null) && nextSibling) {
327
323
  // This queues the next sibling for update, which means this happens recursively.
328
324
  let nextColIndex = (node.colIndex ?? node.index) + (node.colSpan ?? 1);
329
- if (nextColIndex !== nextSibling.node.colIndex) {
325
+ if (nextSibling.node != null && nextColIndex !== nextSibling.node.colIndex) {
330
326
  let siblingNode = nextSibling.getMutableNode();
331
- siblingNode.colIndex = nextColIndex;
327
+ siblingNode!.colIndex = nextColIndex;
332
328
  }
333
329
  }
334
330
  }
@@ -336,7 +332,7 @@ export class ElementNode<T> extends BaseNode<T> {
336
332
  setProps<E extends Element>(obj: {[key: string]: any}, ref: ForwardedRef<E>, CollectionNodeClass: CollectionNodeClass<any>, rendered?: ReactNode, render?: (node: Node<T>) => ReactElement): void {
337
333
  let node;
338
334
  let {value, textValue, id, ...props} = obj;
339
- if (this._node == null) {
335
+ if (this.node == null) {
340
336
  node = new CollectionNodeClass(id ?? `react-aria-${++this.ownerDocument.nodeId}`);
341
337
  this.node = node;
342
338
  } else {
@@ -415,7 +411,6 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
415
411
  isSSR = false;
416
412
  nodeId = 0;
417
413
  nodesByProps: WeakMap<object, ElementNode<T>> = new WeakMap<object, ElementNode<T>>();
418
- isMounted = true;
419
414
  private collection: C;
420
415
  private nextCollection: C | null = null;
421
416
  private subscriptions: Set<() => void> = new Set();
@@ -430,7 +425,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
430
425
  }
431
426
 
432
427
  get isConnected(): boolean {
433
- return this.isMounted;
428
+ return true;
434
429
  }
435
430
 
436
431
  createElement(type: string): ElementNode<T> {
@@ -450,7 +445,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
450
445
  }
451
446
 
452
447
  private addNode(element: ElementNode<T>): void {
453
- if (element.isHidden) {
448
+ if (element.isHidden || element.node == null) {
454
449
  return;
455
450
  }
456
451
 
@@ -461,7 +456,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
461
456
  }
462
457
  }
463
458
 
464
- collection.addNode(element.node!);
459
+ collection.addNode(element.node);
465
460
  }
466
461
 
467
462
  private removeNode(node: ElementNode<T>): void {
@@ -469,8 +464,10 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
469
464
  this.removeNode(child);
470
465
  }
471
466
 
472
- let collection = this.getMutableCollection();
473
- collection.removeNode(node.node.key);
467
+ if (node.node) {
468
+ let collection = this.getMutableCollection();
469
+ collection.removeNode(node.node.key);
470
+ }
474
471
  }
475
472
 
476
473
  /** Finalizes the collection update, updating all nodes and freezing the collection. */
@@ -508,15 +505,19 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
508
505
  this.addNode(element);
509
506
  }
510
507
 
508
+ if (element.node) {
509
+ this.dirtyNodes.delete(element);
510
+ }
511
+
511
512
  element.isMutated = false;
513
+ } else {
514
+ this.dirtyNodes.delete(element);
512
515
  }
513
516
  }
514
517
 
515
- this.dirtyNodes.clear();
516
-
517
518
  // Finally, update the collection.
518
519
  if (this.nextCollection) {
519
- this.nextCollection.commit(this.firstVisibleChild?.node.key ?? null, this.lastVisibleChild?.node.key ?? null, this.isSSR);
520
+ this.nextCollection.commit(this.firstVisibleChild?.node?.key ?? null, this.lastVisibleChild?.node?.key ?? null, this.isSSR);
520
521
  if (!this.isSSR) {
521
522
  this.collection = this.nextCollection;
522
523
  this.nextCollection = null;