@@ -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,30 +305,34 @@ 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
}
335
331
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 {
@@ -450,7 +446,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
450
446
}
451
447
452
448
private addNode ( element : ElementNode < T > ) : void {
453
- if ( element . isHidden ) {
449
+ if ( element . isHidden || element . node == null ) {
454
450
return ;
455
451
}
456
452
@@ -461,10 +457,14 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
461
457
}
462
458
}
463
459
464
- collection . addNode ( element . node ! ) ;
460
+ collection . addNode ( element . node ) ;
465
461
}
466
462
467
463
private removeNode ( node : ElementNode < T > ) : void {
464
+ if ( node . node == null ) {
465
+ return ;
466
+ }
467
+
468
468
for ( let child of node ) {
469
469
this . removeNode ( child ) ;
470
470
}
@@ -516,7 +516,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
516
516
517
517
// Finally, update the collection.
518
518
if ( this . nextCollection ) {
519
- 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 ) ;
520
520
if ( ! this . isSSR ) {
521
521
this . collection = this . nextCollection ;
522
522
this . nextCollection = null ;
0 commit comments