@react-aria/collections 3.0.0-alpha.1 → 3.0.0-nightly.2986

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.
Files changed (40) hide show
  1. package/README.md +1 -1
  2. package/dist/BaseCollection.main.js +157 -0
  3. package/dist/BaseCollection.main.js.map +1 -0
  4. package/dist/BaseCollection.mjs +151 -0
  5. package/dist/BaseCollection.module.js +151 -0
  6. package/dist/BaseCollection.module.js.map +1 -0
  7. package/dist/CollectionBuilder.main.js +233 -0
  8. package/dist/CollectionBuilder.main.js.map +1 -0
  9. package/dist/CollectionBuilder.mjs +221 -0
  10. package/dist/CollectionBuilder.module.js +221 -0
  11. package/dist/CollectionBuilder.module.js.map +1 -0
  12. package/dist/Document.main.js +316 -0
  13. package/dist/Document.main.js.map +1 -0
  14. package/dist/Document.mjs +311 -0
  15. package/dist/Document.module.js +311 -0
  16. package/dist/Document.module.js.map +1 -0
  17. package/dist/Hidden.main.js +79 -0
  18. package/dist/Hidden.main.js.map +1 -0
  19. package/dist/Hidden.mjs +68 -0
  20. package/dist/Hidden.module.js +68 -0
  21. package/dist/Hidden.module.js.map +1 -0
  22. package/dist/import.mjs +23 -0
  23. package/dist/main.js +27 -348
  24. package/dist/main.js.map +1 -0
  25. package/dist/module.js +17 -318
  26. package/dist/module.js.map +1 -0
  27. package/dist/types.d.ts +81 -24
  28. package/dist/types.d.ts.map +1 -1
  29. package/dist/useCachedChildren.main.js +63 -0
  30. package/dist/useCachedChildren.main.js.map +1 -0
  31. package/dist/useCachedChildren.mjs +58 -0
  32. package/dist/useCachedChildren.module.js +58 -0
  33. package/dist/useCachedChildren.module.js.map +1 -0
  34. package/package.json +17 -10
  35. package/src/BaseCollection.ts +211 -0
  36. package/src/CollectionBuilder.tsx +237 -0
  37. package/src/Document.ts +453 -0
  38. package/src/Hidden.tsx +84 -0
  39. package/src/index.ts +19 -0
  40. package/src/useCachedChildren.ts +70 -0
