@react-aria/collections 3.0.0-rc.4 → 3.0.0-rc.6
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/dist/BaseCollection.main.js +105 -100
- package/dist/BaseCollection.main.js.map +1 -1
- package/dist/BaseCollection.mjs +101 -101
- package/dist/BaseCollection.module.js +101 -101
- package/dist/BaseCollection.module.js.map +1 -1
- package/dist/CollectionBuilder.main.js +19 -10
- package/dist/CollectionBuilder.main.js.map +1 -1
- package/dist/CollectionBuilder.mjs +20 -11
- package/dist/CollectionBuilder.module.js +20 -11
- package/dist/CollectionBuilder.module.js.map +1 -1
- package/dist/Document.main.js +19 -14
- package/dist/Document.main.js.map +1 -1
- package/dist/Document.mjs +19 -14
- package/dist/Document.module.js +19 -14
- package/dist/Document.module.js.map +1 -1
- package/dist/import.mjs +2 -2
- package/dist/main.js +5 -0
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -2
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +31 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/BaseCollection.ts +119 -123
- package/src/CollectionBuilder.tsx +31 -15
- package/src/Document.ts +27 -12
- package/src/index.ts +1 -1
package/src/BaseCollection.ts
CHANGED
|
@@ -17,8 +17,11 @@ export type Mutable<T> = {
|
|
|
17
17
|
-readonly[P in keyof T]: T[P]
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
type FilterFn<T> = (textValue: string, node: Node<T>) => boolean;
|
|
21
|
+
|
|
20
22
|
/** An immutable object representing a Node in a Collection. */
|
|
21
23
|
export class CollectionNode<T> implements Node<T> {
|
|
24
|
+
static readonly type: string;
|
|
22
25
|
readonly type: string;
|
|
23
26
|
readonly key: Key;
|
|
24
27
|
readonly value: T | null = null;
|
|
@@ -38,8 +41,8 @@ export class CollectionNode<T> implements Node<T> {
|
|
|
38
41
|
readonly colSpan: number | null = null;
|
|
39
42
|
readonly colIndex: number | null = null;
|
|
40
43
|
|
|
41
|
-
constructor(
|
|
42
|
-
this.type = type;
|
|
44
|
+
constructor(key: Key) {
|
|
45
|
+
this.type = (this.constructor as typeof CollectionNode).type;
|
|
43
46
|
this.key = key;
|
|
44
47
|
}
|
|
45
48
|
|
|
@@ -47,8 +50,8 @@ export class CollectionNode<T> implements Node<T> {
|
|
|
47
50
|
throw new Error('childNodes is not supported');
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
clone():
|
|
51
|
-
let node: Mutable<
|
|
53
|
+
clone(): this {
|
|
54
|
+
let node: Mutable<this> = new (this.constructor as any)(this.key);
|
|
52
55
|
node.value = this.value;
|
|
53
56
|
node.level = this.level;
|
|
54
57
|
node.hasChildNodes = this.hasChildNodes;
|
|
@@ -67,6 +70,63 @@ export class CollectionNode<T> implements Node<T> {
|
|
|
67
70
|
node.colIndex = this.colIndex;
|
|
68
71
|
return node;
|
|
69
72
|
}
|
|
73
|
+
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
75
|
+
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): CollectionNode<T> | null {
|
|
76
|
+
let clone = this.clone();
|
|
77
|
+
newCollection.addDescendants(clone, collection);
|
|
78
|
+
return clone;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class FilterableNode<T> extends CollectionNode<T> {
|
|
83
|
+
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): CollectionNode<T> | null {
|
|
84
|
+
let [firstKey, lastKey] = filterChildren(collection, newCollection, this.firstChildKey, filterFn);
|
|
85
|
+
let newNode: Mutable<CollectionNode<T>> = this.clone();
|
|
86
|
+
newNode.firstChildKey = firstKey;
|
|
87
|
+
newNode.lastChildKey = lastKey;
|
|
88
|
+
return newNode;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export class HeaderNode extends CollectionNode<unknown> {
|
|
93
|
+
static readonly type = 'header';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class LoaderNode extends CollectionNode<unknown> {
|
|
97
|
+
static readonly type = 'loader';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export class ItemNode<T> extends FilterableNode<T> {
|
|
101
|
+
static readonly type = 'item';
|
|
102
|
+
|
|
103
|
+
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): ItemNode<T> | null {
|
|
104
|
+
if (filterFn(this.textValue, this)) {
|
|
105
|
+
let clone = this.clone();
|
|
106
|
+
newCollection.addDescendants(clone, collection);
|
|
107
|
+
return clone;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export class SectionNode<T> extends FilterableNode<T> {
|
|
115
|
+
static readonly type = 'section';
|
|
116
|
+
|
|
117
|
+
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): SectionNode<T> | null {
|
|
118
|
+
let filteredSection = super.filter(collection, newCollection, filterFn);
|
|
119
|
+
if (filteredSection) {
|
|
120
|
+
if (filteredSection.lastChildKey !== null) {
|
|
121
|
+
let lastChild = collection.getItem(filteredSection.lastChildKey);
|
|
122
|
+
if (lastChild && lastChild.type !== 'header') {
|
|
123
|
+
return filteredSection;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
70
130
|
}
|
|
71
131
|
|
|
72
132
|
/**
|
|
@@ -201,6 +261,15 @@ export class BaseCollection<T> implements ICollection<Node<T>> {
|
|
|
201
261
|
this.keyMap.set(node.key, node);
|
|
202
262
|
}
|
|
203
263
|
|
|
264
|
+
// Deeply add a node and its children to the collection from another collection, primarily used when filtering a collection
|
|
265
|
+
addDescendants(node: CollectionNode<T>, oldCollection: BaseCollection<T>): void {
|
|
266
|
+
this.addNode(node);
|
|
267
|
+
let children = oldCollection.getChildren(node.key);
|
|
268
|
+
for (let child of children) {
|
|
269
|
+
this.addDescendants(child as CollectionNode<T>, oldCollection);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
204
273
|
removeNode(key: Key): void {
|
|
205
274
|
if (this.frozen) {
|
|
206
275
|
throw new Error('Cannot remove a node to a frozen collection');
|
|
@@ -224,134 +293,61 @@ export class BaseCollection<T> implements ICollection<Node<T>> {
|
|
|
224
293
|
this.frozen = !isSSR;
|
|
225
294
|
}
|
|
226
295
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
// for updating the next/prevKey for every non-filtered node.
|
|
235
|
-
let lastNode: Mutable<CollectionNode<T>> | null = null;
|
|
236
|
-
|
|
237
|
-
for (let node of this) {
|
|
238
|
-
if (node.type === 'section' && node.hasChildNodes) {
|
|
239
|
-
let clonedSection: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();
|
|
240
|
-
let lastChildInSection: Mutable<CollectionNode<T>> | null = null;
|
|
241
|
-
for (let child of this.getChildren(node.key)) {
|
|
242
|
-
if (shouldKeepNode(child, filterFn, this, newCollection)) {
|
|
243
|
-
let clonedChild: Mutable<CollectionNode<T>> = (child as CollectionNode<T>).clone();
|
|
244
|
-
// eslint-disable-next-line max-depth
|
|
245
|
-
if (lastChildInSection == null) {
|
|
246
|
-
clonedSection.firstChildKey = clonedChild.key;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// eslint-disable-next-line max-depth
|
|
250
|
-
if (newCollection.firstKey == null) {
|
|
251
|
-
newCollection.firstKey = clonedSection.key;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// eslint-disable-next-line max-depth
|
|
255
|
-
if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {
|
|
256
|
-
lastChildInSection.nextKey = clonedChild.key;
|
|
257
|
-
clonedChild.prevKey = lastChildInSection.key;
|
|
258
|
-
} else {
|
|
259
|
-
clonedChild.prevKey = null;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
clonedChild.nextKey = null;
|
|
263
|
-
newCollection.addNode(clonedChild);
|
|
264
|
-
lastChildInSection = clonedChild;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
296
|
+
filter(filterFn: FilterFn<T>): this {
|
|
297
|
+
let newCollection = new (this.constructor as any)();
|
|
298
|
+
let [firstKey, lastKey] = filterChildren(this, newCollection, this.firstKey, filterFn);
|
|
299
|
+
newCollection?.commit(firstKey, lastKey);
|
|
300
|
+
return newCollection;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
267
303
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
newCollection.removeNode(lastChildInSection.key);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
} else if (node.type === 'separator') {
|
|
292
|
-
// will need to check if previous section key exists, if it does then we add the separator to the collection.
|
|
293
|
-
// After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)
|
|
294
|
-
let clonedSeparator: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();
|
|
295
|
-
clonedSeparator.nextKey = null;
|
|
296
|
-
if (lastNode?.type === 'section') {
|
|
297
|
-
lastNode.nextKey = clonedSeparator.key;
|
|
298
|
-
clonedSeparator.prevKey = lastNode.key;
|
|
299
|
-
lastNode = clonedSeparator;
|
|
300
|
-
newCollection.addNode(clonedSeparator);
|
|
301
|
-
}
|
|
302
|
-
} else {
|
|
303
|
-
// At this point, the node is either a subdialogtrigger node or a standard row/item
|
|
304
|
-
let clonedNode: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();
|
|
305
|
-
if (shouldKeepNode(clonedNode, filterFn, this, newCollection)) {
|
|
306
|
-
if (newCollection.firstKey == null) {
|
|
307
|
-
newCollection.firstKey = clonedNode.key;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (lastNode != null && (lastNode.type !== 'section' && lastNode.type !== 'separator') && lastNode.parentKey === clonedNode.parentKey) {
|
|
311
|
-
lastNode.nextKey = clonedNode.key;
|
|
312
|
-
clonedNode.prevKey = lastNode.key;
|
|
313
|
-
} else {
|
|
314
|
-
clonedNode.prevKey = null;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
clonedNode.nextKey = null;
|
|
318
|
-
newCollection.addNode(clonedNode);
|
|
319
|
-
lastNode = clonedNode;
|
|
320
|
-
}
|
|
304
|
+
function filterChildren<T>(collection: BaseCollection<T>, newCollection: BaseCollection<T>, firstChildKey: Key | null, filterFn: FilterFn<T>): [Key | null, Key | null] {
|
|
305
|
+
// loop over the siblings for firstChildKey
|
|
306
|
+
// create new nodes based on calling node.filter for each child
|
|
307
|
+
// if it returns null then don't include it, otherwise update its prev/next keys
|
|
308
|
+
// add them to the newCollection
|
|
309
|
+
if (firstChildKey == null) {
|
|
310
|
+
return [null, null];
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
let firstNode: Node<T> | null = null;
|
|
314
|
+
let lastNode: Node<T> | null = null;
|
|
315
|
+
let currentNode = collection.getItem(firstChildKey);
|
|
316
|
+
|
|
317
|
+
while (currentNode != null) {
|
|
318
|
+
let newNode: Mutable<CollectionNode<T>> | null = (currentNode as CollectionNode<T>).filter(collection, newCollection, filterFn);
|
|
319
|
+
if (newNode != null) {
|
|
320
|
+
newNode.nextKey = null;
|
|
321
|
+
if (lastNode) {
|
|
322
|
+
newNode.prevKey = lastNode.key;
|
|
323
|
+
lastNode.nextKey = newNode.key;
|
|
321
324
|
}
|
|
322
|
-
}
|
|
323
325
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (lastNode.prevKey != null) {
|
|
327
|
-
lastSection = newCollection.getItem(lastNode.prevKey) as Mutable<CollectionNode<T>>;
|
|
328
|
-
lastSection.nextKey = null;
|
|
326
|
+
if (firstNode == null) {
|
|
327
|
+
firstNode = newNode;
|
|
329
328
|
}
|
|
330
|
-
newCollection.removeNode(lastNode.key);
|
|
331
|
-
lastNode = lastSection;
|
|
332
|
-
}
|
|
333
329
|
|
|
334
|
-
|
|
330
|
+
newCollection.addNode(newNode);
|
|
331
|
+
lastNode = newNode;
|
|
332
|
+
}
|
|
335
333
|
|
|
336
|
-
|
|
334
|
+
currentNode = currentNode.nextKey ? collection.getItem(currentNode.nextKey) : null;
|
|
337
335
|
}
|
|
338
|
-
}
|
|
339
336
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
let
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
337
|
+
// TODO: this is pretty specific to dividers but doesn't feel like there is a good way to get around it since we only can know
|
|
338
|
+
// to filter the last separator in a collection only after performing a filter for the rest of the contents after it
|
|
339
|
+
// Its gross that it needs to live here, might be nice if somehow we could have this live in the separator code
|
|
340
|
+
if (lastNode && lastNode.type === 'separator') {
|
|
341
|
+
let prevKey = lastNode.prevKey;
|
|
342
|
+
newCollection.removeNode(lastNode.key);
|
|
343
|
+
|
|
344
|
+
if (prevKey) {
|
|
345
|
+
lastNode = newCollection.getItem(prevKey) as Mutable<CollectionNode<T>>;
|
|
346
|
+
lastNode.nextKey = null;
|
|
349
347
|
} else {
|
|
350
|
-
|
|
348
|
+
lastNode = null;
|
|
351
349
|
}
|
|
352
|
-
} else if (node.type === 'header') {
|
|
353
|
-
return true;
|
|
354
|
-
} else {
|
|
355
|
-
return filterFn(node.textValue);
|
|
356
350
|
}
|
|
351
|
+
|
|
352
|
+
return [firstNode?.key ?? null, lastNode?.key ?? null];
|
|
357
353
|
}
|
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {BaseCollection} from './BaseCollection';
|
|
13
|
+
import {BaseCollection, CollectionNode} from './BaseCollection';
|
|
14
14
|
import {BaseNode, Document, ElementNode} from './Document';
|
|
15
15
|
import {CachedChildrenOptions, useCachedChildren} from './useCachedChildren';
|
|
16
16
|
import {createPortal} from 'react-dom';
|
|
17
17
|
import {FocusableContext} from '@react-aria/interactions';
|
|
18
|
-
import {forwardRefType, Node} from '@react-types/shared';
|
|
18
|
+
import {forwardRefType, Key, Node} from '@react-types/shared';
|
|
19
19
|
import {Hidden} from './Hidden';
|
|
20
20
|
import React, {createContext, ForwardedRef, forwardRef, JSX, ReactElement, ReactNode, useCallback, useContext, useMemo, useRef, useState} from 'react';
|
|
21
21
|
import {useIsSSR} from '@react-aria/ssr';
|
|
@@ -127,22 +127,39 @@ function useCollectionDocument<T extends object, C extends BaseCollection<T>>(cr
|
|
|
127
127
|
|
|
128
128
|
const SSRContext = createContext<BaseNode<any> | null>(null);
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
export type CollectionNodeClass<T> = {
|
|
131
|
+
new (key: Key): CollectionNode<T>,
|
|
132
|
+
readonly type: string
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
function createCollectionNodeClass(type: string): CollectionNodeClass<any> {
|
|
136
|
+
let NodeClass = class extends CollectionNode<any> {
|
|
137
|
+
static readonly type = type;
|
|
138
|
+
};
|
|
139
|
+
return NodeClass;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function useSSRCollectionNode<T extends Element>(CollectionNodeClass: CollectionNodeClass<T> | string, props: object, ref: ForwardedRef<T>, rendered?: any, children?: ReactNode, render?: (node: Node<any>) => ReactElement) {
|
|
143
|
+
// To prevent breaking change, if CollectionNodeClass is a string, create a CollectionNodeClass using the string as the type
|
|
144
|
+
if (typeof CollectionNodeClass === 'string') {
|
|
145
|
+
CollectionNodeClass = createCollectionNodeClass(CollectionNodeClass);
|
|
146
|
+
}
|
|
147
|
+
|
|
131
148
|
// During SSR, portals are not supported, so the collection children will be wrapped in an SSRContext.
|
|
132
149
|
// Since SSR occurs only once, we assume that the elements are rendered in order and never re-render.
|
|
133
150
|
// Therefore we can create elements in our collection document during render so that they are in the
|
|
134
151
|
// collection by the time we need to use the collection to render to the real DOM.
|
|
135
152
|
// After hydration, we switch to client rendering using the portal.
|
|
136
153
|
let itemRef = useCallback((element: ElementNode<any> | null) => {
|
|
137
|
-
element?.setProps(props, ref, rendered, render);
|
|
138
|
-
}, [props, ref, rendered, render]);
|
|
154
|
+
element?.setProps(props, ref, CollectionNodeClass, rendered, render);
|
|
155
|
+
}, [props, ref, rendered, render, CollectionNodeClass]);
|
|
139
156
|
let parentNode = useContext(SSRContext);
|
|
140
157
|
if (parentNode) {
|
|
141
158
|
// Guard against double rendering in strict mode.
|
|
142
159
|
let element = parentNode.ownerDocument.nodesByProps.get(props);
|
|
143
160
|
if (!element) {
|
|
144
|
-
element = parentNode.ownerDocument.createElement(
|
|
145
|
-
element.setProps(props, ref, rendered, render);
|
|
161
|
+
element = parentNode.ownerDocument.createElement(CollectionNodeClass.type);
|
|
162
|
+
element.setProps(props, ref, CollectionNodeClass, rendered, render);
|
|
146
163
|
parentNode.appendChild(element);
|
|
147
164
|
parentNode.ownerDocument.updateCollection();
|
|
148
165
|
parentNode.ownerDocument.nodesByProps.set(props, element);
|
|
@@ -154,13 +171,12 @@ function useSSRCollectionNode<T extends Element>(Type: string, props: object, re
|
|
|
154
171
|
}
|
|
155
172
|
|
|
156
173
|
// @ts-ignore
|
|
157
|
-
return <
|
|
174
|
+
return <CollectionNodeClass.type ref={itemRef}>{children}</CollectionNodeClass.type>;
|
|
158
175
|
}
|
|
159
176
|
|
|
160
|
-
|
|
161
|
-
export function createLeafComponent<T extends object, P extends object, E extends Element>(
|
|
162
|
-
export function createLeafComponent<
|
|
163
|
-
export function createLeafComponent<P extends object, E extends Element>(type: string, render: (props: P, ref: ForwardedRef<E>, node?: any) => ReactElement | null): (props: P & React.RefAttributes<any>) => ReactElement | null {
|
|
177
|
+
export function createLeafComponent<T extends object, P extends object, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>) => ReactElement | null): (props: P & React.RefAttributes<T>) => ReactElement | null;
|
|
178
|
+
export function createLeafComponent<T extends object, P extends object, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactElement | null): (props: P & React.RefAttributes<T>) => ReactElement | null;
|
|
179
|
+
export function createLeafComponent<P extends object, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>, node?: any) => ReactElement | null): (props: P & React.RefAttributes<any>) => ReactElement | null {
|
|
164
180
|
let Component = ({node}) => render(node.props, node.props.ref, node);
|
|
165
181
|
let Result = (forwardRef as forwardRefType)((props: P, ref: ForwardedRef<E>) => {
|
|
166
182
|
let focusableProps = useContext(FocusableContext);
|
|
@@ -173,7 +189,7 @@ export function createLeafComponent<P extends object, E extends Element>(type: s
|
|
|
173
189
|
}
|
|
174
190
|
|
|
175
191
|
return useSSRCollectionNode(
|
|
176
|
-
|
|
192
|
+
CollectionNodeClass,
|
|
177
193
|
props,
|
|
178
194
|
ref,
|
|
179
195
|
'children' in props ? props.children : null,
|
|
@@ -191,11 +207,11 @@ export function createLeafComponent<P extends object, E extends Element>(type: s
|
|
|
191
207
|
return Result;
|
|
192
208
|
}
|
|
193
209
|
|
|
194
|
-
export function createBranchComponent<T extends object, P extends {children?: any}, E extends Element>(
|
|
210
|
+
export function createBranchComponent<T extends object, P extends {children?: any}, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactElement | null, useChildren: (props: P) => ReactNode = useCollectionChildren): (props: P & React.RefAttributes<E>) => ReactElement | null {
|
|
195
211
|
let Component = ({node}) => render(node.props, node.props.ref, node);
|
|
196
212
|
let Result = (forwardRef as forwardRefType)((props: P, ref: ForwardedRef<E>) => {
|
|
197
213
|
let children = useChildren(props);
|
|
198
|
-
return useSSRCollectionNode(
|
|
214
|
+
return useSSRCollectionNode(CollectionNodeClass, props, ref, null, children, node => <Component node={node} />) ?? <></>;
|
|
199
215
|
});
|
|
200
216
|
// @ts-ignore
|
|
201
217
|
Result.displayName = render.name;
|
package/src/Document.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {BaseCollection, CollectionNode, Mutable} from './BaseCollection';
|
|
14
|
+
import {CollectionNodeClass} from './CollectionBuilder';
|
|
14
15
|
import {CSSProperties, ForwardedRef, ReactElement, ReactNode} from 'react';
|
|
15
16
|
import {Node} from '@react-types/shared';
|
|
16
17
|
|
|
@@ -256,15 +257,14 @@ export class BaseNode<T> {
|
|
|
256
257
|
*/
|
|
257
258
|
export class ElementNode<T> extends BaseNode<T> {
|
|
258
259
|
nodeType = 8; // COMMENT_NODE (we'd use ELEMENT_NODE but React DevTools will fail to get its dimensions)
|
|
259
|
-
|
|
260
|
+
private _node: CollectionNode<T> | null;
|
|
260
261
|
isMutated = true;
|
|
261
262
|
private _index: number = 0;
|
|
262
|
-
hasSetProps = false;
|
|
263
263
|
isHidden = false;
|
|
264
264
|
|
|
265
265
|
constructor(type: string, ownerDocument: Document<T, any>) {
|
|
266
266
|
super(ownerDocument);
|
|
267
|
-
this.
|
|
267
|
+
this._node = null;
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
get index(): number {
|
|
@@ -278,12 +278,24 @@ export class ElementNode<T> extends BaseNode<T> {
|
|
|
278
278
|
|
|
279
279
|
get level(): number {
|
|
280
280
|
if (this.parentNode instanceof ElementNode) {
|
|
281
|
-
return this.parentNode.level + (this.node
|
|
281
|
+
return this.parentNode.level + (this.node?.type === 'item' ? 1 : 0);
|
|
282
282
|
}
|
|
283
283
|
|
|
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
|
+
|
|
287
299
|
/**
|
|
288
300
|
* Lazily gets a mutable instance of a Node. If the node has already
|
|
289
301
|
* been cloned during this update cycle, it just returns the existing one.
|
|
@@ -321,9 +333,16 @@ export class ElementNode<T> extends BaseNode<T> {
|
|
|
321
333
|
}
|
|
322
334
|
}
|
|
323
335
|
|
|
324
|
-
setProps<E extends Element>(obj: {[key: string]: any}, ref: ForwardedRef<E>, rendered?: ReactNode, render?: (node: Node<T>) => ReactElement): void {
|
|
325
|
-
let node
|
|
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;
|
|
326
338
|
let {value, textValue, id, ...props} = obj;
|
|
339
|
+
if (this._node == null) {
|
|
340
|
+
node = new CollectionNodeClass(id ?? `react-aria-${++this.ownerDocument.nodeId}`);
|
|
341
|
+
this.node = node;
|
|
342
|
+
} else {
|
|
343
|
+
node = this.getMutableNode();
|
|
344
|
+
}
|
|
345
|
+
|
|
327
346
|
props.ref = ref;
|
|
328
347
|
node.props = props;
|
|
329
348
|
node.rendered = rendered;
|
|
@@ -331,17 +350,13 @@ export class ElementNode<T> extends BaseNode<T> {
|
|
|
331
350
|
node.value = value;
|
|
332
351
|
node.textValue = textValue || (typeof props.children === 'string' ? props.children : '') || obj['aria-label'] || '';
|
|
333
352
|
if (id != null && id !== node.key) {
|
|
334
|
-
|
|
335
|
-
throw new Error('Cannot change the id of an item');
|
|
336
|
-
}
|
|
337
|
-
node.key = id;
|
|
353
|
+
throw new Error('Cannot change the id of an item');
|
|
338
354
|
}
|
|
339
355
|
|
|
340
356
|
if (props.colSpan != null) {
|
|
341
357
|
node.colSpan = props.colSpan;
|
|
342
358
|
}
|
|
343
359
|
|
|
344
|
-
this.hasSetProps = true;
|
|
345
360
|
if (this.isConnected) {
|
|
346
361
|
this.ownerDocument.queueUpdate();
|
|
347
362
|
}
|
|
@@ -446,7 +461,7 @@ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extend
|
|
|
446
461
|
}
|
|
447
462
|
}
|
|
448
463
|
|
|
449
|
-
collection.addNode(element.node);
|
|
464
|
+
collection.addNode(element.node!);
|
|
450
465
|
}
|
|
451
466
|
|
|
452
467
|
private removeNode(node: ElementNode<T>): void {
|
package/src/index.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
export {CollectionBuilder, Collection, createLeafComponent, createBranchComponent} from './CollectionBuilder';
|
|
14
14
|
export {createHideableComponent, useIsHidden} from './Hidden';
|
|
15
15
|
export {useCachedChildren} from './useCachedChildren';
|
|
16
|
-
export {BaseCollection, CollectionNode} from './BaseCollection';
|
|
16
|
+
export {BaseCollection, CollectionNode, ItemNode, SectionNode, FilterableNode, LoaderNode, HeaderNode} from './BaseCollection';
|
|
17
17
|
|
|
18
18
|
export type {CollectionBuilderProps, CollectionProps} from './CollectionBuilder';
|
|
19
19
|
export type {CachedChildrenOptions} from './useCachedChildren';
|