@react-aria/collections 3.0.0-rc.5 → 3.0.0-rc.7

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
@@ -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 {
@@ -416,7 +412,6 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
416
412
  nodeId = 0;
417
413
  nodesByProps: WeakMap<object, ElementNode<T>> = new WeakMap<object, ElementNode<T>>();
418
414
  isMounted = true;
419
- isInitialRender = true;
420
415
  private collection: C;
421
416
  private nextCollection: C | null = null;
422
417
  private subscriptions: Set<() => void> = new Set();
@@ -451,7 +446,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
451
446
  }
452
447
 
453
448
  private addNode(element: ElementNode<T>): void {
454
- if (element.isHidden) {
449
+ if (element.isHidden || element.node == null) {
455
450
  return;
456
451
  }
457
452
 
@@ -462,10 +457,14 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
462
457
  }
463
458
  }
464
459
 
465
- collection.addNode(element.node!);
460
+ collection.addNode(element.node);
466
461
  }
467
462
 
468
463
  private removeNode(node: ElementNode<T>): void {
464
+ if (node.node == null) {
465
+ return;
466
+ }
467
+
469
468
  for (let child of node) {
470
469
  this.removeNode(child);
471
470
  }
@@ -517,16 +516,12 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
517
516
 
518
517
  // Finally, update the collection.
519
518
  if (this.nextCollection) {
520
- this.nextCollection.commit(this.firstVisibleChild?.node.key ?? null, this.lastVisibleChild?.node.key ?? null, this.isSSR);
519
+ this.nextCollection.commit(this.firstVisibleChild?.node?.key ?? null, this.lastVisibleChild?.node?.key ?? null, this.isSSR);
521
520
  if (!this.isSSR) {
522
521
  this.collection = this.nextCollection;
523
522
  this.nextCollection = null;
524
523
  }
525
524
  }
526
-
527
- if (this.isInitialRender) {
528
- this.collection.isComplete = false;
529
- }
530
525
  }
531
526
 
532
527
  queueUpdate(): void {