@react-stately/data 3.0.0-nightly-641446f65-240905

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.
@@ -0,0 +1,217 @@
1
+ import {useState as $bc4N1$useState, useMemo as $bc4N1$useMemo} from "react";
2
+
3
+ /*
4
+ * Copyright 2020 Adobe. All rights reserved.
5
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License. You may obtain a copy
7
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
+ * OF ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ */
14
+ function $0d86e9c8f07f9a7b$export$762f73dccccd255d(options) {
15
+ let { initialItems: initialItems = [], initialSelectedKeys: initialSelectedKeys, getKey: getKey = (item)=>{
16
+ var _item_id;
17
+ return (_item_id = item.id) !== null && _item_id !== void 0 ? _item_id : item.key;
18
+ }, filter: filter, initialFilterText: initialFilterText = '' } = options;
19
+ // Store both items and filteredItems in state so we can go back to the unfiltered list
20
+ let [state, setState] = (0, $bc4N1$useState)({
21
+ items: initialItems,
22
+ selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys || []),
23
+ filterText: initialFilterText
24
+ });
25
+ let filteredItems = (0, $bc4N1$useMemo)(()=>filter ? state.items.filter((item)=>filter(item, state.filterText)) : state.items, [
26
+ state.items,
27
+ state.filterText,
28
+ filter
29
+ ]);
30
+ return {
31
+ ...state,
32
+ items: filteredItems,
33
+ ...$0d86e9c8f07f9a7b$export$79c0c687a5963b0a({
34
+ getKey: getKey
35
+ }, setState),
36
+ getItem (key) {
37
+ return state.items.find((item)=>getKey(item) === key);
38
+ }
39
+ };
40
+ }
41
+ function $0d86e9c8f07f9a7b$export$79c0c687a5963b0a(opts, dispatch) {
42
+ let { cursor: cursor, getKey: getKey } = opts;
43
+ return {
44
+ setSelectedKeys (selectedKeys) {
45
+ dispatch((state)=>({
46
+ ...state,
47
+ selectedKeys: selectedKeys
48
+ }));
49
+ },
50
+ setFilterText (filterText) {
51
+ dispatch((state)=>({
52
+ ...state,
53
+ filterText: filterText
54
+ }));
55
+ },
56
+ insert (index, ...values) {
57
+ dispatch((state)=>$0d86e9c8f07f9a7b$var$insert(state, index, ...values));
58
+ },
59
+ insertBefore (key, ...values) {
60
+ dispatch((state)=>{
61
+ let index = state.items.findIndex((item)=>getKey(item) === key);
62
+ if (index === -1) {
63
+ if (state.items.length === 0) index = 0;
64
+ else return state;
65
+ }
66
+ return $0d86e9c8f07f9a7b$var$insert(state, index, ...values);
67
+ });
68
+ },
69
+ insertAfter (key, ...values) {
70
+ dispatch((state)=>{
71
+ let index = state.items.findIndex((item)=>getKey(item) === key);
72
+ if (index === -1) {
73
+ if (state.items.length === 0) index = 0;
74
+ else return state;
75
+ }
76
+ return $0d86e9c8f07f9a7b$var$insert(state, index + 1, ...values);
77
+ });
78
+ },
79
+ prepend (...values) {
80
+ dispatch((state)=>$0d86e9c8f07f9a7b$var$insert(state, 0, ...values));
81
+ },
82
+ append (...values) {
83
+ dispatch((state)=>$0d86e9c8f07f9a7b$var$insert(state, state.items.length, ...values));
84
+ },
85
+ remove (...keys) {
86
+ dispatch((state)=>{
87
+ let keySet = new Set(keys);
88
+ let items = state.items.filter((item)=>!keySet.has(getKey(item)));
89
+ let selection = 'all';
90
+ if (state.selectedKeys !== 'all') {
91
+ selection = new Set(state.selectedKeys);
92
+ for (let key of keys)selection.delete(key);
93
+ }
94
+ if (cursor == null && items.length === 0) selection = new Set();
95
+ return {
96
+ ...state,
97
+ items: items,
98
+ selectedKeys: selection
99
+ };
100
+ });
101
+ },
102
+ removeSelectedItems () {
103
+ dispatch((state)=>{
104
+ if (state.selectedKeys === 'all') return {
105
+ ...state,
106
+ items: [],
107
+ selectedKeys: new Set()
108
+ };
109
+ let selectedKeys = state.selectedKeys;
110
+ let items = state.items.filter((item)=>!selectedKeys.has(getKey(item)));
111
+ return {
112
+ ...state,
113
+ items: items,
114
+ selectedKeys: new Set()
115
+ };
116
+ });
117
+ },
118
+ move (key, toIndex) {
119
+ dispatch((state)=>{
120
+ let index = state.items.findIndex((item)=>getKey(item) === key);
121
+ if (index === -1) return state;
122
+ let copy = state.items.slice();
123
+ let [item] = copy.splice(index, 1);
124
+ copy.splice(toIndex, 0, item);
125
+ return {
126
+ ...state,
127
+ items: copy
128
+ };
129
+ });
130
+ },
131
+ moveBefore (key, keys) {
132
+ dispatch((state)=>{
133
+ let toIndex = state.items.findIndex((item)=>getKey(item) === key);
134
+ if (toIndex === -1) return state;
135
+ // Find indices of keys to move. Sort them so that the order in the list is retained.
136
+ let keyArray = Array.isArray(keys) ? keys : [
137
+ ...keys
138
+ ];
139
+ let indices = keyArray.map((key)=>state.items.findIndex((item)=>getKey(item) === key)).sort((a, b)=>a - b);
140
+ return $0d86e9c8f07f9a7b$var$move(state, indices, toIndex);
141
+ });
142
+ },
143
+ moveAfter (key, keys) {
144
+ dispatch((state)=>{
145
+ let toIndex = state.items.findIndex((item)=>getKey(item) === key);
146
+ if (toIndex === -1) return state;
147
+ let keyArray = Array.isArray(keys) ? keys : [
148
+ ...keys
149
+ ];
150
+ let indices = keyArray.map((key)=>state.items.findIndex((item)=>getKey(item) === key)).sort((a, b)=>a - b);
151
+ return $0d86e9c8f07f9a7b$var$move(state, indices, toIndex + 1);
152
+ });
153
+ },
154
+ update (key, newValue) {
155
+ dispatch((state)=>{
156
+ let index = state.items.findIndex((item)=>getKey(item) === key);
157
+ if (index === -1) return state;
158
+ return {
159
+ ...state,
160
+ items: [
161
+ ...state.items.slice(0, index),
162
+ newValue,
163
+ ...state.items.slice(index + 1)
164
+ ]
165
+ };
166
+ });
167
+ }
168
+ };
169
+ }
170
+ function $0d86e9c8f07f9a7b$var$insert(state, index, ...values) {
171
+ return {
172
+ ...state,
173
+ items: [
174
+ ...state.items.slice(0, index),
175
+ ...values,
176
+ ...state.items.slice(index)
177
+ ]
178
+ };
179
+ }
180
+ function $0d86e9c8f07f9a7b$var$move(state, indices, toIndex) {
181
+ // Shift the target down by the number of items being moved from before the target
182
+ toIndex -= indices.filter((index)=>index < toIndex).length;
183
+ let moves = indices.map((from)=>({
184
+ from: from,
185
+ to: toIndex++
186
+ }));
187
+ // Shift later from indices down if they have a larger index
188
+ for(let i = 0; i < moves.length; i++){
189
+ let a = moves[i].from;
190
+ for(let j = i; j < moves.length; j++){
191
+ let b = moves[j].from;
192
+ if (b > a) moves[j].from--;
193
+ }
194
+ }
195
+ // Interleave the moves so they can be applied one by one rather than all at once
196
+ for(let i = 0; i < moves.length; i++){
197
+ let a = moves[i];
198
+ for(let j = moves.length - 1; j > i; j--){
199
+ let b = moves[j];
200
+ if (b.from < a.to) a.to++;
201
+ else b.from++;
202
+ }
203
+ }
204
+ let copy = state.items.slice();
205
+ for (let move of moves){
206
+ let [item] = copy.splice(move.from, 1);
207
+ copy.splice(move.to, 0, item);
208
+ }
209
+ return {
210
+ ...state,
211
+ items: copy
212
+ };
213
+ }
214
+
215
+
216
+ export {$0d86e9c8f07f9a7b$export$762f73dccccd255d as useListData, $0d86e9c8f07f9a7b$export$79c0c687a5963b0a as createListActions};
217
+ //# sourceMappingURL=useListData.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC;AAgIM,SAAS,0CAAe,OAAuB;IACpD,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;IAAD,WAC1C,MAAM,qBACN,oBAAoB,IACrB,GAAG;IAEJ,uFAAuF;IACvF,IAAI,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,eAAO,EAAgB;QAC7C,OAAO;QACP,cAAc,wBAAwB,QAAQ,QAAQ,IAAI,IA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,MAAM,CAAC,KAAK,IAAI,EAAE;QACpC,KAAK,MAAM,CAAC,KAAK,EAAE,EAAE,GAAG;IAC1B;IAEA,OAAO;QACL,GAAG,KAAK;QACR,OAAO;IACT;AACF","sources":["packages/@react-stately/data/src/useListData.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 {Key, Selection} from '@react-types/shared';\nimport {useMemo, useState} from 'react';\n\nexport interface ListOptions<T> {\n /** Initial items in the list. */\n initialItems?: T[],\n /** The keys for the initially selected items. */\n initialSelectedKeys?: 'all' | Iterable<Key>,\n /** The initial text to filter the list by. */\n initialFilterText?: string,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that returns whether a item matches the current filter text. */\n filter?: (item: T, filterText: string) => boolean\n}\n\nexport interface ListData<T> {\n /** The items in the list. */\n items: T[],\n\n /** The keys of the currently selected items in the list. */\n selectedKeys: Selection,\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Selection): void,\n\n /** The current filter text. */\n filterText: string,\n\n /** Sets the filter text. */\n setFilterText(filterText: string): void,\n\n /**\n * Gets an item from the list by key.\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): T,\n\n /**\n * Inserts items into the list at the given index.\n * @param index - The index to insert into.\n * @param values - The values to insert.\n */\n insert(index: number, ...values: T[]): void,\n\n /**\n * Inserts items into the list before the item at the given key.\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void,\n\n /**\n * Inserts items into the list after the item at the given key.\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void,\n\n /**\n * Appends items to the list.\n * @param values - The values to insert.\n */\n append(...values: T[]): void,\n\n /**\n * Prepends items to the list.\n * @param value - The value to insert.\n */\n prepend(...values: T[]): void,\n\n /**\n * Removes items from the list by their keys.\n * @param keys - The keys of the item to remove.\n */\n remove(...keys: Key[]): void,\n\n /**\n * Removes all items from the list that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void,\n\n /**\n * Moves an item within the list.\n * @param key - The key of the item to move.\n * @param toIndex - The index to move the item to.\n */\n move(key: Key, toIndex: number): void,\n\n /**\n * Moves one or more items before a given key.\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void,\n\n /**\n * Moves one or more items after a given key.\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void,\n\n /**\n * Updates an item in the list.\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void\n}\n\nexport interface ListState<T> {\n items: T[],\n selectedKeys: Selection,\n filterText: string\n}\n\ninterface CreateListOptions<T, C> extends ListOptions<T> {\n cursor?: C\n}\n\n/**\n * Manages state for an immutable list data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useListData<T>(options: ListOptions<T>): ListData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n filter,\n initialFilterText = ''\n } = options;\n\n // Store both items and filteredItems in state so we can go back to the unfiltered list\n let [state, setState] = useState<ListState<T>>({\n items: initialItems,\n selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys || []),\n filterText: initialFilterText\n });\n\n let filteredItems = useMemo(\n () => filter ? state.items.filter(item => filter(item, state.filterText)) : state.items,\n [state.items, state.filterText, filter]);\n\n return {\n ...state,\n items: filteredItems,\n ...createListActions({getKey}, setState),\n getItem(key: Key) {\n return state.items.find(item => getKey(item) === key);\n }\n };\n}\n\nexport function createListActions<T, C>(opts: CreateListOptions<T, C>, dispatch: (updater: (state: ListState<T>) => ListState<T>) => void): Omit<ListData<T>, 'items' | 'selectedKeys' | 'getItem' | 'filterText'> {\n let {cursor, getKey} = opts;\n return {\n setSelectedKeys(selectedKeys: Selection) {\n dispatch(state => ({\n ...state,\n selectedKeys\n }));\n },\n setFilterText(filterText: string) {\n dispatch(state => ({\n ...state,\n filterText\n }));\n },\n insert(index: number, ...values: T[]) {\n dispatch(state => insert(state, index, ...values));\n },\n insertBefore(key: Key, ...values: T[]) {\n dispatch(state => {\n let index = state.items.findIndex(item => getKey(item) === key);\n if (index === -1) {\n if (state.items.length === 0) {\n index = 0;\n } else {\n return state;\n }\n }\n\n return insert(state, index, ...values);\n });\n },\n insertAfter(key: Key, ...values: T[]) {\n dispatch(state => {\n let index = state.items.findIndex(item => getKey(item) === key);\n if (index === -1) {\n if (state.items.length === 0) {\n index = 0;\n } else {\n return state;\n }\n }\n\n return insert(state, index + 1, ...values);\n });\n },\n prepend(...values: T[]) {\n dispatch(state => insert(state, 0, ...values));\n },\n append(...values: T[]) {\n dispatch(state => insert(state, state.items.length, ...values));\n },\n remove(...keys: Key[]) {\n dispatch(state => {\n let keySet = new Set(keys);\n let items = state.items.filter(item => !keySet.has(getKey(item)));\n\n let selection: Selection = 'all';\n if (state.selectedKeys !== 'all') {\n selection = new Set(state.selectedKeys);\n for (let key of keys) {\n selection.delete(key);\n }\n }\n if (cursor == null && items.length === 0) {\n selection = new Set();\n }\n\n return {\n ...state,\n items,\n selectedKeys: selection\n };\n });\n },\n removeSelectedItems() {\n dispatch(state => {\n if (state.selectedKeys === 'all') {\n return {\n ...state,\n items: [],\n selectedKeys: new Set()\n };\n }\n\n let selectedKeys = state.selectedKeys;\n let items = state.items.filter(item => !selectedKeys.has(getKey(item)));\n return {\n ...state,\n items,\n selectedKeys: new Set()\n };\n });\n },\n move(key: Key, toIndex: number) {\n dispatch(state => {\n let index = state.items.findIndex(item => getKey(item) === key);\n if (index === -1) {\n return state;\n }\n\n let copy = state.items.slice();\n let [item] = copy.splice(index, 1);\n copy.splice(toIndex, 0, item);\n return {\n ...state,\n items: copy\n };\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n dispatch(state => {\n let toIndex = state.items.findIndex(item => getKey(item) === key);\n if (toIndex === -1) {\n return state;\n }\n\n // Find indices of keys to move. Sort them so that the order in the list is retained.\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n let indices = keyArray.map(key => state.items.findIndex(item => getKey(item) === key)).sort((a, b) => a - b);\n return move(state, indices, toIndex);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n dispatch(state => {\n let toIndex = state.items.findIndex(item => getKey(item) === key);\n if (toIndex === -1) {\n return state;\n }\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n let indices = keyArray.map(key => state.items.findIndex(item => getKey(item) === key)).sort((a, b) => a - b);\n return move(state, indices, toIndex + 1);\n });\n },\n update(key: Key, newValue: T) {\n dispatch(state => {\n let index = state.items.findIndex(item => getKey(item) === key);\n if (index === -1) {\n return state;\n }\n\n return {\n ...state,\n items: [\n ...state.items.slice(0, index),\n newValue,\n ...state.items.slice(index + 1)\n ]\n };\n });\n }\n };\n}\n\nfunction insert<T>(state: ListState<T>, index: number, ...values: T[]): ListState<T> {\n return {\n ...state,\n items: [\n ...state.items.slice(0, index),\n ...values,\n ...state.items.slice(index)\n ]\n };\n}\n\nfunction move<T>(state: ListState<T>, indices: number[], toIndex: number): ListState<T> {\n // Shift the target down by the number of items being moved from before the target\n toIndex -= indices.filter(index => index < toIndex).length;\n\n let moves = indices.map(from => ({\n from,\n to: toIndex++\n }));\n\n // Shift later from indices down if they have a larger index\n for (let i = 0; i < moves.length; i++) {\n let a = moves[i].from;\n for (let j = i; j < moves.length; j++) {\n let b = moves[j].from;\n\n if (b > a) {\n moves[j].from--;\n }\n }\n }\n\n // Interleave the moves so they can be applied one by one rather than all at once\n for (let i = 0; i < moves.length; i++) {\n let a = moves[i];\n for (let j = moves.length - 1; j > i; j--) {\n let b = moves[j];\n\n if (b.from < a.to) {\n a.to++;\n } else {\n b.from++;\n }\n }\n }\n\n let copy = state.items.slice();\n for (let move of moves) {\n let [item] = copy.splice(move.from, 1);\n copy.splice(move.to, 0, item);\n }\n\n return {\n ...state,\n items: copy\n };\n}\n"],"names":[],"version":3,"file":"useListData.module.js.map"}
@@ -0,0 +1,224 @@
1
+ var $aMaLn$react = require("react");
2
+
3
+
4
+ function $parcel$export(e, n, v, s) {
5
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
6
+ }
7
+
8
+ $parcel$export(module.exports, "useTreeData", () => $2d16d1aab63a81f4$export$d14e1352e21f4a16);
9
+ /*
10
+ * Copyright 2020 Adobe. All rights reserved.
11
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License. You may obtain a copy
13
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software distributed under
16
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
17
+ * OF ANY KIND, either express or implied. See the License for the specific language
18
+ * governing permissions and limitations under the License.
19
+ */
20
+ function $2d16d1aab63a81f4$export$d14e1352e21f4a16(options) {
21
+ let { initialItems: initialItems = [], initialSelectedKeys: initialSelectedKeys, getKey: getKey = (item)=>{
22
+ var _item_id;
23
+ return (_item_id = item.id) !== null && _item_id !== void 0 ? _item_id : item.key;
24
+ }, getChildren: getChildren = (item)=>item.children } = options;
25
+ // We only want to compute this on initial render.
26
+ let [tree, setItems] = (0, $aMaLn$react.useState)(()=>buildTree(initialItems, new Map()));
27
+ let { items: items, nodeMap: nodeMap } = tree;
28
+ let [selectedKeys, setSelectedKeys] = (0, $aMaLn$react.useState)(new Set(initialSelectedKeys || []));
29
+ function buildTree(initialItems = [], map, parentKey) {
30
+ if (initialItems == null) initialItems = [];
31
+ return {
32
+ items: initialItems.map((item)=>{
33
+ let node = {
34
+ key: getKey(item),
35
+ parentKey: parentKey,
36
+ value: item,
37
+ children: null
38
+ };
39
+ node.children = buildTree(getChildren(item), map, node.key).items;
40
+ map.set(node.key, node);
41
+ return node;
42
+ }),
43
+ nodeMap: map
44
+ };
45
+ }
46
+ function updateTree(items, key, update, originalMap) {
47
+ let node = originalMap.get(key);
48
+ if (!node) return {
49
+ items: items,
50
+ nodeMap: originalMap
51
+ };
52
+ let map = new Map(originalMap);
53
+ // Create a new node. If null, then delete the node, otherwise replace.
54
+ let newNode = update(node);
55
+ if (newNode == null) deleteNode(node, map);
56
+ else addNode(newNode, map);
57
+ // Walk up the tree and update each parent to refer to the new children.
58
+ while(node.parentKey){
59
+ let nextParent = map.get(node.parentKey);
60
+ let copy = {
61
+ key: nextParent.key,
62
+ parentKey: nextParent.parentKey,
63
+ value: nextParent.value,
64
+ children: null
65
+ };
66
+ let children = nextParent.children;
67
+ if (newNode == null) children = children.filter((c)=>c !== node);
68
+ copy.children = children.map((child)=>{
69
+ if (child === node) return newNode;
70
+ return child;
71
+ });
72
+ map.set(copy.key, copy);
73
+ newNode = copy;
74
+ node = nextParent;
75
+ }
76
+ if (newNode == null) items = items.filter((c)=>c !== node);
77
+ return {
78
+ items: items.map((item)=>{
79
+ if (item === node) return newNode;
80
+ return item;
81
+ }),
82
+ nodeMap: map
83
+ };
84
+ }
85
+ function addNode(node, map) {
86
+ map.set(node.key, node);
87
+ for (let child of node.children)addNode(child, map);
88
+ }
89
+ function deleteNode(node, map) {
90
+ map.delete(node.key);
91
+ for (let child of node.children)deleteNode(child, map);
92
+ }
93
+ return {
94
+ items: items,
95
+ selectedKeys: selectedKeys,
96
+ setSelectedKeys: setSelectedKeys,
97
+ getItem (key) {
98
+ return nodeMap.get(key);
99
+ },
100
+ insert (parentKey, index, ...values) {
101
+ setItems(({ items: items, nodeMap: originalMap })=>{
102
+ let { items: newNodes, nodeMap: newMap } = buildTree(values, originalMap, parentKey);
103
+ // If parentKey is null, insert into the root.
104
+ if (parentKey == null) return {
105
+ items: [
106
+ ...items.slice(0, index),
107
+ ...newNodes,
108
+ ...items.slice(index)
109
+ ],
110
+ nodeMap: newMap
111
+ };
112
+ // Otherwise, update the parent node and its ancestors.
113
+ return updateTree(items, parentKey, (parentNode)=>({
114
+ key: parentNode.key,
115
+ parentKey: parentNode.parentKey,
116
+ value: parentNode.value,
117
+ children: [
118
+ ...parentNode.children.slice(0, index),
119
+ ...newNodes,
120
+ ...parentNode.children.slice(index)
121
+ ]
122
+ }), newMap);
123
+ });
124
+ },
125
+ insertBefore (key, ...values) {
126
+ let node = nodeMap.get(key);
127
+ if (!node) return;
128
+ let parentNode = nodeMap.get(node.parentKey);
129
+ let nodes = parentNode ? parentNode.children : items;
130
+ let index = nodes.indexOf(node);
131
+ this.insert(parentNode === null || parentNode === void 0 ? void 0 : parentNode.key, index, ...values);
132
+ },
133
+ insertAfter (key, ...values) {
134
+ let node = nodeMap.get(key);
135
+ if (!node) return;
136
+ let parentNode = nodeMap.get(node.parentKey);
137
+ let nodes = parentNode ? parentNode.children : items;
138
+ let index = nodes.indexOf(node);
139
+ this.insert(parentNode === null || parentNode === void 0 ? void 0 : parentNode.key, index + 1, ...values);
140
+ },
141
+ prepend (parentKey, ...values) {
142
+ this.insert(parentKey, 0, ...values);
143
+ },
144
+ append (parentKey, ...values) {
145
+ if (parentKey == null) this.insert(null, items.length, ...values);
146
+ else {
147
+ let parentNode = nodeMap.get(parentKey);
148
+ if (!parentNode) return;
149
+ this.insert(parentKey, parentNode.children.length, ...values);
150
+ }
151
+ },
152
+ remove (...keys) {
153
+ if (keys.length === 0) return;
154
+ let newItems = items;
155
+ let prevMap = nodeMap;
156
+ let newTree;
157
+ for (let key of keys){
158
+ newTree = updateTree(newItems, key, ()=>null, prevMap);
159
+ prevMap = newTree.nodeMap;
160
+ newItems = newTree.items;
161
+ }
162
+ setItems(newTree);
163
+ let selection = new Set(selectedKeys);
164
+ for (let key of selectedKeys)if (!newTree.nodeMap.has(key)) selection.delete(key);
165
+ setSelectedKeys(selection);
166
+ },
167
+ removeSelectedItems () {
168
+ this.remove(...selectedKeys);
169
+ },
170
+ move (key, toParentKey, index) {
171
+ setItems(({ items: items, nodeMap: originalMap })=>{
172
+ let node = originalMap.get(key);
173
+ if (!node) return {
174
+ items: items,
175
+ nodeMap: originalMap
176
+ };
177
+ let { items: newItems, nodeMap: newMap } = updateTree(items, key, ()=>null, originalMap);
178
+ const movedNode = {
179
+ ...node,
180
+ parentKey: toParentKey
181
+ };
182
+ // If parentKey is null, insert into the root.
183
+ if (toParentKey == null) {
184
+ newMap.set(movedNode.key, movedNode);
185
+ return {
186
+ items: [
187
+ ...newItems.slice(0, index),
188
+ movedNode,
189
+ ...newItems.slice(index)
190
+ ],
191
+ nodeMap: newMap
192
+ };
193
+ }
194
+ // Otherwise, update the parent node and its ancestors.
195
+ return updateTree(newItems, toParentKey, (parentNode)=>({
196
+ key: parentNode.key,
197
+ parentKey: parentNode.parentKey,
198
+ value: parentNode.value,
199
+ children: [
200
+ ...parentNode.children.slice(0, index),
201
+ movedNode,
202
+ ...parentNode.children.slice(index)
203
+ ]
204
+ }), newMap);
205
+ });
206
+ },
207
+ update (oldKey, newValue) {
208
+ setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
209
+ let node = {
210
+ key: oldNode.key,
211
+ parentKey: oldNode.parentKey,
212
+ value: newValue,
213
+ children: null
214
+ };
215
+ let tree = buildTree(getChildren(newValue), originalMap, node.key);
216
+ node.children = tree.items;
217
+ return node;
218
+ }, originalMap));
219
+ }
220
+ };
221
+ }
222
+
223
+
224
+ //# sourceMappingURL=useTreeData.main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AA+GM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;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,QAAW;YAC7B,SAAS,CAAC,SAAC,KAAK,EAAE,SAAS,WAAW,EAAC,GAAK,WAAW,OAAO,QAAQ,CAAA;oBACpE,IAAI,OAAoB;wBACtB,KAAK,QAAQ,GAAG;wBAChB,WAAW,QAAQ,SAAS;wBAC5B,OAAO;wBACP,UAAU;oBACZ;oBAEA,IAAI,OAAO,UAAU,YAAY,WAAW,aAAa,KAAK,GAAG;oBACjE,KAAK,QAAQ,GAAG,KAAK,KAAK;oBAC1B,OAAO;gBACT,GAAG;QACL;IACF;AACF","sources":["packages/@react-stately/data/src/useTreeData.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 {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[],\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[]\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key,\n /** The key of the parent node. */\n parentKey: Key,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[]\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[],\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>,\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void,\n\n /**\n * Gets a node from the tree by key.\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T>,\n\n /**\n * Inserts an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void,\n\n /**\n * Inserts items into the list before the item at the given key.\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void,\n\n /**\n * Inserts items into the list after the item at the given key.\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void,\n\n /**\n * Appends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void,\n\n /**\n * Prepends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void,\n\n /**\n * Removes an item from the tree by its key.\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void,\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void,\n\n /**\n * Moves an item within the tree.\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void,\n\n /**\n * Updates an item in the tree.\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<{items: TreeNode<T>[], nodeMap: Map<Key, TreeNode<T>>}>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(initialItems: T[] | null = [], map: Map<Key, TreeNode<T>>, parentKey?: Key | null) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(items: TreeNode<T>[], key: Key, update: (node: TreeNode<T>) => TreeNode<T>, originalMap: Map<Key, TreeNode<T>>) {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node.parentKey) {\n let nextParent = map.get(node.parentKey);\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children.map(child => {\n if (child === node) {\n return newNode;\n }\n\n return child;\n });\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n return newNode;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, originalMap, parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [\n ...items.slice(0, index),\n ...newNodes,\n ...items.slice(index)\n ],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(items, parentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children.slice(0, index),\n ...newNodes,\n ...parentNode.children.slice(index)\n ]\n }), newMap);\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes.indexOf(node);\n this.insert(parentNode?.key, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes.indexOf(node);\n this.insert(parentNode?.key, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n newMap.set(movedNode.key, movedNode);\n return {items: [\n ...newItems.slice(0, index),\n movedNode,\n ...newItems.slice(index)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children.slice(0, index),\n movedNode,\n ...parentNode.children.slice(index)\n ]\n }), newMap);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) => updateTree(items, oldKey, oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n }, originalMap));\n }\n };\n}\n"],"names":[],"version":3,"file":"useTreeData.main.js.map"}