@react-aria/collections 3.0.3 → 3.1.0

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/dist/import.mjs +7 -5
  2. package/dist/main.js +20 -18
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +7 -5
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +8 -0
  7. package/package.json +14 -15
  8. package/src/index.ts +8 -7
  9. package/dist/BaseCollection.main.js +0 -271
  10. package/dist/BaseCollection.main.js.map +0 -1
  11. package/dist/BaseCollection.mjs +0 -260
  12. package/dist/BaseCollection.module.js +0 -260
  13. package/dist/BaseCollection.module.js.map +0 -1
  14. package/dist/CollectionBuilder.main.js +0 -242
  15. package/dist/CollectionBuilder.main.js.map +0 -1
  16. package/dist/CollectionBuilder.mjs +0 -230
  17. package/dist/CollectionBuilder.module.js +0 -230
  18. package/dist/CollectionBuilder.module.js.map +0 -1
  19. package/dist/Document.main.js +0 -369
  20. package/dist/Document.main.js.map +0 -1
  21. package/dist/Document.mjs +0 -364
  22. package/dist/Document.module.js +0 -364
  23. package/dist/Document.module.js.map +0 -1
  24. package/dist/Hidden.main.js +0 -90
  25. package/dist/Hidden.main.js.map +0 -1
  26. package/dist/Hidden.mjs +0 -79
  27. package/dist/Hidden.module.js +0 -79
  28. package/dist/Hidden.module.js.map +0 -1
  29. package/dist/types.d.ts +0 -119
  30. package/dist/types.d.ts.map +0 -1
  31. package/dist/useCachedChildren.main.js +0 -61
  32. package/dist/useCachedChildren.main.js.map +0 -1
  33. package/dist/useCachedChildren.mjs +0 -56
  34. package/dist/useCachedChildren.module.js +0 -56
  35. package/dist/useCachedChildren.module.js.map +0 -1
  36. package/src/BaseCollection.ts +0 -353
  37. package/src/CollectionBuilder.tsx +0 -265
  38. package/src/Document.ts +0 -569
  39. package/src/Hidden.tsx +0 -97
  40. package/src/useCachedChildren.ts +0 -70
@@ -1,70 +0,0 @@
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?: ReadonlyArray<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
-
49
- if (key == null) {
50
- throw new Error('Could not determine key for item');
51
- }
52
-
53
- if (idScope != null) {
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
- }