@react-stately/collections 3.12.9 → 3.13.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.
- package/dist/import.mjs +7 -7
- package/dist/main.js +16 -16
- package/dist/main.js.map +1 -1
- package/dist/module.js +7 -7
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +7 -0
- package/package.json +17 -13
- package/src/index.ts +8 -7
- package/dist/CollectionBuilder.main.js +0 -223
- package/dist/CollectionBuilder.main.js.map +0 -1
- package/dist/CollectionBuilder.mjs +0 -214
- package/dist/CollectionBuilder.module.js +0 -214
- package/dist/CollectionBuilder.module.js.map +0 -1
- package/dist/Item.main.js +0 -68
- package/dist/Item.main.js.map +0 -1
- package/dist/Item.mjs +0 -59
- package/dist/Item.module.js +0 -59
- package/dist/Item.module.js.map +0 -1
- package/dist/Section.main.js +0 -60
- package/dist/Section.main.js.map +0 -1
- package/dist/Section.mjs +0 -51
- package/dist/Section.module.js +0 -51
- package/dist/Section.module.js.map +0 -1
- package/dist/getChildNodes.main.js +0 -81
- package/dist/getChildNodes.main.js.map +0 -1
- package/dist/getChildNodes.mjs +0 -72
- package/dist/getChildNodes.module.js +0 -72
- package/dist/getChildNodes.module.js.map +0 -1
- package/dist/getItemCount.main.js +0 -38
- package/dist/getItemCount.main.js.map +0 -1
- package/dist/getItemCount.mjs +0 -33
- package/dist/getItemCount.module.js +0 -33
- package/dist/getItemCount.module.js.map +0 -1
- package/dist/types.d.ts +0 -36
- package/dist/types.d.ts.map +0 -1
- package/dist/useCollection.main.js +0 -44
- package/dist/useCollection.main.js.map +0 -1
- package/dist/useCollection.mjs +0 -39
- package/dist/useCollection.module.js +0 -39
- package/dist/useCollection.module.js.map +0 -1
- package/src/CollectionBuilder.ts +0 -283
- package/src/Item.ts +0 -80
- package/src/Section.ts +0 -59
- package/src/getChildNodes.ts +0 -95
- package/src/getItemCount.ts +0 -39
- package/src/types.ts +0 -31
- package/src/useCollection.ts +0 -34
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
var $51588fd411aace25$exports = require("./CollectionBuilder.main.js");
|
|
2
|
-
var $bSehf$react = require("react");
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function $parcel$export(e, n, v, s) {
|
|
6
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
$parcel$export(module.exports, "useCollection", () => $98fc0fafaca75b6a$export$6cd28814d92fa9c9);
|
|
10
|
-
/*
|
|
11
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
12
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
13
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
14
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
-
*
|
|
16
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
17
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
18
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
19
|
-
* governing permissions and limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
function $98fc0fafaca75b6a$export$6cd28814d92fa9c9(props, factory, context) {
|
|
23
|
-
let builder = (0, $bSehf$react.useMemo)(()=>new (0, $51588fd411aace25$exports.CollectionBuilder)(), []);
|
|
24
|
-
let { children: children, items: items, collection: collection } = props;
|
|
25
|
-
let result = (0, $bSehf$react.useMemo)(()=>{
|
|
26
|
-
if (collection) return collection;
|
|
27
|
-
let nodes = builder.build({
|
|
28
|
-
children: children,
|
|
29
|
-
items: items
|
|
30
|
-
}, context);
|
|
31
|
-
return factory(nodes);
|
|
32
|
-
}, [
|
|
33
|
-
builder,
|
|
34
|
-
children,
|
|
35
|
-
items,
|
|
36
|
-
collection,
|
|
37
|
-
context,
|
|
38
|
-
factory
|
|
39
|
-
]);
|
|
40
|
-
return result;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//# sourceMappingURL=useCollection.main.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAYM,SAAS,0CAAqF,KAA8B,EAAE,OAAgC,EAAE,OAAiB;IACtL,IAAI,UAAU,CAAA,GAAA,oBAAM,EAAE,IAAM,IAAI,CAAA,GAAA,2CAAgB,KAAQ,EAAE;IAC1D,IAAI,YAAC,QAAQ,SAAE,KAAK,cAAE,UAAU,EAAC,GAAG;IACpC,IAAI,SAAS,CAAA,GAAA,oBAAM,EAAE;QACnB,IAAI,YACF,OAAO;QAET,IAAI,QAAQ,QAAQ,KAAK,CAAC;sBAAC;mBAAU;QAAK,GAAG;QAC7C,OAAO,QAAQ;IACjB,GAAG;QAAC;QAAS;QAAU;QAAO;QAAY;QAAS;KAAQ;IAC3D,OAAO;AACT","sources":["packages/@react-stately/collections/src/useCollection.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection, CollectionStateBase, Node} from '@react-types/shared';\nimport {CollectionBuilder} from './CollectionBuilder';\nimport {ReactElement, useMemo} from 'react';\n\ninterface CollectionOptions<T, C extends Collection<Node<T>>> extends Omit<CollectionStateBase<T, C>, 'children'> {\n children?: ReactElement<any> | null | (ReactElement<any> | null)[] | ((item: T) => ReactElement<any> | null)\n}\n\ntype CollectionFactory<T, C extends Collection<Node<T>>> = (node: Iterable<Node<T>>) => C;\n\nexport function useCollection<T extends object, C extends Collection<Node<T>> = Collection<Node<T>>>(props: CollectionOptions<T, C>, factory: CollectionFactory<T, C>, context?: unknown): C {\n let builder = useMemo(() => new CollectionBuilder<T>(), []);\n let {children, items, collection} = props;\n let result = useMemo(() => {\n if (collection) {\n return collection;\n }\n let nodes = builder.build({children, items}, context);\n return factory(nodes);\n }, [builder, children, items, collection, context, factory]);\n return result;\n}\n"],"names":[],"version":3,"file":"useCollection.main.js.map"}
|
package/dist/useCollection.mjs
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {CollectionBuilder as $eb2240fc39a57fa5$export$bf788dd355e3a401} from "./CollectionBuilder.mjs";
|
|
2
|
-
import {useMemo as $lbNth$useMemo} from "react";
|
|
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
|
-
|
|
16
|
-
function $7613b1592d41b092$export$6cd28814d92fa9c9(props, factory, context) {
|
|
17
|
-
let builder = (0, $lbNth$useMemo)(()=>new (0, $eb2240fc39a57fa5$export$bf788dd355e3a401)(), []);
|
|
18
|
-
let { children: children, items: items, collection: collection } = props;
|
|
19
|
-
let result = (0, $lbNth$useMemo)(()=>{
|
|
20
|
-
if (collection) return collection;
|
|
21
|
-
let nodes = builder.build({
|
|
22
|
-
children: children,
|
|
23
|
-
items: items
|
|
24
|
-
}, context);
|
|
25
|
-
return factory(nodes);
|
|
26
|
-
}, [
|
|
27
|
-
builder,
|
|
28
|
-
children,
|
|
29
|
-
items,
|
|
30
|
-
collection,
|
|
31
|
-
context,
|
|
32
|
-
factory
|
|
33
|
-
]);
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
export {$7613b1592d41b092$export$6cd28814d92fa9c9 as useCollection};
|
|
39
|
-
//# sourceMappingURL=useCollection.module.js.map
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {CollectionBuilder as $eb2240fc39a57fa5$export$bf788dd355e3a401} from "./CollectionBuilder.module.js";
|
|
2
|
-
import {useMemo as $lbNth$useMemo} from "react";
|
|
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
|
-
|
|
16
|
-
function $7613b1592d41b092$export$6cd28814d92fa9c9(props, factory, context) {
|
|
17
|
-
let builder = (0, $lbNth$useMemo)(()=>new (0, $eb2240fc39a57fa5$export$bf788dd355e3a401)(), []);
|
|
18
|
-
let { children: children, items: items, collection: collection } = props;
|
|
19
|
-
let result = (0, $lbNth$useMemo)(()=>{
|
|
20
|
-
if (collection) return collection;
|
|
21
|
-
let nodes = builder.build({
|
|
22
|
-
children: children,
|
|
23
|
-
items: items
|
|
24
|
-
}, context);
|
|
25
|
-
return factory(nodes);
|
|
26
|
-
}, [
|
|
27
|
-
builder,
|
|
28
|
-
children,
|
|
29
|
-
items,
|
|
30
|
-
collection,
|
|
31
|
-
context,
|
|
32
|
-
factory
|
|
33
|
-
]);
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
export {$7613b1592d41b092$export$6cd28814d92fa9c9 as useCollection};
|
|
39
|
-
//# sourceMappingURL=useCollection.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAYM,SAAS,0CAAqF,KAA8B,EAAE,OAAgC,EAAE,OAAiB;IACtL,IAAI,UAAU,CAAA,GAAA,cAAM,EAAE,IAAM,IAAI,CAAA,GAAA,yCAAgB,KAAQ,EAAE;IAC1D,IAAI,YAAC,QAAQ,SAAE,KAAK,cAAE,UAAU,EAAC,GAAG;IACpC,IAAI,SAAS,CAAA,GAAA,cAAM,EAAE;QACnB,IAAI,YACF,OAAO;QAET,IAAI,QAAQ,QAAQ,KAAK,CAAC;sBAAC;mBAAU;QAAK,GAAG;QAC7C,OAAO,QAAQ;IACjB,GAAG;QAAC;QAAS;QAAU;QAAO;QAAY;QAAS;KAAQ;IAC3D,OAAO;AACT","sources":["packages/@react-stately/collections/src/useCollection.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection, CollectionStateBase, Node} from '@react-types/shared';\nimport {CollectionBuilder} from './CollectionBuilder';\nimport {ReactElement, useMemo} from 'react';\n\ninterface CollectionOptions<T, C extends Collection<Node<T>>> extends Omit<CollectionStateBase<T, C>, 'children'> {\n children?: ReactElement<any> | null | (ReactElement<any> | null)[] | ((item: T) => ReactElement<any> | null)\n}\n\ntype CollectionFactory<T, C extends Collection<Node<T>>> = (node: Iterable<Node<T>>) => C;\n\nexport function useCollection<T extends object, C extends Collection<Node<T>> = Collection<Node<T>>>(props: CollectionOptions<T, C>, factory: CollectionFactory<T, C>, context?: unknown): C {\n let builder = useMemo(() => new CollectionBuilder<T>(), []);\n let {children, items, collection} = props;\n let result = useMemo(() => {\n if (collection) {\n return collection;\n }\n let nodes = builder.build({children, items}, context);\n return factory(nodes);\n }, [builder, children, items, collection, context, factory]);\n return result;\n}\n"],"names":[],"version":3,"file":"useCollection.module.js.map"}
|
package/src/CollectionBuilder.ts
DELETED
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {CollectionBase, CollectionElement, Key, Node} from '@react-types/shared';
|
|
14
|
-
import {PartialNode} from './types';
|
|
15
|
-
import React, {ReactElement} from 'react';
|
|
16
|
-
|
|
17
|
-
interface CollectionBuilderState {
|
|
18
|
-
renderer?: (value: any) => ReactElement | null
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface CollectReactElement<T> extends ReactElement {
|
|
22
|
-
getCollectionNode(props: any, context: any): Generator<PartialNode<T>, void, Node<T>[]>
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class CollectionBuilder<T extends object> {
|
|
26
|
-
private context?: unknown;
|
|
27
|
-
private cache: WeakMap<T, Node<T>> = new WeakMap();
|
|
28
|
-
|
|
29
|
-
build(props: Partial<CollectionBase<T>>, context?: unknown): Iterable<Node<T>> {
|
|
30
|
-
this.context = context;
|
|
31
|
-
return iterable(() => this.iterateCollection(props));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private *iterateCollection(props: Partial<CollectionBase<T>>): Generator<Node<T>> {
|
|
35
|
-
let {children, items} = props;
|
|
36
|
-
|
|
37
|
-
if (React.isValidElement<{children: CollectionElement<T>}>(children) && children.type === React.Fragment) {
|
|
38
|
-
yield* this.iterateCollection({
|
|
39
|
-
children: children.props.children,
|
|
40
|
-
items
|
|
41
|
-
});
|
|
42
|
-
} else if (typeof children === 'function') {
|
|
43
|
-
if (!items) {
|
|
44
|
-
throw new Error('props.children was a function but props.items is missing');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let index = 0;
|
|
48
|
-
for (let item of items) {
|
|
49
|
-
yield* this.getFullNode({
|
|
50
|
-
value: item,
|
|
51
|
-
index
|
|
52
|
-
}, {renderer: children});
|
|
53
|
-
index++;
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
let items: CollectionElement<T>[] = [];
|
|
57
|
-
React.Children.forEach(children, child => {
|
|
58
|
-
if (child) {
|
|
59
|
-
items.push(child);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
let index = 0;
|
|
64
|
-
for (let item of items) {
|
|
65
|
-
let nodes = this.getFullNode({
|
|
66
|
-
element: item,
|
|
67
|
-
index: index
|
|
68
|
-
}, {});
|
|
69
|
-
|
|
70
|
-
for (let node of nodes) {
|
|
71
|
-
index++;
|
|
72
|
-
yield node;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private getKey(item: NonNullable<CollectionElement<T>>, partialNode: PartialNode<T>, state: CollectionBuilderState, parentKey?: Key | null): Key {
|
|
79
|
-
if (item.key != null) {
|
|
80
|
-
return item.key;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (partialNode.type === 'cell' && partialNode.key != null) {
|
|
84
|
-
return `${parentKey}${partialNode.key}`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
let v = partialNode.value as any;
|
|
88
|
-
if (v != null) {
|
|
89
|
-
let key = v.key ?? v.id;
|
|
90
|
-
if (key == null) {
|
|
91
|
-
throw new Error('No key found for item');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return key;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return parentKey ? `${parentKey}.${partialNode.index}` : `$.${partialNode.index}`;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private getChildState(state: CollectionBuilderState, partialNode: PartialNode<T>) {
|
|
101
|
-
return {
|
|
102
|
-
renderer: partialNode.renderer || state.renderer
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private *getFullNode(partialNode: PartialNode<T> & {index: number}, state: CollectionBuilderState, parentKey?: Key | null, parentNode?: Node<T>): Generator<Node<T>> {
|
|
107
|
-
if (React.isValidElement<{children: CollectionElement<T>}>(partialNode.element) && partialNode.element.type === React.Fragment) {
|
|
108
|
-
let children: CollectionElement<T>[] = [];
|
|
109
|
-
|
|
110
|
-
React.Children.forEach(partialNode.element.props.children, child => {
|
|
111
|
-
children.push(child);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
let index = partialNode.index ?? 0;
|
|
115
|
-
|
|
116
|
-
for (const child of children) {
|
|
117
|
-
yield* this.getFullNode({
|
|
118
|
-
element: child,
|
|
119
|
-
index: index++
|
|
120
|
-
}, state, parentKey, parentNode);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// If there's a value instead of an element on the node, and a parent renderer function is available,
|
|
127
|
-
// use it to render an element for the value.
|
|
128
|
-
let element = partialNode.element;
|
|
129
|
-
if (!element && partialNode.value && state && state.renderer) {
|
|
130
|
-
let cached = this.cache.get(partialNode.value);
|
|
131
|
-
if (cached && (!cached.shouldInvalidate || !cached.shouldInvalidate(this.context))) {
|
|
132
|
-
cached.index = partialNode.index;
|
|
133
|
-
cached.parentKey = parentNode ? parentNode.key : null;
|
|
134
|
-
yield cached;
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
element = state.renderer(partialNode.value);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// If there's an element with a getCollectionNode function on its type, then it's a supported component.
|
|
142
|
-
// Call this function to get a partial node, and recursively build a full node from there.
|
|
143
|
-
if (React.isValidElement(element)) {
|
|
144
|
-
let type = element.type as unknown as CollectReactElement<T>;
|
|
145
|
-
if (typeof type !== 'function' && typeof type.getCollectionNode !== 'function') {
|
|
146
|
-
let name = element.type;
|
|
147
|
-
throw new Error(`Unknown element <${name}> in collection.`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
let childNodes = type.getCollectionNode(element.props, this.context) as Generator<PartialNode<T>, void, Node<T>[]>;
|
|
151
|
-
let index = partialNode.index ?? 0;
|
|
152
|
-
let result = childNodes.next();
|
|
153
|
-
while (!result.done && result.value) {
|
|
154
|
-
let childNode = result.value;
|
|
155
|
-
|
|
156
|
-
partialNode.index = index;
|
|
157
|
-
|
|
158
|
-
let nodeKey = childNode.key ?? null;
|
|
159
|
-
if (nodeKey == null) {
|
|
160
|
-
nodeKey = childNode.element ? null : this.getKey(element as NonNullable<CollectionElement<T>>, partialNode, state, parentKey);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let nodes = this.getFullNode({
|
|
164
|
-
...childNode,
|
|
165
|
-
key: nodeKey,
|
|
166
|
-
index,
|
|
167
|
-
wrapper: compose(partialNode.wrapper, childNode.wrapper)
|
|
168
|
-
}, this.getChildState(state, childNode), parentKey ? `${parentKey}${element.key}` : element.key, parentNode);
|
|
169
|
-
|
|
170
|
-
let children = [...nodes];
|
|
171
|
-
for (let node of children) {
|
|
172
|
-
// Cache the node based on its value
|
|
173
|
-
node.value = childNode.value ?? partialNode.value ?? null;
|
|
174
|
-
if (node.value) {
|
|
175
|
-
this.cache.set(node.value, node);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// The partial node may have specified a type for the child in order to specify a constraint.
|
|
179
|
-
// Verify that the full node that was built recursively matches this type.
|
|
180
|
-
if (partialNode.type && node.type !== partialNode.type) {
|
|
181
|
-
throw new Error(`Unsupported type <${capitalize(node.type)}> in <${capitalize(parentNode?.type ?? 'unknown parent type')}>. Only <${capitalize(partialNode.type)}> is supported.`);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
index++;
|
|
185
|
-
yield node;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
result = childNodes.next(children);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Ignore invalid elements
|
|
195
|
-
if (partialNode.key == null || partialNode.type == null) {
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Create full node
|
|
200
|
-
let builder = this;
|
|
201
|
-
let node: Node<T> = {
|
|
202
|
-
type: partialNode.type,
|
|
203
|
-
props: partialNode.props,
|
|
204
|
-
key: partialNode.key,
|
|
205
|
-
parentKey: parentNode ? parentNode.key : null,
|
|
206
|
-
value: partialNode.value ?? null,
|
|
207
|
-
level: parentNode ? parentNode.level + 1 : 0,
|
|
208
|
-
index: partialNode.index,
|
|
209
|
-
rendered: partialNode.rendered,
|
|
210
|
-
textValue: partialNode.textValue ?? '',
|
|
211
|
-
'aria-label': partialNode['aria-label'],
|
|
212
|
-
wrapper: partialNode.wrapper,
|
|
213
|
-
shouldInvalidate: partialNode.shouldInvalidate,
|
|
214
|
-
hasChildNodes: partialNode.hasChildNodes || false,
|
|
215
|
-
childNodes: iterable(function *() {
|
|
216
|
-
if (!partialNode.hasChildNodes || !partialNode.childNodes) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
let index = 0;
|
|
221
|
-
for (let child of partialNode.childNodes()) {
|
|
222
|
-
// Ensure child keys are globally unique by prepending the parent node's key
|
|
223
|
-
if (child.key != null) {
|
|
224
|
-
// TODO: Remove this line entirely and enforce that users always provide unique keys.
|
|
225
|
-
// Currently this line will have issues when a parent has a key `a` and a child with key `bc`
|
|
226
|
-
// but another parent has key `ab` and its child has a key `c`. The combined keys would result in both
|
|
227
|
-
// children having a key of `abc`.
|
|
228
|
-
child.key = `${node.key}${child.key}`;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
let nodes = builder.getFullNode({...child, index}, builder.getChildState(state, child), node.key, node);
|
|
232
|
-
for (let node of nodes) {
|
|
233
|
-
index++;
|
|
234
|
-
yield node;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
})
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
yield node;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Wraps an iterator function as an iterable object, and caches the results.
|
|
245
|
-
function iterable<T>(iterator: () => IterableIterator<Node<T>>): Iterable<Node<T>> {
|
|
246
|
-
let cache: Array<Node<T>> = [];
|
|
247
|
-
let iterable: null | IterableIterator<Node<T>> = null;
|
|
248
|
-
return {
|
|
249
|
-
*[Symbol.iterator]() {
|
|
250
|
-
for (let item of cache) {
|
|
251
|
-
yield item;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (!iterable) {
|
|
255
|
-
iterable = iterator();
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
for (let item of iterable) {
|
|
259
|
-
cache.push(item);
|
|
260
|
-
yield item;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
type Wrapper = (element: ReactElement) => ReactElement;
|
|
267
|
-
function compose(outer: Wrapper | void, inner: Wrapper | void): Wrapper | undefined {
|
|
268
|
-
if (outer && inner) {
|
|
269
|
-
return (element) => outer(inner(element));
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (outer) {
|
|
273
|
-
return outer;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (inner) {
|
|
277
|
-
return inner;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function capitalize(str: string) {
|
|
282
|
-
return str[0].toUpperCase() + str.slice(1);
|
|
283
|
-
}
|
package/src/Item.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {ItemElement, ItemProps} from '@react-types/shared';
|
|
14
|
-
import {PartialNode} from './types';
|
|
15
|
-
import React, {JSX, ReactElement} from 'react';
|
|
16
|
-
|
|
17
|
-
function Item<T>(props: ItemProps<T>): ReactElement | null { // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
Item.getCollectionNode = function* getCollectionNode<T>(props: ItemProps<T>, context: any): Generator<PartialNode<T>> {
|
|
22
|
-
let {childItems, title, children} = props;
|
|
23
|
-
|
|
24
|
-
let rendered = props.title || props.children;
|
|
25
|
-
let textValue = props.textValue || (typeof rendered === 'string' ? rendered : '') || props['aria-label'] || '';
|
|
26
|
-
|
|
27
|
-
// suppressTextValueWarning is used in components like Tabs, which don't have type to select support.
|
|
28
|
-
if (!textValue && !context?.suppressTextValueWarning && process.env.NODE_ENV !== 'production') {
|
|
29
|
-
console.warn('<Item> with non-plain text contents is unsupported by type to select for accessibility. Please add a `textValue` prop.');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
yield {
|
|
33
|
-
type: 'item',
|
|
34
|
-
props: props,
|
|
35
|
-
rendered,
|
|
36
|
-
textValue,
|
|
37
|
-
'aria-label': props['aria-label'],
|
|
38
|
-
hasChildNodes: hasChildItems(props),
|
|
39
|
-
*childNodes() {
|
|
40
|
-
if (childItems) {
|
|
41
|
-
for (let child of childItems) {
|
|
42
|
-
yield {
|
|
43
|
-
type: 'item',
|
|
44
|
-
value: child
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
} else if (title) {
|
|
48
|
-
let items: PartialNode<T>[] = [];
|
|
49
|
-
React.Children.forEach(children, child => {
|
|
50
|
-
items.push({
|
|
51
|
-
type: 'item',
|
|
52
|
-
element: child as ItemElement<T>
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
yield* items;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
function hasChildItems<T>(props: ItemProps<T>) {
|
|
63
|
-
if (props.hasChildItems != null) {
|
|
64
|
-
return props.hasChildItems;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (props.childItems) {
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (props.title && React.Children.count(props.children) > 0) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// We don't want getCollectionNode to show up in the type definition
|
|
79
|
-
let _Item = Item as <T>(props: ItemProps<T>) => JSX.Element;
|
|
80
|
-
export {_Item as Item};
|
package/src/Section.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {PartialNode} from './types';
|
|
14
|
-
import React, {JSX, ReactElement} from 'react';
|
|
15
|
-
import {SectionProps} from '@react-types/shared';
|
|
16
|
-
|
|
17
|
-
function Section<T>(props: SectionProps<T>): ReactElement | null { // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
Section.getCollectionNode = function* getCollectionNode<T>(props: SectionProps<T>): Generator<PartialNode<T>> {
|
|
22
|
-
let {children, title, items} = props;
|
|
23
|
-
yield {
|
|
24
|
-
type: 'section',
|
|
25
|
-
props: props,
|
|
26
|
-
hasChildNodes: true,
|
|
27
|
-
rendered: title,
|
|
28
|
-
'aria-label': props['aria-label'],
|
|
29
|
-
*childNodes() {
|
|
30
|
-
if (typeof children === 'function') {
|
|
31
|
-
if (!items) {
|
|
32
|
-
throw new Error('props.children was a function but props.items is missing');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
for (let item of items) {
|
|
36
|
-
yield {
|
|
37
|
-
type: 'item',
|
|
38
|
-
value: item,
|
|
39
|
-
renderer: children
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
let items: PartialNode<T>[] = [];
|
|
44
|
-
React.Children.forEach(children, child => {
|
|
45
|
-
items.push({
|
|
46
|
-
type: 'item',
|
|
47
|
-
element: child
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
yield* items;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// We don't want getCollectionNode to show up in the type definition
|
|
58
|
-
let _Section = Section as <T>(props: SectionProps<T>) => JSX.Element;
|
|
59
|
-
export {_Section as Section};
|
package/src/getChildNodes.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 type {Collection, Node} from '@react-types/shared';
|
|
14
|
-
|
|
15
|
-
export function getChildNodes<T>(node: Node<T>, collection: Collection<Node<T>>): Iterable<Node<T>> {
|
|
16
|
-
// New API: call collection.getChildren with the node key.
|
|
17
|
-
if (typeof collection.getChildren === 'function') {
|
|
18
|
-
return collection.getChildren(node.key);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Old API: access childNodes directly.
|
|
22
|
-
return node.childNodes;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function getFirstItem<T>(iterable: Iterable<T>): T | undefined {
|
|
26
|
-
return getNthItem(iterable, 0);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function getNthItem<T>(iterable: Iterable<T>, index: number): T | undefined {
|
|
30
|
-
if (index < 0) {
|
|
31
|
-
return undefined;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let i = 0;
|
|
35
|
-
for (let item of iterable) {
|
|
36
|
-
if (i === index) {
|
|
37
|
-
return item;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
i++;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function getLastItem<T>(iterable: Iterable<T>): T | undefined {
|
|
45
|
-
let lastItem: T | undefined = undefined;
|
|
46
|
-
for (let value of iterable) {
|
|
47
|
-
lastItem = value;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return lastItem;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function compareNodeOrder<T>(collection: Collection<Node<T>>, a: Node<T>, b: Node<T>): number {
|
|
54
|
-
// If the two nodes have the same parent, compare their indices.
|
|
55
|
-
if (a.parentKey === b.parentKey) {
|
|
56
|
-
return a.index - b.index;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Otherwise, collect all of the ancestors from each node, and find the first one that doesn't match starting from the root.
|
|
60
|
-
// Include the base nodes in case we are comparing nodes of different levels so that we can compare the higher node to the lower level node's
|
|
61
|
-
// ancestor of the same level
|
|
62
|
-
let aAncestors = [...getAncestors(collection, a), a];
|
|
63
|
-
let bAncestors = [...getAncestors(collection, b), b];
|
|
64
|
-
let firstNonMatchingAncestor = aAncestors.slice(0, bAncestors.length).findIndex((a, i) => a !== bAncestors[i]);
|
|
65
|
-
if (firstNonMatchingAncestor !== -1) {
|
|
66
|
-
// Compare the indices of two children within the common ancestor.
|
|
67
|
-
a = aAncestors[firstNonMatchingAncestor];
|
|
68
|
-
b = bAncestors[firstNonMatchingAncestor];
|
|
69
|
-
return a.index - b.index;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// If there isn't a non matching ancestor, we might be in a case where one of the nodes is the ancestor of the other.
|
|
73
|
-
if (aAncestors.findIndex(node => node === b) >= 0) {
|
|
74
|
-
return 1;
|
|
75
|
-
} else if (bAncestors.findIndex(node => node === a) >= 0) {
|
|
76
|
-
return -1;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 🤷
|
|
80
|
-
return -1;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function getAncestors<T>(collection: Collection<Node<T>>, node: Node<T>): Node<T>[] {
|
|
84
|
-
let parents: Node<T>[] = [];
|
|
85
|
-
|
|
86
|
-
let currNode: Node<T> | null = node;
|
|
87
|
-
while (currNode?.parentKey != null) {
|
|
88
|
-
currNode = collection.getItem(currNode.parentKey);
|
|
89
|
-
if (currNode) {
|
|
90
|
-
parents.unshift(currNode);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return parents;
|
|
95
|
-
}
|