@react-stately/collections 3.5.1 → 3.6.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 (2) hide show
  1. package/dist/import.mjs +353 -0
  2. package/package.json +8 -3
@@ -0,0 +1,353 @@
1
+ import $tyW6A$react, {useMemo as $tyW6A$useMemo, useRef as $tyW6A$useRef} from "react";
2
+ import $tyW6A$swchelperssrc_define_propertymjs from "@swc/helpers/src/_define_property.mjs";
3
+
4
+ /*
5
+ * Copyright 2020 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */ /*
15
+ * Copyright 2020 Adobe. All rights reserved.
16
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
17
+ * you may not use this file except in compliance with the License. You may obtain a copy
18
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
19
+ *
20
+ * Unless required by applicable law or agreed to in writing, software distributed under
21
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
22
+ * OF ANY KIND, either express or implied. See the License for the specific language
23
+ * governing permissions and limitations under the License.
24
+ */
25
+ function $c1d7fb2ec91bae71$var$Item(props) {
26
+ return null;
27
+ }
28
+ $c1d7fb2ec91bae71$var$Item.getCollectionNode = function* getCollectionNode(props, context) {
29
+ let { childItems: childItems , title: title , children: children } = props;
30
+ let rendered = props.title || props.children;
31
+ let textValue = props.textValue || (typeof rendered === "string" ? rendered : "") || props["aria-label"] || "";
32
+ // suppressTextValueWarning is used in components like Tabs, which don't have type to select support.
33
+ if (!textValue && !(context === null || context === void 0 ? void 0 : context.suppressTextValueWarning)) console.warn("<Item> with non-plain text contents is unsupported by type to select for accessibility. Please add a `textValue` prop.");
34
+ yield {
35
+ type: "item",
36
+ props: props,
37
+ rendered: rendered,
38
+ textValue: textValue,
39
+ "aria-label": props["aria-label"],
40
+ hasChildNodes: $c1d7fb2ec91bae71$var$hasChildItems(props),
41
+ *childNodes () {
42
+ if (childItems) for (let child of childItems)yield {
43
+ type: "item",
44
+ value: child
45
+ };
46
+ else if (title) {
47
+ let items = [];
48
+ (0, $tyW6A$react).Children.forEach(children, (child)=>{
49
+ items.push({
50
+ type: "item",
51
+ element: child
52
+ });
53
+ });
54
+ yield* items;
55
+ }
56
+ }
57
+ };
58
+ };
59
+ function $c1d7fb2ec91bae71$var$hasChildItems(props) {
60
+ if (props.hasChildItems != null) return props.hasChildItems;
61
+ if (props.childItems) return true;
62
+ if (props.title && (0, $tyW6A$react).Children.count(props.children) > 0) return true;
63
+ return false;
64
+ }
65
+ // We don't want getCollectionNode to show up in the type definition
66
+ let $c1d7fb2ec91bae71$export$6d08773d2e66f8f2 = $c1d7fb2ec91bae71$var$Item;
67
+
68
+
69
+ /*
70
+ * Copyright 2020 Adobe. All rights reserved.
71
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
72
+ * you may not use this file except in compliance with the License. You may obtain a copy
73
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
74
+ *
75
+ * Unless required by applicable law or agreed to in writing, software distributed under
76
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
77
+ * OF ANY KIND, either express or implied. See the License for the specific language
78
+ * governing permissions and limitations under the License.
79
+ */
80
+ function $9fc4852771d079eb$var$Section(props) {
81
+ return null;
82
+ }
83
+ $9fc4852771d079eb$var$Section.getCollectionNode = function* getCollectionNode(props) {
84
+ let { children: children , title: title , items: items } = props;
85
+ yield {
86
+ type: "section",
87
+ props: props,
88
+ hasChildNodes: true,
89
+ rendered: title,
90
+ "aria-label": props["aria-label"],
91
+ *childNodes () {
92
+ if (typeof children === "function") {
93
+ if (!items) throw new Error("props.children was a function but props.items is missing");
94
+ for (let item of items)yield {
95
+ type: "item",
96
+ value: item,
97
+ renderer: children
98
+ };
99
+ } else {
100
+ let items1 = [];
101
+ (0, $tyW6A$react).Children.forEach(children, (child)=>{
102
+ items1.push({
103
+ type: "item",
104
+ element: child
105
+ });
106
+ });
107
+ yield* items1;
108
+ }
109
+ }
110
+ };
111
+ };
112
+ // We don't want getCollectionNode to show up in the type definition
113
+ let $9fc4852771d079eb$export$6e2c8f0811a474ce = $9fc4852771d079eb$var$Section;
114
+
115
+
116
+ /*
117
+ * Copyright 2020 Adobe. All rights reserved.
118
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
119
+ * you may not use this file except in compliance with the License. You may obtain a copy
120
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
121
+ *
122
+ * Unless required by applicable law or agreed to in writing, software distributed under
123
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
124
+ * OF ANY KIND, either express or implied. See the License for the specific language
125
+ * governing permissions and limitations under the License.
126
+ */ /*
127
+ * Copyright 2020 Adobe. All rights reserved.
128
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
129
+ * you may not use this file except in compliance with the License. You may obtain a copy
130
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
131
+ *
132
+ * Unless required by applicable law or agreed to in writing, software distributed under
133
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
134
+ * OF ANY KIND, either express or implied. See the License for the specific language
135
+ * governing permissions and limitations under the License.
136
+ */
137
+
138
+ class $eb2240fc39a57fa5$export$bf788dd355e3a401 {
139
+ build(props, context) {
140
+ this.context = context;
141
+ return $eb2240fc39a57fa5$var$iterable(()=>this.iterateCollection(props));
142
+ }
143
+ *iterateCollection(props) {
144
+ let { children: children , items: items } = props;
145
+ if (typeof children === "function") {
146
+ if (!items) throw new Error("props.children was a function but props.items is missing");
147
+ for (let item of props.items)yield* this.getFullNode({
148
+ value: item
149
+ }, {
150
+ renderer: children
151
+ });
152
+ } else {
153
+ let items1 = [];
154
+ (0, $tyW6A$react).Children.forEach(children, (child)=>{
155
+ items1.push(child);
156
+ });
157
+ let index = 0;
158
+ for (let item1 of items1){
159
+ let nodes = this.getFullNode({
160
+ element: item1,
161
+ index: index
162
+ }, {});
163
+ for (let node of nodes){
164
+ index++;
165
+ yield node;
166
+ }
167
+ }
168
+ }
169
+ }
170
+ getKey(item, partialNode, state, parentKey) {
171
+ if (item.key != null) return item.key;
172
+ if (partialNode.type === "cell" && partialNode.key != null) return `${parentKey}${partialNode.key}`;
173
+ let v = partialNode.value;
174
+ if (v != null) {
175
+ var _v_key;
176
+ let key = (_v_key = v.key) !== null && _v_key !== void 0 ? _v_key : v.id;
177
+ if (key == null) throw new Error("No key found for item");
178
+ return key;
179
+ }
180
+ return parentKey ? `${parentKey}.${partialNode.index}` : `$.${partialNode.index}`;
181
+ }
182
+ getChildState(state, partialNode) {
183
+ return {
184
+ renderer: partialNode.renderer || state.renderer
185
+ };
186
+ }
187
+ *getFullNode(partialNode, state, parentKey, parentNode) {
188
+ // If there's a value instead of an element on the node, and a parent renderer function is available,
189
+ // use it to render an element for the value.
190
+ let element = partialNode.element;
191
+ if (!element && partialNode.value && state && state.renderer) {
192
+ let cached = this.cache.get(partialNode.value);
193
+ if (cached && (!cached.shouldInvalidate || !cached.shouldInvalidate(this.context))) {
194
+ cached.index = partialNode.index;
195
+ cached.parentKey = parentNode ? parentNode.key : null;
196
+ yield cached;
197
+ return;
198
+ }
199
+ element = state.renderer(partialNode.value);
200
+ }
201
+ // If there's an element with a getCollectionNode function on its type, then it's a supported component.
202
+ // Call this function to get a partial node, and recursively build a full node from there.
203
+ if ((0, $tyW6A$react).isValidElement(element)) {
204
+ let type = element.type;
205
+ if (typeof type !== "function" && typeof type.getCollectionNode !== "function") {
206
+ let name = typeof element.type === "function" ? element.type.name : element.type;
207
+ throw new Error(`Unknown element <${name}> in collection.`);
208
+ }
209
+ let childNodes = type.getCollectionNode(element.props, this.context);
210
+ let index = partialNode.index;
211
+ let result = childNodes.next();
212
+ while(!result.done && result.value){
213
+ let childNode = result.value;
214
+ partialNode.index = index;
215
+ let nodeKey = childNode.key;
216
+ if (!nodeKey) nodeKey = childNode.element ? null : this.getKey(element, partialNode, state, parentKey);
217
+ let nodes = this.getFullNode({
218
+ ...childNode,
219
+ key: nodeKey,
220
+ index: index,
221
+ wrapper: $eb2240fc39a57fa5$var$compose(partialNode.wrapper, childNode.wrapper)
222
+ }, this.getChildState(state, childNode), parentKey ? `${parentKey}${element.key}` : element.key, parentNode);
223
+ let children = [
224
+ ...nodes
225
+ ];
226
+ for (let node of children){
227
+ // Cache the node based on its value
228
+ node.value = childNode.value || partialNode.value;
229
+ if (node.value) this.cache.set(node.value, node);
230
+ // The partial node may have specified a type for the child in order to specify a constraint.
231
+ // Verify that the full node that was built recursively matches this type.
232
+ if (partialNode.type && node.type !== partialNode.type) throw new Error(`Unsupported type <${$eb2240fc39a57fa5$var$capitalize(node.type)}> in <${$eb2240fc39a57fa5$var$capitalize(parentNode.type)}>. Only <${$eb2240fc39a57fa5$var$capitalize(partialNode.type)}> is supported.`);
233
+ index++;
234
+ yield node;
235
+ }
236
+ result = childNodes.next(children);
237
+ }
238
+ return;
239
+ }
240
+ // Ignore invalid elements
241
+ if (partialNode.key == null) return;
242
+ // Create full node
243
+ let builder = this;
244
+ let node1 = {
245
+ type: partialNode.type,
246
+ props: partialNode.props,
247
+ key: partialNode.key,
248
+ parentKey: parentNode ? parentNode.key : null,
249
+ value: partialNode.value,
250
+ level: parentNode ? parentNode.level + 1 : 0,
251
+ index: partialNode.index,
252
+ rendered: partialNode.rendered,
253
+ textValue: partialNode.textValue,
254
+ "aria-label": partialNode["aria-label"],
255
+ wrapper: partialNode.wrapper,
256
+ shouldInvalidate: partialNode.shouldInvalidate,
257
+ hasChildNodes: partialNode.hasChildNodes,
258
+ childNodes: $eb2240fc39a57fa5$var$iterable(function*() {
259
+ if (!partialNode.hasChildNodes) return;
260
+ let index = 0;
261
+ for (let child of partialNode.childNodes()){
262
+ // Ensure child keys are globally unique by prepending the parent node's key
263
+ if (child.key != null) // TODO: Remove this line entirely and enforce that users always provide unique keys.
264
+ // Currently this line will have issues when a parent has a key `a` and a child with key `bc`
265
+ // but another parent has key `ab` and its child has a key `c`. The combined keys would result in both
266
+ // children having a key of `abc`.
267
+ child.key = `${node1.key}${child.key}`;
268
+ child.index = index;
269
+ let nodes = builder.getFullNode(child, builder.getChildState(state, child), node1.key, node1);
270
+ for (let node of nodes){
271
+ index++;
272
+ yield node;
273
+ }
274
+ }
275
+ })
276
+ };
277
+ yield node1;
278
+ }
279
+ constructor(){
280
+ (0, $tyW6A$swchelperssrc_define_propertymjs)(this, "cache", new WeakMap());
281
+ }
282
+ }
283
+ // Wraps an iterator function as an iterable object, and caches the results.
284
+ function $eb2240fc39a57fa5$var$iterable(iterator) {
285
+ let cache = [];
286
+ let iterable = null;
287
+ return {
288
+ *[Symbol.iterator] () {
289
+ for (let item of cache)yield item;
290
+ if (!iterable) iterable = iterator();
291
+ for (let item1 of iterable){
292
+ cache.push(item1);
293
+ yield item1;
294
+ }
295
+ }
296
+ };
297
+ }
298
+ function $eb2240fc39a57fa5$var$compose(outer, inner) {
299
+ if (outer && inner) return (element)=>outer(inner(element));
300
+ if (outer) return outer;
301
+ if (inner) return inner;
302
+ }
303
+ function $eb2240fc39a57fa5$var$capitalize(str) {
304
+ return str[0].toUpperCase() + str.slice(1);
305
+ }
306
+
307
+
308
+
309
+ function $7613b1592d41b092$export$6cd28814d92fa9c9(props, factory, context, invalidators = []) {
310
+ let builder = (0, $tyW6A$useMemo)(()=>new (0, $eb2240fc39a57fa5$export$bf788dd355e3a401)(), []);
311
+ let prev = (0, $tyW6A$useRef)(null);
312
+ return (0, $tyW6A$useMemo)(()=>{
313
+ let nodes = builder.build(props, context);
314
+ prev.current = factory(nodes, prev.current);
315
+ return prev.current;
316
+ // Don't invalidate when any prop changes, just the two we care about.
317
+ // eslint-disable-next-line react-hooks/exhaustive-deps
318
+ }, [
319
+ builder,
320
+ props.children,
321
+ props.items,
322
+ context,
323
+ ...invalidators
324
+ ]);
325
+ }
326
+
327
+
328
+ /*
329
+ * Copyright 2020 Adobe. All rights reserved.
330
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
331
+ * you may not use this file except in compliance with the License. You may obtain a copy
332
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
333
+ *
334
+ * Unless required by applicable law or agreed to in writing, software distributed under
335
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
336
+ * OF ANY KIND, either express or implied. See the License for the specific language
337
+ * governing permissions and limitations under the License.
338
+ */ const $453cc9f0df89c0a5$var$cache = new WeakMap();
339
+ function $453cc9f0df89c0a5$export$77d5aafae4e095b2(collection) {
340
+ let count = $453cc9f0df89c0a5$var$cache.get(collection);
341
+ if (count != null) return count;
342
+ count = 0;
343
+ for (let item of collection)if (item.type === "section") count += $453cc9f0df89c0a5$export$77d5aafae4e095b2(item.childNodes);
344
+ else count++;
345
+ $453cc9f0df89c0a5$var$cache.set(collection, count);
346
+ return count;
347
+ }
348
+
349
+
350
+
351
+
352
+ export {$c1d7fb2ec91bae71$export$6d08773d2e66f8f2 as Item, $9fc4852771d079eb$export$6e2c8f0811a474ce as Section, $7613b1592d41b092$export$6cd28814d92fa9c9 as useCollection, $453cc9f0df89c0a5$export$77d5aafae4e095b2 as getItemCount};
353
+ //# sourceMappingURL=module.js.map
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@react-stately/collections",
3
- "version": "3.5.1",
3
+ "version": "3.6.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
8
13
  "types": "dist/types.d.ts",
9
14
  "source": "src/index.ts",
10
15
  "files": [
@@ -17,7 +22,7 @@
17
22
  "url": "https://github.com/adobe/react-spectrum"
18
23
  },
19
24
  "dependencies": {
20
- "@react-types/shared": "^3.16.0",
25
+ "@react-types/shared": "^3.17.0",
21
26
  "@swc/helpers": "^0.4.14"
22
27
  },
23
28
  "peerDependencies": {
@@ -26,5 +31,5 @@
26
31
  "publishConfig": {
27
32
  "access": "public"
28
33
  },
29
- "gitHead": "5480d76bd815e239366f92852c76b6831ad2a4fd"
34
+ "gitHead": "a0efee84aa178cb1a202951dfd6d8de02b292307"
30
35
  }