@@ -0,0 +1,453 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {BaseCollection, Mutable, NodeValue} from './BaseCollection';
14
+ import {ForwardedRef, ReactElement} from 'react';
15
+ import {Node} from '@react-types/shared';
16
+
17
+ // This Collection implementation is perhaps a little unusual. It works by rendering the React tree into a
18
+ // Portal to a fake DOM implementation. This gives us efficient access to the tree of rendered objects, and
19
+ // supports React features like composition and context. We use this fake DOM to access the full set of elements
20
+ // before we render into the real DOM, which allows us to render a subset of the elements (e.g. virtualized scrolling),
21
+ // and compute properties like the total number of items. It also enables keyboard navigation, selection, and other features.
22
+ // React takes care of efficiently rendering components and updating the collection for us via this fake DOM.
23
+ //
24
+ // The DOM is a mutable API, and React expects the node instances to remain stable over time. So the implementation is split
25
+ // into two parts. Each mutable fake DOM node owns an instance of an immutable collection node. When a fake DOM node is updated,
26
+ // it queues a second render for the collection. Multiple updates to a collection can be queued at once. Collection nodes are
27
+ // lazily copied on write, so only the changed nodes need to be cloned. During the second render, the new immutable collection
28
+ // is finalized by updating the map of Key -> Node with the new cloned nodes. Then the new collection is frozen so it can no
29
+ // longer be mutated, and returned to the calling component to render.
30
+
31
+ /**
32
+ * A mutable node in the fake DOM tree. When mutated, it marks itself as dirty
33
+ * and queues an update with the owner document.
34
+ */
35
+ export class BaseNode<T> {
36
+ private _firstChild: ElementNode<T> | null = null;
37
+ private _lastChild: ElementNode<T> | null = null;
38
+ private _previousSibling: ElementNode<T> | null = null;
39
+ private _nextSibling: ElementNode<T> | null = null;
40
+ private _parentNode: BaseNode<T> | null = null;
41
+ ownerDocument: Document<T, any>;
42
+
43
+ constructor(ownerDocument: Document<T, any>) {
44
+ this.ownerDocument = ownerDocument;
45
+ }
46
+
47
+ *[Symbol.iterator]() {
48
+ let node = this.firstChild;
49
+ while (node) {
50
+ yield node;
51
+ node = node.nextSibling;
52
+ }
53
+ }
54
+
55
+ get firstChild() {
56
+ return this._firstChild;
57
+ }
58
+
59
+ set firstChild(firstChild) {
60
+ this._firstChild = firstChild;
61
+ this.ownerDocument.markDirty(this);
62
+ }
63
+
64
+ get lastChild() {
65
+ return this._lastChild;
66
+ }
67
+
68
+ set lastChild(lastChild) {
69
+ this._lastChild = lastChild;
70
+ this.ownerDocument.markDirty(this);
71
+ }
72
+
73
+ get previousSibling() {
74
+ return this._previousSibling;
75
+ }
76
+
77
+ set previousSibling(previousSibling) {
78
+ this._previousSibling = previousSibling;
79
+ this.ownerDocument.markDirty(this);
80
+ }
81
+
82
+ get nextSibling() {
83
+ return this._nextSibling;
84
+ }
85
+
86
+ set nextSibling(nextSibling) {
87
+ this._nextSibling = nextSibling;
88
+ this.ownerDocument.markDirty(this);
89
+ }
90
+
91
+ get parentNode() {
92
+ return this._parentNode;
93
+ }
94
+
95
+ set parentNode(parentNode) {
96
+ this._parentNode = parentNode;
97
+ this.ownerDocument.markDirty(this);
98
+ }
99
+
100
+ get isConnected() {
101
+ return this.parentNode?.isConnected || false;
102
+ }
103
+
104
+ appendChild(child: ElementNode<T>) {
105
+ this.ownerDocument.startTransaction();
106
+ if (child.parentNode) {
107
+ child.parentNode.removeChild(child);
108
+ }
109
+
110
+ if (this.firstChild == null) {
111
+ this.firstChild = child;
112
+ }
113
+
114
+ if (this.lastChild) {
115
+ this.lastChild.nextSibling = child;
116
+ child.index = this.lastChild.index + 1;
117
+ child.previousSibling = this.lastChild;
118
+ } else {
119
+ child.previousSibling = null;
120
+ child.index = 0;
121
+ }
122
+
123
+ child.parentNode = this;
124
+ child.nextSibling = null;
125
+ this.lastChild = child;
126
+
127
+ this.ownerDocument.markDirty(this);
128
+ if (child.hasSetProps) {
129
+ // Only add the node to the collection if we already received props for it.
130
+ // Otherwise wait until then so we have the correct id for the node.
131
+ this.ownerDocument.addNode(child);
132
+ }
133
+
134
+ this.ownerDocument.endTransaction();
135
+ this.ownerDocument.queueUpdate();
136
+ }
137
+
138
+ insertBefore(newNode: ElementNode<T>, referenceNode: ElementNode<T>) {
139
+ if (referenceNode == null) {
140
+ return this.appendChild(newNode);
141
+ }
142
+
143
+ this.ownerDocument.startTransaction();
144
+ if (newNode.parentNode) {
145
+ newNode.parentNode.removeChild(newNode);
146
+ }
147
+
148
+ newNode.nextSibling = referenceNode;
149
+ newNode.previousSibling = referenceNode.previousSibling;
150
+ newNode.index = referenceNode.index;
151
+
152
+ if (this.firstChild === referenceNode) {
153
+ this.firstChild = newNode;
154
+ } else if (referenceNode.previousSibling) {
155
+ referenceNode.previousSibling.nextSibling = newNode;
156
+ }
157
+
158
+ referenceNode.previousSibling = newNode;
159
+ newNode.parentNode = referenceNode.parentNode;
160
+
161
+ let node: ElementNode<T> | null = referenceNode;
162
+ while (node) {
163
+ node.index++;
164
+ node = node.nextSibling;
165
+ }
166
+
167
+ if (newNode.hasSetProps) {
168
+ this.ownerDocument.addNode(newNode);
169
+ }
170
+
171
+ this.ownerDocument.endTransaction();
172
+ this.ownerDocument.queueUpdate();
173
+ }
174
+
175
+ removeChild(child: ElementNode<T>) {
176
+ if (child.parentNode !== this || !this.ownerDocument.isMounted) {
177
+ return;
178
+ }
179
+
180
+ this.ownerDocument.startTransaction();
181
+ let node = child.nextSibling;
182
+ while (node) {
183
+ node.index--;
184
+ node = node.nextSibling;
185
+ }
186
+
187
+ if (child.nextSibling) {
188
+ child.nextSibling.previousSibling = child.previousSibling;
189
+ }
190
+
191
+ if (child.previousSibling) {
192
+ child.previousSibling.nextSibling = child.nextSibling;
193
+ }
194
+
195
+ if (this.firstChild === child) {
196
+ this.firstChild = child.nextSibling;
197
+ }
198
+
199
+ if (this.lastChild === child) {
200
+ this.lastChild = child.previousSibling;
201
+ }
202
+
203
+ child.parentNode = null;
204
+ child.nextSibling = null;
205
+ child.previousSibling = null;
206
+ child.index = 0;
207
+
208
+ this.ownerDocument.removeNode(child);
209
+ this.ownerDocument.endTransaction();
210
+ this.ownerDocument.queueUpdate();
211
+ }
212
+
213
+ addEventListener() {}
214
+ removeEventListener() {}
215
+ }
216
+
217
+ /**
218
+ * A mutable element node in the fake DOM tree. It owns an immutable
219
+ * Collection Node which is copied on write.
220
+ */
221
+ export class ElementNode<T> extends BaseNode<T> {
222
+ nodeType = 8; // COMMENT_NODE (we'd use ELEMENT_NODE but React DevTools will fail to get its dimensions)
223
+ node: NodeValue<T>;
224
+ private _index: number = 0;
225
+ hasSetProps = false;
226
+
227
+ constructor(type: string, ownerDocument: Document<T, any>) {
228
+ super(ownerDocument);
229
+ this.node = new NodeValue(type, `react-aria-${++ownerDocument.nodeId}`);
230
+ // Start a transaction so that no updates are emitted from the collection
231
+ // until the props for this node are set. We don't know the real id for the
232
+ // node until then, so we need to avoid emitting collections in an inconsistent state.
233
+ this.ownerDocument.startTransaction();
234
+ }
235
+
236
+ get index() {
237
+ return this._index;
238
+ }
239
+
240
+ set index(index) {
241
+ this._index = index;
242
+ this.ownerDocument.markDirty(this);
243
+ }
244
+
245
+ get level(): number {
246
+ if (this.parentNode instanceof ElementNode) {
247
+ return this.parentNode.level + (this.node.type === 'item' ? 1 : 0);
248
+ }
249
+
250
+ return 0;
251
+ }
252
+
253
+ updateNode() {
254
+ let node = this.ownerDocument.getMutableNode(this);
255
+ node.index = this.index;
256
+ node.level = this.level;
257
+ node.parentKey = this.parentNode instanceof ElementNode ? this.parentNode.node.key : null;
258
+ node.prevKey = this.previousSibling?.node.key ?? null;
259
+ node.nextKey = this.nextSibling?.node.key ?? null;
260
+ node.hasChildNodes = !!this.firstChild;
261
+ node.firstChildKey = this.firstChild?.node.key ?? null;
262
+ node.lastChildKey = this.lastChild?.node.key ?? null;
263
+ }
264
+
265
+ setProps<E extends Element>(obj: any, ref: ForwardedRef<E>, rendered?: any, render?: (node: Node<T>) => ReactElement) {
266
+ let node = this.ownerDocument.getMutableNode(this);
267
+ let {value, textValue, id, ...props} = obj;
268
+ props.ref = ref;
269
+ node.props = props;
270
+ node.rendered = rendered;
271
+ node.render = render;
272
+ node.value = value;
273
+ node.textValue = textValue || (typeof props.children === 'string' ? props.children : '') || obj['aria-label'] || '';
274
+ if (id != null && id !== node.key) {
275
+ if (this.hasSetProps) {
276
+ throw new Error('Cannot change the id of an item');
277
+ }
278
+ node.key = id;
279
+ }
280
+
281
+ // If this is the first time props have been set, end the transaction started in the constructor
282
+ // so this node can be emitted.
283
+ if (!this.hasSetProps) {
284
+ this.ownerDocument.addNode(this);
285
+ this.ownerDocument.endTransaction();
286
+ this.hasSetProps = true;
287
+ }
288
+
289
+ this.ownerDocument.queueUpdate();
290
+ }
291
+
292
+ get style() {
293
+ return {};
294
+ }
295
+
296
+ hasAttribute() {}
297
+ setAttribute() {}
298
+ setAttributeNS() {}
299
+ removeAttribute() {}
300
+ }
301
+
302
+ /**
303
+ * A mutable Document in the fake DOM. It owns an immutable Collection instance,
304
+ * which is lazily copied on write during updates.
305
+ */
306
+ export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extends BaseNode<T> {
307
+ nodeType = 11; // DOCUMENT_FRAGMENT_NODE
308
+ ownerDocument = this;
309
+ dirtyNodes: Set<BaseNode<T>> = new Set();
310
+ isSSR = false;
311
+ nodeId = 0;
312
+ nodesByProps = new WeakMap<object, ElementNode<T>>();
313
+ isMounted = true;
314
+ private collection: C;
315
+ private collectionMutated: boolean;
316
+ private mutatedNodes: Set<ElementNode<T>> = new Set();
317
+ private subscriptions: Set<() => void> = new Set();
318
+ private transactionCount = 0;
319
+
320
+ constructor(collection: C) {
321
+ // @ts-ignore
322
+ super(null);
323
+ this.collection = collection;
324
+ this.collectionMutated = true;
325
+ }
326
+
327
+ get isConnected() {
328
+ return this.isMounted;
329
+ }
330
+
331
+ createElement(type: string) {
332
+ return new ElementNode(type, this);
333
+ }
334
+
335
+ /**
336
+ * Lazily gets a mutable instance of a Node. If the node has already
337
+ * been cloned during this update cycle, it just returns the existing one.
338
+ */
339
+ getMutableNode(element: ElementNode<T>): Mutable<NodeValue<T>> {
340
+ let node = element.node;
341
+ if (!this.mutatedNodes.has(element)) {
342
+ node = element.node.clone();
343
+ this.mutatedNodes.add(element);
344
+ element.node = node;
345
+ }
346
+ this.markDirty(element);
347
+ return node;
348
+ }
349
+
350
+ private getMutableCollection() {
351
+ if (!this.isSSR && !this.collectionMutated) {
352
+ this.collection = this.collection.clone();
353
+ this.collectionMutated = true;
354
+ }
355
+
356
+ return this.collection;
357
+ }
358
+
359
+ markDirty(node: BaseNode<T>) {
360
+ this.dirtyNodes.add(node);
361
+ }
362
+
363
+ startTransaction() {
364
+ this.transactionCount++;
365
+ }
366
+
367
+ endTransaction() {
368
+ this.transactionCount--;
369
+ }
370
+
371
+ addNode(element: ElementNode<T>) {
372
+ let collection = this.getMutableCollection();
373
+ if (!collection.getItem(element.node.key)) {
374
+ collection.addNode(element.node);
375
+
376
+ for (let child of element) {
377
+ this.addNode(child);
378
+ }
379
+ }
380
+
381
+ this.markDirty(element);
382
+ }
383
+
384
+ removeNode(node: ElementNode<T>) {
385
+ for (let child of node) {
386
+ this.removeNode(child);
387
+ }
388
+
389
+ let collection = this.getMutableCollection();
390
+ collection.removeNode(node.node.key);
391
+ this.markDirty(node);
392
+ }
393
+
394
+ /** Finalizes the collection update, updating all nodes and freezing the collection. */
395
+ getCollection(): C {
396
+ if (this.transactionCount > 0) {
397
+ return this.collection;
398
+ }
399
+
400
+ this.updateCollection();
401
+ return this.collection;
402
+ }
403
+
404
+ updateCollection() {
405
+ for (let element of this.dirtyNodes) {
406
+ if (element instanceof ElementNode && element.isConnected) {
407
+ element.updateNode();
408
+ }
409
+ }
410
+
411
+ this.dirtyNodes.clear();
412
+
413
+ if (this.mutatedNodes.size || this.collectionMutated) {
414
+ let collection = this.getMutableCollection();
415
+ for (let element of this.mutatedNodes) {
416
+ if (element.isConnected) {
417
+ collection.addNode(element.node);
418
+ }
419
+ }
420
+
421
+ collection.commit(this.firstChild?.node.key ?? null, this.lastChild?.node.key ?? null, this.isSSR);
422
+ this.mutatedNodes.clear();
423
+ }
424
+
425
+ this.collectionMutated = false;
426
+ }
427
+
428
+ queueUpdate() {
429
+ // Don't emit any updates if there is a transaction in progress.
430
+ // queueUpdate should be called again after the transaction.
431
+ if (this.dirtyNodes.size === 0 || this.transactionCount > 0) {
432
+ return;
433
+ }
434
+
435
+ for (let fn of this.subscriptions) {
436
+ fn();
437
+ }
438
+ }
439
+
440
+ subscribe(fn: () => void) {
441
+ this.subscriptions.add(fn);
442
+ return () => this.subscriptions.delete(fn);
443
+ }
444
+
445
+ resetAfterSSR() {
446
+ if (this.isSSR) {
447
+ this.isSSR = false;
448
+ this.firstChild = null;
449
+ this.lastChild = null;
450
+ this.nodeId = 0;
451
+ }
452
+ }
453
+ }
package/src/Hidden.tsx ADDED
@@ -0,0 +1,84 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {createPortal} from 'react-dom';
14
+ import {forwardRefType} from '@react-types/shared';
15
+ import React, {createContext, forwardRef, ReactNode, useContext} from 'react';
16
+ import {useIsSSR} from '@react-aria/ssr';
17
+
18
+ // React doesn't understand the <template> element, which doesn't have children like a normal element.
19
+ // It will throw an error during hydration when it expects the firstChild to contain content rendered
20
+ // on the server, when in reality, the browser will have placed this inside the `content` document fragment.
21
+ // This monkey patches the firstChild property for our special hidden template elements to work around this error.
22
+ // See https://github.com/facebook/react/issues/19932
23
+ if (typeof HTMLTemplateElement !== 'undefined') {
24
+ const getFirstChild = Object.getOwnPropertyDescriptor(Node.prototype, 'firstChild')!.get!;
25
+ Object.defineProperty(HTMLTemplateElement.prototype, 'firstChild', {
26
+ configurable: true,
27
+ enumerable: true,
28
+ get: function () {
29
+ if (this.dataset.reactAriaHidden) {
30
+ return this.content.firstChild;
31
+ } else {
32
+ return getFirstChild.call(this);
33
+ }
34
+ }
35
+ });
36
+ }
37
+
38
+ export const HiddenContext = createContext<boolean>(false);
39
+
40
+ // Portal to nowhere
41
+ const hiddenFragment = typeof DocumentFragment !== 'undefined' ? new DocumentFragment() : null;
42
+
43
+ export function Hidden(props: {children: ReactNode}) {
44
+ let isHidden = useContext(HiddenContext);
45
+ let isSSR = useIsSSR();
46
+ if (isHidden) {
47
+ // Don't hide again if we are already hidden.
48
+ return <>{props.children}</>;
49
+ }
50
+
51
+ let children = (
52
+ <HiddenContext.Provider value>
53
+ {props.children}
54
+ </HiddenContext.Provider>
55
+ );
56
+
57
+ // In SSR, portals are not supported by React. Instead, render into a <template>
58
+ // element, which the browser will never display to the user. In addition, the
59
+ // content is not part of the DOM tree, so it won't affect ids or other accessibility attributes.
60
+ return isSSR
61
+ ? <template data-react-aria-hidden>{children}</template>
62
+ : createPortal(children, hiddenFragment!);
63
+ }
64
+
65
+ /** Creates a component that forwards its ref and returns null if it is in a hidden subtree. */
66
+ // Note: this function is handled specially in the documentation generator. If you change it, you'll need to update DocsTransformer as well.
67
+ export function createHideableComponent<T, P = {}>(fn: (props: P, ref: React.Ref<T>) => ReactNode | null): (props: P & React.RefAttributes<T>) => ReactNode | null {
68
+ let Wrapper = (props: P, ref: React.Ref<T>) => {
69
+ let isHidden = useContext(HiddenContext);
70
+ if (isHidden) {
71
+ return null;
72
+ }
73
+
74
+ return fn(props, ref);
75
+ };
76
+ // @ts-ignore - for react dev tools
77
+ Wrapper.displayName = fn.displayName || fn.name;
78
+ return (forwardRef as forwardRefType)(Wrapper);
79
+ }
80
+
81
+ /** Returns whether the component is in a hidden subtree. */
82
+ export function useIsHidden(): boolean {
83
+ return useContext(HiddenContext);
84
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ export {CollectionBuilder, Collection, createLeafComponent, createBranchComponent} from './CollectionBuilder';
14
+ export {createHideableComponent, useIsHidden} from './Hidden';
15
+ export {useCachedChildren} from './useCachedChildren';
16
+ export {BaseCollection, NodeValue} from './BaseCollection';
17
+
18
+ export type {CollectionBuilderProps, CollectionProps} from './CollectionBuilder';
19
+ export type {CachedChildrenOptions} from './useCachedChildren';
@@ -0,0 +1,70 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {cloneElement, ReactElement, ReactNode, useMemo} from 'react';
14
+ import {Key} from '@react-types/shared';
15
+
16
+ export interface CachedChildrenOptions<T> {
17
+ /** Item objects in the collection. */
18
+ items?: Iterable<T>,
19
+ /** The contents of the collection. */
20
+ children?: ReactNode | ((item: T) => ReactNode),
21
+ /** Values that should invalidate the item cache when using dynamic collections. */
22
+ dependencies?: any[],
23
+ /** A scope to prepend to all child item ids to ensure they are unique. */
24
+ idScope?: Key,
25
+ /** Whether to add `id` and `value` props to all child items. */
26
+ addIdAndValue?: boolean
27
+ }
28
+
29
+ /**
30
+ * Maps over a list of items and renders React elements for them. Each rendered item is
31
+ * cached based on object identity, and React keys are generated from the `key` or `id` property.
32
+ */
33
+ export function useCachedChildren<T extends object>(props: CachedChildrenOptions<T>): ReactNode {
34
+ let {children, items, idScope, addIdAndValue, dependencies = []} = props;
35
+
36
+ // Invalidate the cache whenever the parent value changes.
37
+ // eslint-disable-next-line react-hooks/exhaustive-deps
38
+ let cache = useMemo(() => new WeakMap(), dependencies);
39
+ return useMemo(() => {
40
+ if (items && typeof children === 'function') {
41
+ let res: ReactElement[] = [];
42
+ for (let item of items) {
43
+ let rendered = cache.get(item);
44
+ if (!rendered) {
45
+ rendered = children(item);
46
+ // @ts-ignore
47
+ let key = rendered.props.id ?? item.key ?? item.id;
48
+ // eslint-disable-next-line max-depth
49
+ if (key == null) {
50
+ throw new Error('Could not determine key for item');
51
+ }
52
+ // eslint-disable-next-line max-depth
53
+ if (idScope) {
54
+ key = idScope + ':' + key;
55
+ }
56
+ // Note: only works if wrapped Item passes through id...
57
+ rendered = cloneElement(
58
+ rendered,
59
+ addIdAndValue ? {key, id: key, value: item} : {key}
60
+ );
61
+ cache.set(item, rendered);
62
+ }
63
+ res.push(rendered);
64
+ }
65
+ return res;
66
+ } else if (typeof children !== 'function') {
67
+ return children;
68
+ }
69
+ }, [children, items, cache, idScope, addIdAndValue]);
70
+ }