Skip to content

Commit 3c2e92c

Browse files
committed
simplifying setProps logic since we have already have id when calling it
1 parent 739e93f commit 3c2e92c

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

packages/@react-aria/collections/src/CollectionBuilder.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ function useSSRCollectionNode<T extends Element>(CollectionNodeClass: Collection
152152
// collection by the time we need to use the collection to render to the real DOM.
153153
// After hydration, we switch to client rendering using the portal.
154154
let itemRef = useCallback((element: ElementNode<any> | null) => {
155-
element?.setProps(props, ref, rendered, render, CollectionNodeClass);
155+
element?.setProps(props, ref, CollectionNodeClass, rendered, render);
156156
}, [props, ref, rendered, render, CollectionNodeClass]);
157157
let parentNode = useContext(SSRContext);
158158
if (parentNode) {
159159
// Guard against double rendering in strict mode.
160160
let element = parentNode.ownerDocument.nodesByProps.get(props);
161161
if (!element) {
162162
element = parentNode.ownerDocument.createElement(CollectionNodeClass.type);
163-
element.setProps(props, ref, rendered, render, CollectionNodeClass);
163+
element.setProps(props, ref, CollectionNodeClass, rendered, render);
164164
parentNode.appendChild(element);
165165
parentNode.ownerDocument.updateCollection();
166166
parentNode.ownerDocument.nodesByProps.set(props, element);

packages/@react-aria/collections/src/Document.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ export class BaseNode<T> {
257257
*/
258258
export class ElementNode<T> extends BaseNode<T> {
259259
nodeType = 8; // COMMENT_NODE (we'd use ELEMENT_NODE but React DevTools will fail to get its dimensions)
260-
// TODO: running with assumption that setProps will be called before any other calls to node are made so theoretically
261-
// node will be defined
262260
private _node: CollectionNode<T> | null;
263261
isMutated = true;
264262
private _index: number = 0;
@@ -335,17 +333,14 @@ export class ElementNode<T> extends BaseNode<T> {
335333
}
336334
}
337335

338-
setProps<E extends Element>(obj: {[key: string]: any}, ref: ForwardedRef<E>, rendered?: ReactNode, render?: (node: Node<T>) => ReactElement, CollectionNodeClass?: CollectionNodeClass<any>): void {
339-
let node = this.getMutableNode();
336+
setProps<E extends Element>(obj: {[key: string]: any}, ref: ForwardedRef<E>, CollectionNodeClass: CollectionNodeClass<any>, rendered?: ReactNode, render?: (node: Node<T>) => ReactElement): void {
337+
let node;
340338
let {value, textValue, id, ...props} = obj;
341-
342-
343-
// TODO: Flow here is that if this called for first time, aka this.node is undef, call
344-
// this.node = new CollectionNode(type, `react-aria-${++ownerDocument.nodeId}`); but make new TreeNode/MenuNode/etc instead of CollectionNode
345-
// Caveat is this assumes we don't need a node before setProps is called on it
346-
if (node == null && CollectionNodeClass) {
347-
node = new CollectionNodeClass(`react-aria-${++this.ownerDocument.nodeId}`);
339+
if (this._node == null) {
340+
node = new CollectionNodeClass(id ?? `react-aria-${++this.ownerDocument.nodeId}`);
348341
this.node = node;
342+
} else {
343+
node = this.getMutableNode();
349344
}
350345

351346
props.ref = ref;
@@ -355,20 +350,13 @@ export class ElementNode<T> extends BaseNode<T> {
355350
node.value = value;
356351
node.textValue = textValue || (typeof props.children === 'string' ? props.children : '') || obj['aria-label'] || '';
357352
if (id != null && id !== node.key) {
358-
// TODO: still need to use this.hasSetProps so this can run twice (?) instead of setting node.key above
359-
// If we set node.key = id and change this to if (this.node), setting refs fails. If we just check (this.node here), it will fail if the user provides an id
360-
if (this.hasSetProps) {
361-
throw new Error('Cannot change the id of an item');
362-
}
363-
node.key = id;
353+
throw new Error('Cannot change the id of an item');
364354
}
365355

366356
if (props.colSpan != null) {
367357
node.colSpan = props.colSpan;
368358
}
369359

370-
// TODO: still need this, see above comment
371-
this.hasSetProps = true;
372360
if (this.isConnected) {
373361
this.ownerDocument.queueUpdate();
374362
}

0 commit comments

Comments
 (0)