@react-stately/data 3.13.1 → 3.14.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/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/useAsyncList.main.js +1 -1
- package/dist/useAsyncList.main.js.map +1 -1
- package/dist/useAsyncList.mjs +1 -1
- package/dist/useAsyncList.module.js +1 -1
- package/dist/useAsyncList.module.js.map +1 -1
- package/dist/useListData.main.js +30 -0
- package/dist/useListData.main.js.map +1 -1
- package/dist/useListData.mjs +30 -0
- package/dist/useListData.module.js +30 -0
- package/dist/useListData.module.js.map +1 -1
- package/dist/useTreeData.main.js +2 -1
- package/dist/useTreeData.main.js.map +1 -1
- package/dist/useTreeData.mjs +2 -1
- package/dist/useTreeData.module.js +2 -1
- package/dist/useTreeData.module.js.map +1 -1
- package/package.json +3 -3
- package/src/useAsyncList.ts +1 -1
- package/src/useListData.ts +43 -0
- package/src/useTreeData.ts +3 -1
package/dist/types.d.ts
CHANGED
|
@@ -18,6 +18,10 @@ export interface ListData<T> {
|
|
|
18
18
|
selectedKeys: Selection;
|
|
19
19
|
/** Sets the selected keys. */
|
|
20
20
|
setSelectedKeys(keys: Selection): void;
|
|
21
|
+
/** Adds the given keys to the current selected keys. */
|
|
22
|
+
addKeysToSelection(keys: Selection): void;
|
|
23
|
+
/** Removes the given keys from the current selected keys. */
|
|
24
|
+
removeKeysFromSelection(keys: Selection): void;
|
|
21
25
|
/** The current filter text. */
|
|
22
26
|
filterText: string;
|
|
23
27
|
/** Sets the filter text. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";AAeA,6BAA6B,CAAC;IAC5B,iCAAiC;IACjC,YAAY,CAAC,EAAE,CAAC,EAAE,CAAC;IACnB,iDAAiD;IACjD,mBAAmB,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC5C,8CAA8C;IA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E,CAAC,EAAE,GAAG,IAAI,CAAC;IAEpD;;;OAGG;IACH,MAAM,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAE7B;;;OAGG;IACH,mBAAmB,IAAI,IAAI,CAAC;IAE5B;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7D;;;;OAIG;IACH,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAEhD;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAE/C;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAA;CACpC;AAOD;;;GAGG;AACH,4BAA4B,CAAC,SAAS,MAAM,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CA6SlF","sources":["packages/@react-stately/data/src/packages/@react-stately/data/src/useListData.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/useAsyncList.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/useTreeData.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/index.ts","packages/@react-stately/data/src/index.ts"],"sourcesContent":[null,null,null,null,"/*\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\nexport {useAsyncList} from './useAsyncList';\nexport {useTreeData} from './useTreeData';\nexport {useListData} from './useListData';\n\nexport type {ListOptions, ListData} from './useListData';\nexport type {AsyncListOptions, AsyncListData} from './useAsyncList';\nexport type {TreeOptions, TreeData} from './useTreeData';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -256,7 +256,7 @@ function $1cb48366e5c5533f$export$bc3384a35de93d66(options) {
|
|
|
256
256
|
},
|
|
257
257
|
loadMore () {
|
|
258
258
|
// Ignore if already loading more or if performing server side filtering.
|
|
259
|
-
if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
259
|
+
if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
260
260
|
dispatchFetch({
|
|
261
261
|
type: 'loadingMore'
|
|
262
262
|
}, load);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAuGD,SAAS,8BAAc,IAA0B,EAAE,MAAoB;IACrE,IAAI;IACJ,OAAQ,KAAK,KAAK;QAChB,KAAK;QACL,KAAK;YACH,OAAQ,OAAO,IAAI;gBACjB,KAAK;gBACL,KAAK;gBACL,KAAK;gBACL,KAAK;wBAGW,oBAII;oBANlB,OAAO;wBACL,GAAG,IAAI;wBACP,YAAY,CAAA,qBAAA,OAAO,UAAU,cAAjB,gCAAA,qBAAqB,KAAK,UAAU;wBAChD,OAAO,OAAO,IAAI;wBAClB,iEAAiE;wBACjE,OAAO,OAAO,IAAI,KAAK,YAAY,EAAE,GAAG,KAAK,KAAK;wBAClD,gBAAgB,CAAA,yBAAA,OAAO,cAAc,cAArB,oCAAA,yBAAyB,KAAK,cAAc;wBAC5D,iBAAiB,OAAO,eAAe;oBACzC;gBACF,KAAK;wBAGE;oBAFL,OAAO;wBACL,GAAG,IAAI;4BACJ,kBAAA,OAAO,OAAO,cAAd,sCAAA,qBAAA,QAAiB,KAApB;oBACF;gBACF,KAAK;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C,IAAI,CAAC,CAAA,OAAQ,OAAO,UAAU;QAClD;QACA;YACE,cAAc;gBAAC,MAAM;YAAS,GAAG;QACnC;QACA;YACE,yEAAyE;YACzE,IAAI,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,KAAK,eAAe,KAAK,MAAM,IAAI,MAC/E;YAGF,cAAc;gBAAC,MAAM;YAAa,GAAG;QACvC;QACA,MAAK,cAA8B;YACjC,cAAc;gBAAC,MAAM;gCAAW;YAAc,GAAI,QAAQ;QAC5D;QACA,GAAG,CAAA,GAAA,2CAAgB,EAAE;YAAC,GAAG,OAAO;oBAAE;YAAQ,QAAQ,KAAK,MAAM;QAAA,GAAG,CAAA;YAC9D,SAAS;gBAAC,MAAM;gBAAU,SAAS;YAAE;QACvC,EAAE;QACF,eAAc,UAAkB;YAC9B,cAAc;gBAAC,MAAM;4BAAa;YAAU,GAAG;QACjD;IACF;AACF","sources":["packages/@react-stately/data/src/useAsyncList.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 {createListActions, ListData, ListState} from './useListData';\nimport {Key, LoadingState, Selection, SortDescriptor} from '@react-types/shared';\nimport {useEffect, useReducer, useRef} from 'react';\n\nexport interface AsyncListOptions<T, C> {\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** The initial sort descriptor. */\n initialSortDescriptor?: SortDescriptor,\n /** The initial filter text. */\n initialFilterText?: string,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that loads the data for the items in the list. */\n load: AsyncListLoadFunction<T, C>,\n /**\n * An optional function that performs sorting. If not provided,\n * then `sortDescriptor` is passed to the `load` function.\n */\n sort?: AsyncListLoadFunction<T, C, AsyncListLoadOptions<T, C> & {sortDescriptor: SortDescriptor}>\n}\n\ntype AsyncListLoadFunction<T, C, S extends AsyncListLoadOptions<T, C> = AsyncListLoadOptions<T, C>> = (state: S) => AsyncListStateUpdate<T, C> | Promise<AsyncListStateUpdate<T, C>>;\n\ninterface AsyncListLoadOptions<T, C> {\n /** The items currently in the list. */\n items: T[],\n /** The keys of the currently selected items in the list. */\n selectedKeys: Selection,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n /** An abort signal used to notify the load function that the request has been aborted. */\n signal: AbortSignal,\n /** The pagination cursor returned from the last page load. */\n cursor?: C,\n /** The current filter text used to perform server side filtering. */\n filterText?: string,\n /** The current loading state of the list. */\n loadingState?: LoadingState\n}\n\ninterface AsyncListStateUpdate<T, C> {\n /** The new items to append to the list. */\n items: Iterable<T>,\n /** The keys to add to the selection. */\n selectedKeys?: Iterable<Key>,\n /** The sort descriptor to set. */\n sortDescriptor?: SortDescriptor,\n /** The pagination cursor to be used for the next page load. */\n cursor?: C,\n /** The updated filter text for the list. */\n filterText?: string\n}\n\ninterface AsyncListState<T, C> extends ListState<T> {\n state: LoadingState,\n items: T[],\n // disabledKeys?: Iterable<Key>,\n selectedKeys: Selection,\n // selectedKey?: Key,\n // expandedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n cursor?: C\n}\n\ntype ActionType = 'success' | 'error' | 'loading' | 'loadingMore' | 'sorting' | 'update' | 'filtering';\ninterface Action<T, C> {\n type: ActionType,\n items?: Iterable<T>,\n selectedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n updater?: (state: ListState<T>) => ListState<T>,\n cursor?: C,\n filterText?: string\n}\n\nexport interface AsyncListData<T> extends ListData<T> {\n /** Whether data is currently being loaded. */\n isLoading: boolean,\n /** If loading data failed, then this contains the error that occurred. */\n error?: Error,\n // disabledKeys?: Set<Key>,\n // selectedKey?: Key,\n // expandedKeys?: Set<Key>,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n\n /** Reloads the data in the list. */\n reload(): void,\n /** Loads the next page of data in the list. */\n loadMore(): void,\n /** Triggers sorting for the list. */\n sort(descriptor: SortDescriptor): void,\n /** The current loading state for the list. */\n loadingState: LoadingState\n}\n\nfunction reducer<T, C>(data: AsyncListState<T, C>, action: Action<T, C>): AsyncListState<T, C> {\n let selectedKeys;\n switch (data.state) {\n case 'idle':\n case 'error':\n switch (action.type) {\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: action.abortController\n };\n case 'update':\n return {\n ...data,\n ...action.updater?.(data)\n };\n case 'success':\n case 'error':\n return data;\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loading':\n case 'sorting':\n case 'filtering':\n switch (action.type) {\n case 'success':\n // Ignore if there is a newer abortcontroller in state.\n // This means that multiple requests were going at once.\n // We want to take only the latest result.\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n selectedKeys = action.selectedKeys ?? data.selectedKeys;\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: 'idle',\n items: [...(action.items) ?? []],\n selectedKeys: selectedKeys === 'all' ? 'all' : new Set(selectedKeys),\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error,\n abortController: undefined\n };\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n // We're already loading, and another load was triggered at the same time.\n // We need to abort the previous load and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loadingMore':\n switch (action.type) {\n case 'success':\n selectedKeys = (data.selectedKeys === 'all' || action.selectedKeys === 'all')\n ? 'all'\n : new Set([...data.selectedKeys, ...(action.selectedKeys ?? [])]);\n // Append the new items\n return {\n ...data,\n state: 'idle',\n items: [...data.items, ...(action.items ?? [])],\n selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error\n };\n case 'loading':\n case 'sorting':\n case 'filtering':\n // We're already loading more, and another load was triggered at the same time.\n // We need to abort the previous load more and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'loadingMore':\n // If already loading more and another loading more is triggered, abort the new load more since\n // it is a duplicate request since the cursor hasn't been updated.\n // Do not overwrite the data.abortController\n action.abortController?.abort();\n\n return data;\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n default:\n throw new Error(`Invalid state \"${data.state}\"`);\n }\n}\n\n/**\n * Manages state for an immutable async loaded list data structure, and provides convenience methods to\n * update the data over time. Manages loading and error states, pagination, and sorting.\n */\nexport function useAsyncList<T, C = string>(options: AsyncListOptions<T, C>): AsyncListData<T> {\n const {\n load,\n sort,\n initialSelectedKeys,\n initialSortDescriptor,\n getKey = (item: any) => item.id || item.key,\n initialFilterText = ''\n } = options;\n\n let [data, dispatch] = useReducer<AsyncListState<T, C>, [Action<T, C>]>(reducer, {\n state: 'idle',\n error: undefined,\n items: [],\n selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys),\n sortDescriptor: initialSortDescriptor,\n filterText: initialFilterText\n });\n\n const dispatchFetch = async (action: Action<T, C>, fn: AsyncListLoadFunction<T, C>) => {\n let abortController = new AbortController();\n try {\n dispatch({...action, abortController});\n let previousFilterText = action.filterText ?? data.filterText;\n\n let response = await fn({\n items: data.items.slice(),\n selectedKeys: data.selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n signal: abortController.signal,\n cursor: action.type === 'loadingMore' ? data.cursor : undefined,\n filterText: previousFilterText,\n loadingState: data.state\n });\n\n let filterText = response.filterText ?? previousFilterText;\n dispatch({type: 'success', ...response, abortController});\n\n // Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText\n // Only do this if not aborted (e.g. user triggers another filter action before load completes)\n if (filterText && (filterText !== previousFilterText) && !abortController.signal.aborted) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n } catch (e) {\n dispatch({type: 'error', error: e as Error, abortController});\n }\n };\n\n let didDispatchInitialFetch = useRef(false);\n useEffect(() => {\n if (!didDispatchInitialFetch.current) {\n dispatchFetch({type: 'loading'}, load);\n didDispatchInitialFetch.current = true;\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return {\n items: data.items,\n selectedKeys: data.selectedKeys,\n sortDescriptor: data.sortDescriptor,\n isLoading: data.state === 'loading' || data.state === 'loadingMore' || data.state === 'sorting' || data.state === 'filtering',\n loadingState: data.state,\n error: data.error,\n filterText: data.filterText,\n getItem(key: Key) {\n return data.items.find(item => getKey(item) === key);\n },\n reload() {\n dispatchFetch({type: 'loading'}, load);\n },\n loadMore() {\n // Ignore if already loading more or if performing server side filtering.\n if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {\n return;\n }\n\n dispatchFetch({type: 'loadingMore'}, load);\n },\n sort(sortDescriptor: SortDescriptor) {\n dispatchFetch({type: 'sorting', sortDescriptor}, (sort || load) as AsyncListLoadFunction<T, C>);\n },\n ...createListActions({...options, getKey, cursor: data.cursor}, fn => {\n dispatch({type: 'update', updater: fn});\n }),\n setFilterText(filterText: string) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n };\n}\n"],"names":[],"version":3,"file":"useAsyncList.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAuGD,SAAS,8BAAc,IAA0B,EAAE,MAAoB;IACrE,IAAI;IACJ,OAAQ,KAAK,KAAK;QAChB,KAAK;QACL,KAAK;YACH,OAAQ,OAAO,IAAI;gBACjB,KAAK;gBACL,KAAK;gBACL,KAAK;gBACL,KAAK;wBAGW,oBAII;oBANlB,OAAO;wBACL,GAAG,IAAI;wBACP,YAAY,CAAA,qBAAA,OAAO,UAAU,cAAjB,gCAAA,qBAAqB,KAAK,UAAU;wBAChD,OAAO,OAAO,IAAI;wBAClB,iEAAiE;wBACjE,OAAO,OAAO,IAAI,KAAK,YAAY,EAAE,GAAG,KAAK,KAAK;wBAClD,gBAAgB,CAAA,yBAAA,OAAO,cAAc,cAArB,oCAAA,yBAAyB,KAAK,cAAc;wBAC5D,iBAAiB,OAAO,eAAe;oBACzC;gBACF,KAAK;wBAGE;oBAFL,OAAO;wBACL,GAAG,IAAI;4BACJ,kBAAA,OAAO,OAAO,cAAd,sCAAA,qBAAA,QAAiB,KAApB;oBACF;gBACF,KAAK;gBACL,KAAK;oBACH,OAAO;gBACT;oBACE,MAAM,IAAI,MAAM,CAAC,gBAAgB,EAAE,OAAO,IAAI,CAAC,YAAY,EAAE,KAAK,KAAK,CAAC,C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AAK,KAAK,KAAK,aAAa,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,KAAK,eAAe,KAAK,MAAM,IAAI,MAC3G;YAGF,cAAc;gBAAC,MAAM;YAAa,GAAG;QACvC;QACA,MAAK,cAA8B;YACjC,cAAc;gBAAC,MAAM;gCAAW;YAAc,GAAI,QAAQ;QAC5D;QACA,GAAG,CAAA,GAAA,2CAAgB,EAAE;YAAC,GAAG,OAAO;oBAAE;YAAQ,QAAQ,KAAK,MAAM;QAAA,GAAG,CAAA;YAC9D,SAAS;gBAAC,MAAM;gBAAU,SAAS;YAAE;QACvC,EAAE;QACF,eAAc,UAAkB;YAC9B,cAAc;gBAAC,MAAM;4BAAa;YAAU,GAAG;QACjD;IACF;AACF","sources":["packages/@react-stately/data/src/useAsyncList.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 {createListActions, ListData, ListState} from './useListData';\nimport {Key, LoadingState, Selection, SortDescriptor} from '@react-types/shared';\nimport {useEffect, useReducer, useRef} from 'react';\n\nexport interface AsyncListOptions<T, C> {\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** The initial sort descriptor. */\n initialSortDescriptor?: SortDescriptor,\n /** The initial filter text. */\n initialFilterText?: string,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that loads the data for the items in the list. */\n load: AsyncListLoadFunction<T, C>,\n /**\n * An optional function that performs sorting. If not provided,\n * then `sortDescriptor` is passed to the `load` function.\n */\n sort?: AsyncListLoadFunction<T, C, AsyncListLoadOptions<T, C> & {sortDescriptor: SortDescriptor}>\n}\n\ntype AsyncListLoadFunction<T, C, S extends AsyncListLoadOptions<T, C> = AsyncListLoadOptions<T, C>> = (state: S) => AsyncListStateUpdate<T, C> | Promise<AsyncListStateUpdate<T, C>>;\n\ninterface AsyncListLoadOptions<T, C> {\n /** The items currently in the list. */\n items: T[],\n /** The keys of the currently selected items in the list. */\n selectedKeys: Selection,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n /** An abort signal used to notify the load function that the request has been aborted. */\n signal: AbortSignal,\n /** The pagination cursor returned from the last page load. */\n cursor?: C,\n /** The current filter text used to perform server side filtering. */\n filterText?: string,\n /** The current loading state of the list. */\n loadingState?: LoadingState\n}\n\ninterface AsyncListStateUpdate<T, C> {\n /** The new items to append to the list. */\n items: Iterable<T>,\n /** The keys to add to the selection. */\n selectedKeys?: Iterable<Key>,\n /** The sort descriptor to set. */\n sortDescriptor?: SortDescriptor,\n /** The pagination cursor to be used for the next page load. */\n cursor?: C,\n /** The updated filter text for the list. */\n filterText?: string\n}\n\ninterface AsyncListState<T, C> extends ListState<T> {\n state: LoadingState,\n items: T[],\n // disabledKeys?: Iterable<Key>,\n selectedKeys: Selection,\n // selectedKey?: Key,\n // expandedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n cursor?: C\n}\n\ntype ActionType = 'success' | 'error' | 'loading' | 'loadingMore' | 'sorting' | 'update' | 'filtering';\ninterface Action<T, C> {\n type: ActionType,\n items?: Iterable<T>,\n selectedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n updater?: (state: ListState<T>) => ListState<T>,\n cursor?: C,\n filterText?: string\n}\n\nexport interface AsyncListData<T> extends ListData<T> {\n /** Whether data is currently being loaded. */\n isLoading: boolean,\n /** If loading data failed, then this contains the error that occurred. */\n error?: Error,\n // disabledKeys?: Set<Key>,\n // selectedKey?: Key,\n // expandedKeys?: Set<Key>,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n\n /** Reloads the data in the list. */\n reload(): void,\n /** Loads the next page of data in the list. */\n loadMore(): void,\n /** Triggers sorting for the list. */\n sort(descriptor: SortDescriptor): void,\n /** The current loading state for the list. */\n loadingState: LoadingState\n}\n\nfunction reducer<T, C>(data: AsyncListState<T, C>, action: Action<T, C>): AsyncListState<T, C> {\n let selectedKeys;\n switch (data.state) {\n case 'idle':\n case 'error':\n switch (action.type) {\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: action.abortController\n };\n case 'update':\n return {\n ...data,\n ...action.updater?.(data)\n };\n case 'success':\n case 'error':\n return data;\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loading':\n case 'sorting':\n case 'filtering':\n switch (action.type) {\n case 'success':\n // Ignore if there is a newer abortcontroller in state.\n // This means that multiple requests were going at once.\n // We want to take only the latest result.\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n selectedKeys = action.selectedKeys ?? data.selectedKeys;\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: 'idle',\n items: [...(action.items) ?? []],\n selectedKeys: selectedKeys === 'all' ? 'all' : new Set(selectedKeys),\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error,\n abortController: undefined\n };\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n // We're already loading, and another load was triggered at the same time.\n // We need to abort the previous load and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loadingMore':\n switch (action.type) {\n case 'success':\n selectedKeys = (data.selectedKeys === 'all' || action.selectedKeys === 'all')\n ? 'all'\n : new Set([...data.selectedKeys, ...(action.selectedKeys ?? [])]);\n // Append the new items\n return {\n ...data,\n state: 'idle',\n items: [...data.items, ...(action.items ?? [])],\n selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error\n };\n case 'loading':\n case 'sorting':\n case 'filtering':\n // We're already loading more, and another load was triggered at the same time.\n // We need to abort the previous load more and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'loadingMore':\n // If already loading more and another loading more is triggered, abort the new load more since\n // it is a duplicate request since the cursor hasn't been updated.\n // Do not overwrite the data.abortController\n action.abortController?.abort();\n\n return data;\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n default:\n throw new Error(`Invalid state \"${data.state}\"`);\n }\n}\n\n/**\n * Manages state for an immutable async loaded list data structure, and provides convenience methods to\n * update the data over time. Manages loading and error states, pagination, and sorting.\n */\nexport function useAsyncList<T, C = string>(options: AsyncListOptions<T, C>): AsyncListData<T> {\n const {\n load,\n sort,\n initialSelectedKeys,\n initialSortDescriptor,\n getKey = (item: any) => item.id || item.key,\n initialFilterText = ''\n } = options;\n\n let [data, dispatch] = useReducer<AsyncListState<T, C>, [Action<T, C>]>(reducer, {\n state: 'idle',\n error: undefined,\n items: [],\n selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys),\n sortDescriptor: initialSortDescriptor,\n filterText: initialFilterText\n });\n\n const dispatchFetch = async (action: Action<T, C>, fn: AsyncListLoadFunction<T, C>) => {\n let abortController = new AbortController();\n try {\n dispatch({...action, abortController});\n let previousFilterText = action.filterText ?? data.filterText;\n\n let response = await fn({\n items: data.items.slice(),\n selectedKeys: data.selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n signal: abortController.signal,\n cursor: action.type === 'loadingMore' ? data.cursor : undefined,\n filterText: previousFilterText,\n loadingState: data.state\n });\n\n let filterText = response.filterText ?? previousFilterText;\n dispatch({type: 'success', ...response, abortController});\n\n // Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText\n // Only do this if not aborted (e.g. user triggers another filter action before load completes)\n if (filterText && (filterText !== previousFilterText) && !abortController.signal.aborted) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n } catch (e) {\n dispatch({type: 'error', error: e as Error, abortController});\n }\n };\n\n let didDispatchInitialFetch = useRef(false);\n useEffect(() => {\n if (!didDispatchInitialFetch.current) {\n dispatchFetch({type: 'loading'}, load);\n didDispatchInitialFetch.current = true;\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return {\n items: data.items,\n selectedKeys: data.selectedKeys,\n sortDescriptor: data.sortDescriptor,\n isLoading: data.state === 'loading' || data.state === 'loadingMore' || data.state === 'sorting' || data.state === 'filtering',\n loadingState: data.state,\n error: data.error,\n filterText: data.filterText,\n getItem(key: Key) {\n return data.items.find(item => getKey(item) === key);\n },\n reload() {\n dispatchFetch({type: 'loading'}, load);\n },\n loadMore() {\n // Ignore if already loading more or if performing server side filtering.\n if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {\n return;\n }\n\n dispatchFetch({type: 'loadingMore'}, load);\n },\n sort(sortDescriptor: SortDescriptor) {\n dispatchFetch({type: 'sorting', sortDescriptor}, (sort || load) as AsyncListLoadFunction<T, C>);\n },\n ...createListActions({...options, getKey, cursor: data.cursor}, fn => {\n dispatch({type: 'update', updater: fn});\n }),\n setFilterText(filterText: string) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n };\n}\n"],"names":[],"version":3,"file":"useAsyncList.main.js.map"}
|
package/dist/useAsyncList.mjs
CHANGED
|
@@ -250,7 +250,7 @@ function $f86e6c1ec7da6ebb$export$bc3384a35de93d66(options) {
|
|
|
250
250
|
},
|
|
251
251
|
loadMore () {
|
|
252
252
|
// Ignore if already loading more or if performing server side filtering.
|
|
253
|
-
if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
253
|
+
if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
254
254
|
dispatchFetch({
|
|
255
255
|
type: 'loadingMore'
|
|
256
256
|
}, load);
|
|
@@ -250,7 +250,7 @@ function $f86e6c1ec7da6ebb$export$bc3384a35de93d66(options) {
|
|
|
250
250
|
},
|
|
251
251
|
loadMore () {
|
|
252
252
|
// Ignore if already loading more or if performing server side filtering.
|
|
253
|
-
if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
253
|
+
if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
254
254
|
dispatchFetch({
|
|
255
255
|
type: 'loadingMore'
|
|
256
256
|
}, load);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAuGD,SAAS,8BAAc,IAA0B,EAAE,MAAoB;IACrE,IAAI;IACJ,OAAQ,KAAK,KAAK;QAChB,KAAK;QACL,KAAK;YACH,OAAQ,OAAO,IAAI;gBACjB,KAAK;gBAC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,EAAE;IACrC,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,CAAC,wBAAwB,OAAO,EAAE;YACpC,cAAc;gBAAC,MAAM;YAAS,GAAG;YACjC,wBAAwB,OAAO,GAAG;QACpC;IACF,uDAAuD;IACvD,GAAG,EAAE;IAEL,OAAO;QACL,OAAO,KAAK,KAAK;QACjB,cAAc,KAAK,YAAY;QAC/B,gBAAgB,KAAK,cAAc;QACnC,WAAW,KAAK,KAAK,KAAK,aAAa,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,KAAK,aAAa,KAAK,KAAK,KAAK;QAClH,cAAc,KAAK,KAAK;QACxB,OAAO,KAAK,KAAK;QACjB,YAAY,KAAK,UAAU;QAC3B,SAAQ,GAAQ;YACd,OAAO,KAAK,KAAK,CAAC,IAAI,CAAC,CAAA,OAAQ,OAAO,UAAU;QAClD;QACA;YACE,cAAc;gBAAC,MAAM;YAAS,GAAG;QACnC;QACA;YACE,yEAAyE;YACzE,IAAI,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,KAAK,eAAe,KAAK,MAAM,IAAI,MAC/E;YAGF,cAAc;gBAAC,MAAM;YAAa,GAAG;QACvC;QACA,MAAK,cAA8B;YACjC,cAAc;gBAAC,MAAM;gCAAW;YAAc,GAAI,QAAQ;QAC5D;QACA,GAAG,CAAA,GAAA,yCAAgB,EAAE;YAAC,GAAG,OAAO;oBAAE;YAAQ,QAAQ,KAAK,MAAM;QAAA,GAAG,CAAA;YAC9D,SAAS;gBAAC,MAAM;gBAAU,SAAS;YAAE;QACvC,EAAE;QACF,eAAc,UAAkB;YAC9B,cAAc;gBAAC,MAAM;4BAAa;YAAU,GAAG;QACjD;IACF;AACF","sources":["packages/@react-stately/data/src/useAsyncList.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 {createListActions, ListData, ListState} from './useListData';\nimport {Key, LoadingState, Selection, SortDescriptor} from '@react-types/shared';\nimport {useEffect, useReducer, useRef} from 'react';\n\nexport interface AsyncListOptions<T, C> {\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** The initial sort descriptor. */\n initialSortDescriptor?: SortDescriptor,\n /** The initial filter text. */\n initialFilterText?: string,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that loads the data for the items in the list. */\n load: AsyncListLoadFunction<T, C>,\n /**\n * An optional function that performs sorting. If not provided,\n * then `sortDescriptor` is passed to the `load` function.\n */\n sort?: AsyncListLoadFunction<T, C, AsyncListLoadOptions<T, C> & {sortDescriptor: SortDescriptor}>\n}\n\ntype AsyncListLoadFunction<T, C, S extends AsyncListLoadOptions<T, C> = AsyncListLoadOptions<T, C>> = (state: S) => AsyncListStateUpdate<T, C> | Promise<AsyncListStateUpdate<T, C>>;\n\ninterface AsyncListLoadOptions<T, C> {\n /** The items currently in the list. */\n items: T[],\n /** The keys of the currently selected items in the list. */\n selectedKeys: Selection,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n /** An abort signal used to notify the load function that the request has been aborted. */\n signal: AbortSignal,\n /** The pagination cursor returned from the last page load. */\n cursor?: C,\n /** The current filter text used to perform server side filtering. */\n filterText?: string,\n /** The current loading state of the list. */\n loadingState?: LoadingState\n}\n\ninterface AsyncListStateUpdate<T, C> {\n /** The new items to append to the list. */\n items: Iterable<T>,\n /** The keys to add to the selection. */\n selectedKeys?: Iterable<Key>,\n /** The sort descriptor to set. */\n sortDescriptor?: SortDescriptor,\n /** The pagination cursor to be used for the next page load. */\n cursor?: C,\n /** The updated filter text for the list. */\n filterText?: string\n}\n\ninterface AsyncListState<T, C> extends ListState<T> {\n state: LoadingState,\n items: T[],\n // disabledKeys?: Iterable<Key>,\n selectedKeys: Selection,\n // selectedKey?: Key,\n // expandedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n cursor?: C\n}\n\ntype ActionType = 'success' | 'error' | 'loading' | 'loadingMore' | 'sorting' | 'update' | 'filtering';\ninterface Action<T, C> {\n type: ActionType,\n items?: Iterable<T>,\n selectedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n updater?: (state: ListState<T>) => ListState<T>,\n cursor?: C,\n filterText?: string\n}\n\nexport interface AsyncListData<T> extends ListData<T> {\n /** Whether data is currently being loaded. */\n isLoading: boolean,\n /** If loading data failed, then this contains the error that occurred. */\n error?: Error,\n // disabledKeys?: Set<Key>,\n // selectedKey?: Key,\n // expandedKeys?: Set<Key>,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n\n /** Reloads the data in the list. */\n reload(): void,\n /** Loads the next page of data in the list. */\n loadMore(): void,\n /** Triggers sorting for the list. */\n sort(descriptor: SortDescriptor): void,\n /** The current loading state for the list. */\n loadingState: LoadingState\n}\n\nfunction reducer<T, C>(data: AsyncListState<T, C>, action: Action<T, C>): AsyncListState<T, C> {\n let selectedKeys;\n switch (data.state) {\n case 'idle':\n case 'error':\n switch (action.type) {\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: action.abortController\n };\n case 'update':\n return {\n ...data,\n ...action.updater?.(data)\n };\n case 'success':\n case 'error':\n return data;\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loading':\n case 'sorting':\n case 'filtering':\n switch (action.type) {\n case 'success':\n // Ignore if there is a newer abortcontroller in state.\n // This means that multiple requests were going at once.\n // We want to take only the latest result.\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n selectedKeys = action.selectedKeys ?? data.selectedKeys;\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: 'idle',\n items: [...(action.items) ?? []],\n selectedKeys: selectedKeys === 'all' ? 'all' : new Set(selectedKeys),\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error,\n abortController: undefined\n };\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n // We're already loading, and another load was triggered at the same time.\n // We need to abort the previous load and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loadingMore':\n switch (action.type) {\n case 'success':\n selectedKeys = (data.selectedKeys === 'all' || action.selectedKeys === 'all')\n ? 'all'\n : new Set([...data.selectedKeys, ...(action.selectedKeys ?? [])]);\n // Append the new items\n return {\n ...data,\n state: 'idle',\n items: [...data.items, ...(action.items ?? [])],\n selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error\n };\n case 'loading':\n case 'sorting':\n case 'filtering':\n // We're already loading more, and another load was triggered at the same time.\n // We need to abort the previous load more and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'loadingMore':\n // If already loading more and another loading more is triggered, abort the new load more since\n // it is a duplicate request since the cursor hasn't been updated.\n // Do not overwrite the data.abortController\n action.abortController?.abort();\n\n return data;\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n default:\n throw new Error(`Invalid state \"${data.state}\"`);\n }\n}\n\n/**\n * Manages state for an immutable async loaded list data structure, and provides convenience methods to\n * update the data over time. Manages loading and error states, pagination, and sorting.\n */\nexport function useAsyncList<T, C = string>(options: AsyncListOptions<T, C>): AsyncListData<T> {\n const {\n load,\n sort,\n initialSelectedKeys,\n initialSortDescriptor,\n getKey = (item: any) => item.id || item.key,\n initialFilterText = ''\n } = options;\n\n let [data, dispatch] = useReducer<AsyncListState<T, C>, [Action<T, C>]>(reducer, {\n state: 'idle',\n error: undefined,\n items: [],\n selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys),\n sortDescriptor: initialSortDescriptor,\n filterText: initialFilterText\n });\n\n const dispatchFetch = async (action: Action<T, C>, fn: AsyncListLoadFunction<T, C>) => {\n let abortController = new AbortController();\n try {\n dispatch({...action, abortController});\n let previousFilterText = action.filterText ?? data.filterText;\n\n let response = await fn({\n items: data.items.slice(),\n selectedKeys: data.selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n signal: abortController.signal,\n cursor: action.type === 'loadingMore' ? data.cursor : undefined,\n filterText: previousFilterText,\n loadingState: data.state\n });\n\n let filterText = response.filterText ?? previousFilterText;\n dispatch({type: 'success', ...response, abortController});\n\n // Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText\n // Only do this if not aborted (e.g. user triggers another filter action before load completes)\n if (filterText && (filterText !== previousFilterText) && !abortController.signal.aborted) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n } catch (e) {\n dispatch({type: 'error', error: e as Error, abortController});\n }\n };\n\n let didDispatchInitialFetch = useRef(false);\n useEffect(() => {\n if (!didDispatchInitialFetch.current) {\n dispatchFetch({type: 'loading'}, load);\n didDispatchInitialFetch.current = true;\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return {\n items: data.items,\n selectedKeys: data.selectedKeys,\n sortDescriptor: data.sortDescriptor,\n isLoading: data.state === 'loading' || data.state === 'loadingMore' || data.state === 'sorting' || data.state === 'filtering',\n loadingState: data.state,\n error: data.error,\n filterText: data.filterText,\n getItem(key: Key) {\n return data.items.find(item => getKey(item) === key);\n },\n reload() {\n dispatchFetch({type: 'loading'}, load);\n },\n loadMore() {\n // Ignore if already loading more or if performing server side filtering.\n if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {\n return;\n }\n\n dispatchFetch({type: 'loadingMore'}, load);\n },\n sort(sortDescriptor: SortDescriptor) {\n dispatchFetch({type: 'sorting', sortDescriptor}, (sort || load) as AsyncListLoadFunction<T, C>);\n },\n ...createListActions({...options, getKey, cursor: data.cursor}, fn => {\n dispatch({type: 'update', updater: fn});\n }),\n setFilterText(filterText: string) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n };\n}\n"],"names":[],"version":3,"file":"useAsyncList.module.js.map"}
|
|
1
|
+
{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAuGD,SAAS,8BAAc,IAA0B,EAAE,MAAoB;IACrE,IAAI;IACJ,OAAQ,KAAK,KAAK;QAChB,KAAK;QACL,KAAK;YACH,OAAQ,OAAO,IAAI;gBACjB,KAAK;gBACL,KAAK;gBACL,KAAK;gBACL,KAAK;wBAGW,oBAII;oBANlB,OAAO;wBACL,GAAG,IAAI;wBACP,YAAY,CAAA,qBAAA,OAAO,UAAU,cAAjB,gCAAA,qBA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O,GAAG;QACpC;IACF,uDAAuD;IACvD,GAAG,EAAE;IAEL,OAAO;QACL,OAAO,KAAK,KAAK;QACjB,cAAc,KAAK,YAAY;QAC/B,gBAAgB,KAAK,cAAc;QACnC,WAAW,KAAK,KAAK,KAAK,aAAa,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,KAAK,aAAa,KAAK,KAAK,KAAK;QAClH,cAAc,KAAK,KAAK;QACxB,OAAO,KAAK,KAAK;QACjB,YAAY,KAAK,UAAU;QAC3B,SAAQ,GAAQ;YACd,OAAO,KAAK,KAAK,CAAC,IAAI,CAAC,CAAA,OAAQ,OAAO,UAAU;QAClD;QACA;YACE,cAAc;gBAAC,MAAM;YAAS,GAAG;QACnC;QACA;YACE,yEAAyE;YACzE,IAAI,KAAK,KAAK,KAAK,aAAa,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,KAAK,eAAe,KAAK,MAAM,IAAI,MAC3G;YAGF,cAAc;gBAAC,MAAM;YAAa,GAAG;QACvC;QACA,MAAK,cAA8B;YACjC,cAAc;gBAAC,MAAM;gCAAW;YAAc,GAAI,QAAQ;QAC5D;QACA,GAAG,CAAA,GAAA,yCAAgB,EAAE;YAAC,GAAG,OAAO;oBAAE;YAAQ,QAAQ,KAAK,MAAM;QAAA,GAAG,CAAA;YAC9D,SAAS;gBAAC,MAAM;gBAAU,SAAS;YAAE;QACvC,EAAE;QACF,eAAc,UAAkB;YAC9B,cAAc;gBAAC,MAAM;4BAAa;YAAU,GAAG;QACjD;IACF;AACF","sources":["packages/@react-stately/data/src/useAsyncList.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 {createListActions, ListData, ListState} from './useListData';\nimport {Key, LoadingState, Selection, SortDescriptor} from '@react-types/shared';\nimport {useEffect, useReducer, useRef} from 'react';\n\nexport interface AsyncListOptions<T, C> {\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** The initial sort descriptor. */\n initialSortDescriptor?: SortDescriptor,\n /** The initial filter text. */\n initialFilterText?: string,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that loads the data for the items in the list. */\n load: AsyncListLoadFunction<T, C>,\n /**\n * An optional function that performs sorting. If not provided,\n * then `sortDescriptor` is passed to the `load` function.\n */\n sort?: AsyncListLoadFunction<T, C, AsyncListLoadOptions<T, C> & {sortDescriptor: SortDescriptor}>\n}\n\ntype AsyncListLoadFunction<T, C, S extends AsyncListLoadOptions<T, C> = AsyncListLoadOptions<T, C>> = (state: S) => AsyncListStateUpdate<T, C> | Promise<AsyncListStateUpdate<T, C>>;\n\ninterface AsyncListLoadOptions<T, C> {\n /** The items currently in the list. */\n items: T[],\n /** The keys of the currently selected items in the list. */\n selectedKeys: Selection,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n /** An abort signal used to notify the load function that the request has been aborted. */\n signal: AbortSignal,\n /** The pagination cursor returned from the last page load. */\n cursor?: C,\n /** The current filter text used to perform server side filtering. */\n filterText?: string,\n /** The current loading state of the list. */\n loadingState?: LoadingState\n}\n\ninterface AsyncListStateUpdate<T, C> {\n /** The new items to append to the list. */\n items: Iterable<T>,\n /** The keys to add to the selection. */\n selectedKeys?: Iterable<Key>,\n /** The sort descriptor to set. */\n sortDescriptor?: SortDescriptor,\n /** The pagination cursor to be used for the next page load. */\n cursor?: C,\n /** The updated filter text for the list. */\n filterText?: string\n}\n\ninterface AsyncListState<T, C> extends ListState<T> {\n state: LoadingState,\n items: T[],\n // disabledKeys?: Iterable<Key>,\n selectedKeys: Selection,\n // selectedKey?: Key,\n // expandedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n cursor?: C\n}\n\ntype ActionType = 'success' | 'error' | 'loading' | 'loadingMore' | 'sorting' | 'update' | 'filtering';\ninterface Action<T, C> {\n type: ActionType,\n items?: Iterable<T>,\n selectedKeys?: Iterable<Key>,\n sortDescriptor?: SortDescriptor,\n error?: Error,\n abortController?: AbortController,\n updater?: (state: ListState<T>) => ListState<T>,\n cursor?: C,\n filterText?: string\n}\n\nexport interface AsyncListData<T> extends ListData<T> {\n /** Whether data is currently being loaded. */\n isLoading: boolean,\n /** If loading data failed, then this contains the error that occurred. */\n error?: Error,\n // disabledKeys?: Set<Key>,\n // selectedKey?: Key,\n // expandedKeys?: Set<Key>,\n /** The current sort descriptor for the list. */\n sortDescriptor?: SortDescriptor,\n\n /** Reloads the data in the list. */\n reload(): void,\n /** Loads the next page of data in the list. */\n loadMore(): void,\n /** Triggers sorting for the list. */\n sort(descriptor: SortDescriptor): void,\n /** The current loading state for the list. */\n loadingState: LoadingState\n}\n\nfunction reducer<T, C>(data: AsyncListState<T, C>, action: Action<T, C>): AsyncListState<T, C> {\n let selectedKeys;\n switch (data.state) {\n case 'idle':\n case 'error':\n switch (action.type) {\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: action.abortController\n };\n case 'update':\n return {\n ...data,\n ...action.updater?.(data)\n };\n case 'success':\n case 'error':\n return data;\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loading':\n case 'sorting':\n case 'filtering':\n switch (action.type) {\n case 'success':\n // Ignore if there is a newer abortcontroller in state.\n // This means that multiple requests were going at once.\n // We want to take only the latest result.\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n selectedKeys = action.selectedKeys ?? data.selectedKeys;\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: 'idle',\n items: [...(action.items) ?? []],\n selectedKeys: selectedKeys === 'all' ? 'all' : new Set(selectedKeys),\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error,\n abortController: undefined\n };\n case 'loading':\n case 'loadingMore':\n case 'sorting':\n case 'filtering':\n // We're already loading, and another load was triggered at the same time.\n // We need to abort the previous load and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n case 'loadingMore':\n switch (action.type) {\n case 'success':\n selectedKeys = (data.selectedKeys === 'all' || action.selectedKeys === 'all')\n ? 'all'\n : new Set([...data.selectedKeys, ...(action.selectedKeys ?? [])]);\n // Append the new items\n return {\n ...data,\n state: 'idle',\n items: [...data.items, ...(action.items ?? [])],\n selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n abortController: undefined,\n cursor: action.cursor\n };\n case 'error':\n if (action.abortController !== data.abortController) {\n return data;\n }\n\n return {\n ...data,\n state: 'error',\n error: action.error\n };\n case 'loading':\n case 'sorting':\n case 'filtering':\n // We're already loading more, and another load was triggered at the same time.\n // We need to abort the previous load more and start a new one.\n data.abortController?.abort();\n return {\n ...data,\n filterText: action.filterText ?? data.filterText,\n state: action.type,\n // Reset items to an empty list if loading, but not when sorting.\n items: action.type === 'loading' ? [] : data.items,\n abortController: action.abortController\n };\n case 'loadingMore':\n // If already loading more and another loading more is triggered, abort the new load more since\n // it is a duplicate request since the cursor hasn't been updated.\n // Do not overwrite the data.abortController\n action.abortController?.abort();\n\n return data;\n case 'update':\n // We're already loading, and an update happened at the same time (e.g. selectedKey changed).\n // Update data but don't abort previous load.\n return {\n ...data,\n ...action.updater?.(data)\n };\n default:\n throw new Error(`Invalid action \"${action.type}\" in state \"${data.state}\"`);\n }\n default:\n throw new Error(`Invalid state \"${data.state}\"`);\n }\n}\n\n/**\n * Manages state for an immutable async loaded list data structure, and provides convenience methods to\n * update the data over time. Manages loading and error states, pagination, and sorting.\n */\nexport function useAsyncList<T, C = string>(options: AsyncListOptions<T, C>): AsyncListData<T> {\n const {\n load,\n sort,\n initialSelectedKeys,\n initialSortDescriptor,\n getKey = (item: any) => item.id || item.key,\n initialFilterText = ''\n } = options;\n\n let [data, dispatch] = useReducer<AsyncListState<T, C>, [Action<T, C>]>(reducer, {\n state: 'idle',\n error: undefined,\n items: [],\n selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys),\n sortDescriptor: initialSortDescriptor,\n filterText: initialFilterText\n });\n\n const dispatchFetch = async (action: Action<T, C>, fn: AsyncListLoadFunction<T, C>) => {\n let abortController = new AbortController();\n try {\n dispatch({...action, abortController});\n let previousFilterText = action.filterText ?? data.filterText;\n\n let response = await fn({\n items: data.items.slice(),\n selectedKeys: data.selectedKeys,\n sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,\n signal: abortController.signal,\n cursor: action.type === 'loadingMore' ? data.cursor : undefined,\n filterText: previousFilterText,\n loadingState: data.state\n });\n\n let filterText = response.filterText ?? previousFilterText;\n dispatch({type: 'success', ...response, abortController});\n\n // Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText\n // Only do this if not aborted (e.g. user triggers another filter action before load completes)\n if (filterText && (filterText !== previousFilterText) && !abortController.signal.aborted) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n } catch (e) {\n dispatch({type: 'error', error: e as Error, abortController});\n }\n };\n\n let didDispatchInitialFetch = useRef(false);\n useEffect(() => {\n if (!didDispatchInitialFetch.current) {\n dispatchFetch({type: 'loading'}, load);\n didDispatchInitialFetch.current = true;\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return {\n items: data.items,\n selectedKeys: data.selectedKeys,\n sortDescriptor: data.sortDescriptor,\n isLoading: data.state === 'loading' || data.state === 'loadingMore' || data.state === 'sorting' || data.state === 'filtering',\n loadingState: data.state,\n error: data.error,\n filterText: data.filterText,\n getItem(key: Key) {\n return data.items.find(item => getKey(item) === key);\n },\n reload() {\n dispatchFetch({type: 'loading'}, load);\n },\n loadMore() {\n // Ignore if already loading more or if performing server side filtering.\n if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {\n return;\n }\n\n dispatchFetch({type: 'loadingMore'}, load);\n },\n sort(sortDescriptor: SortDescriptor) {\n dispatchFetch({type: 'sorting', sortDescriptor}, (sort || load) as AsyncListLoadFunction<T, C>);\n },\n ...createListActions({...options, getKey, cursor: data.cursor}, fn => {\n dispatch({type: 'update', updater: fn});\n }),\n setFilterText(filterText: string) {\n dispatchFetch({type: 'filtering', filterText}, load);\n }\n };\n}\n"],"names":[],"version":3,"file":"useAsyncList.module.js.map"}
|
package/dist/useListData.main.js
CHANGED
|
@@ -54,6 +54,36 @@ function $fbc1d750f520c94e$export$79c0c687a5963b0a(opts, dispatch) {
|
|
|
54
54
|
selectedKeys: selectedKeys
|
|
55
55
|
}));
|
|
56
56
|
},
|
|
57
|
+
addKeysToSelection (selectedKeys) {
|
|
58
|
+
dispatch((state)=>{
|
|
59
|
+
if (state.selectedKeys === 'all') return state;
|
|
60
|
+
if (selectedKeys === 'all') return {
|
|
61
|
+
...state,
|
|
62
|
+
selectedKeys: 'all'
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
...state,
|
|
66
|
+
selectedKeys: new Set([
|
|
67
|
+
...state.selectedKeys,
|
|
68
|
+
...selectedKeys
|
|
69
|
+
])
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
removeKeysFromSelection (selectedKeys) {
|
|
74
|
+
dispatch((state)=>{
|
|
75
|
+
if (selectedKeys === 'all') return {
|
|
76
|
+
...state,
|
|
77
|
+
selectedKeys: new Set()
|
|
78
|
+
};
|
|
79
|
+
let selection = state.selectedKeys === 'all' ? new Set(state.items.map(getKey)) : new Set(state.selectedKeys);
|
|
80
|
+
for (let key of selectedKeys)selection.delete(key);
|
|
81
|
+
return {
|
|
82
|
+
...state,
|
|
83
|
+
selectedKeys: selection
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
},
|
|
57
87
|
setFilterText (filterText) {
|
|
58
88
|
dispatch((state)=>({
|
|
59
89
|
...state,
|
|
@@ -1 +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;eAC3C,MAAM,qBACN,oBAAoB,IACrB,GAAG;IAEJ,uFAAuF;IACvF,IAAI,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,qBAAO,EAAgB;QAC7C,OAAO;QACP,cAAc,wBAAwB,QAAQ,QAAQ,IAAI,IAAI,uBAAuB,EAAE;QACvF,YAAY;IACd;IAEA,IAAI,gBAAgB,CAAA,GAAA,oBAAM,EACxB,IAAM,SAAS,MAAM,KAAK,CAAC,MAAM,CAAC,CAAA,OAAQ,OAAO,MAAM,MAAM,UAAU,KAAK,MAAM,KAAK,EACvF;QAAC,MAAM,KAAK;QAAE,MAAM,UAAU;QAAE;KAAO;IAEzC,OAAO;QACL,GAAG,KAAK;QACR,OAAO;QACP,GAAG,0CAAkB;oBAAC;QAAM,GAAG,SAA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KAAK,CAAC,KAAK,CAAC;SACtB;IACH;AACF;AAEA,SAAS,2BAAQ,KAAmB,EAAE,OAAiB,EAAE,OAAe;IACtE,kFAAkF;IAClF,WAAW,QAAQ,MAAM,CAAC,CAAA,QAAS,QAAQ,SAAS,MAAM;IAE1D,IAAI,QAAQ,QAAQ,GAAG,CAAC,CAAA,OAAS,CAAA;kBAC/B;YACA,IAAI;QACN,CAAA;IAEA,4DAA4D;IAC5D,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;QACrC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC,IAAI;QACrB,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;YACrC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC,IAAI;YAErB,IAAI,IAAI,GACN,KAAK,CAAC,EAAE,CAAC,IAAI;QAEjB;IACF;IAEA,iFAAiF;IACjF,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;QACrC,IAAI,IAAI,KAAK,CAAC,EAAE;QAChB,IAAK,IAAI,IAAI,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,IAAK;YACzC,IAAI,IAAI,KAAK,CAAC,EAAE;YAEhB,IAAI,EAAE,IAAI,GAAG,EAAE,EAAE,EACf,EAAE,EAAE;iBAEJ,EAAE,IAAI;QAEV;IACF;IAEA,IAAI,OAAO,MAAM,KAAK,CAAC,KAAK;IAC5B,KAAK,IAAI,QAAQ,MAAO;QACtB,IAAI,CAAC,KAAK,GAAG,KAAK,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 | undefined,\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":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ourcesContent":["/*\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 /** Adds the given keys to the current selected keys. */\n addKeysToSelection(keys: Selection): void,\n\n /** Removes the given keys from the current selected keys. */\n removeKeysFromSelection(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 | undefined,\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 addKeysToSelection(selectedKeys: Selection) {\n dispatch(state => {\n if (state.selectedKeys === 'all') {\n return state;\n }\n if (selectedKeys === 'all') {\n return {\n ...state,\n selectedKeys: 'all'\n };\n }\n\n return {\n ...state,\n selectedKeys: new Set([...state.selectedKeys, ...selectedKeys])\n };\n });\n },\n removeKeysFromSelection(selectedKeys: Selection) {\n dispatch(state => {\n if (selectedKeys === 'all') {\n return {\n ...state,\n selectedKeys: new Set()\n };\n }\n\n let selection: Selection = state.selectedKeys === 'all' ? new Set(state.items.map(getKey!)) : new Set(state.selectedKeys);\n for (let key of selectedKeys) {\n selection.delete(key);\n }\n return {\n ...state,\n selectedKeys: selection\n };\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.main.js.map"}
|
package/dist/useListData.mjs
CHANGED
|
@@ -47,6 +47,36 @@ function $0d86e9c8f07f9a7b$export$79c0c687a5963b0a(opts, dispatch) {
|
|
|
47
47
|
selectedKeys: selectedKeys
|
|
48
48
|
}));
|
|
49
49
|
},
|
|
50
|
+
addKeysToSelection (selectedKeys) {
|
|
51
|
+
dispatch((state)=>{
|
|
52
|
+
if (state.selectedKeys === 'all') return state;
|
|
53
|
+
if (selectedKeys === 'all') return {
|
|
54
|
+
...state,
|
|
55
|
+
selectedKeys: 'all'
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
...state,
|
|
59
|
+
selectedKeys: new Set([
|
|
60
|
+
...state.selectedKeys,
|
|
61
|
+
...selectedKeys
|
|
62
|
+
])
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
removeKeysFromSelection (selectedKeys) {
|
|
67
|
+
dispatch((state)=>{
|
|
68
|
+
if (selectedKeys === 'all') return {
|
|
69
|
+
...state,
|
|
70
|
+
selectedKeys: new Set()
|
|
71
|
+
};
|
|
72
|
+
let selection = state.selectedKeys === 'all' ? new Set(state.items.map(getKey)) : new Set(state.selectedKeys);
|
|
73
|
+
for (let key of selectedKeys)selection.delete(key);
|
|
74
|
+
return {
|
|
75
|
+
...state,
|
|
76
|
+
selectedKeys: selection
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
},
|
|
50
80
|
setFilterText (filterText) {
|
|
51
81
|
dispatch((state)=>({
|
|
52
82
|
...state,
|
|
@@ -47,6 +47,36 @@ function $0d86e9c8f07f9a7b$export$79c0c687a5963b0a(opts, dispatch) {
|
|
|
47
47
|
selectedKeys: selectedKeys
|
|
48
48
|
}));
|
|
49
49
|
},
|
|
50
|
+
addKeysToSelection (selectedKeys) {
|
|
51
|
+
dispatch((state)=>{
|
|
52
|
+
if (state.selectedKeys === 'all') return state;
|
|
53
|
+
if (selectedKeys === 'all') return {
|
|
54
|
+
...state,
|
|
55
|
+
selectedKeys: 'all'
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
...state,
|
|
59
|
+
selectedKeys: new Set([
|
|
60
|
+
...state.selectedKeys,
|
|
61
|
+
...selectedKeys
|
|
62
|
+
])
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
removeKeysFromSelection (selectedKeys) {
|
|
67
|
+
dispatch((state)=>{
|
|
68
|
+
if (selectedKeys === 'all') return {
|
|
69
|
+
...state,
|
|
70
|
+
selectedKeys: new Set()
|
|
71
|
+
};
|
|
72
|
+
let selection = state.selectedKeys === 'all' ? new Set(state.items.map(getKey)) : new Set(state.selectedKeys);
|
|
73
|
+
for (let key of selectedKeys)selection.delete(key);
|
|
74
|
+
return {
|
|
75
|
+
...state,
|
|
76
|
+
selectedKeys: selection
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
},
|
|
50
80
|
setFilterText (filterText) {
|
|
51
81
|
dispatch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IAAI,IAAI,GACN,KAAK,CAAC,EAAE,CAAC,IAAI;QAEjB;IACF;IAEA,iFAAiF;IACjF,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;QACrC,IAAI,IAAI,KAAK,CAAC,EAAE;QAChB,IAAK,IAAI,IAAI,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,IAAK;YACzC,IAAI,IAAI,KAAK,CAAC,EAAE;YAEhB,IAAI,EAAE,IAAI,GAAG,EAAE,EAAE,EACf,EAAE,EAAE;iBAEJ,EAAE,IAAI;QAEV;IACF;IAEA,IAAI,OAAO,MAAM,KAAK,CAAC,KAAK;IAC5B,KAAK,IAAI,QAAQ,MAAO;QACtB,IAAI,CAAC,KAAK,GAAG,KAAK,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 | undefined,\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"}
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AAsIM,SAAS,0CAAe,OAAuB;IACpD,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;eAC3C,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,IAAI,uBAAuB,EAAE;QACvF,YAAY;IACd;IAEA,IAAI,gBAAgB,CAAA,GAAA,cAAM,EACxB,IAAM,SAAS,M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IAAI,MAAM,MAAM,EAAE,IAAK;YACrC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC,IAAI;YAErB,IAAI,IAAI,GACN,KAAK,CAAC,EAAE,CAAC,IAAI;QAEjB;IACF;IAEA,iFAAiF;IACjF,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;QACrC,IAAI,IAAI,KAAK,CAAC,EAAE;QAChB,IAAK,IAAI,IAAI,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,IAAK;YACzC,IAAI,IAAI,KAAK,CAAC,EAAE;YAEhB,IAAI,EAAE,IAAI,GAAG,EAAE,EAAE,EACf,EAAE,EAAE;iBAEJ,EAAE,IAAI;QAEV;IACF;IAEA,IAAI,OAAO,MAAM,KAAK,CAAC,KAAK;IAC5B,KAAK,IAAI,QAAQ,MAAO;QACtB,IAAI,CAAC,KAAK,GAAG,KAAK,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 /** Adds the given keys to the current selected keys. */\n addKeysToSelection(keys: Selection): void,\n\n /** Removes the given keys from the current selected keys. */\n removeKeysFromSelection(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 | undefined,\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 addKeysToSelection(selectedKeys: Selection) {\n dispatch(state => {\n if (state.selectedKeys === 'all') {\n return state;\n }\n if (selectedKeys === 'all') {\n return {\n ...state,\n selectedKeys: 'all'\n };\n }\n\n return {\n ...state,\n selectedKeys: new Set([...state.selectedKeys, ...selectedKeys])\n };\n });\n },\n removeKeysFromSelection(selectedKeys: Selection) {\n dispatch(state => {\n if (selectedKeys === 'all') {\n return {\n ...state,\n selectedKeys: new Set()\n };\n }\n\n let selection: Selection = state.selectedKeys === 'all' ? new Set(state.items.map(getKey!)) : new Set(state.selectedKeys);\n for (let key of selectedKeys) {\n selection.delete(key);\n }\n return {\n ...state,\n selectedKeys: selection\n };\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"}
|
package/dist/useTreeData.main.js
CHANGED
|
@@ -301,7 +301,8 @@ function $2d16d1aab63a81f4$var$moveItems(state, keys, toParent, toIndex, updateT
|
|
|
301
301
|
newMap = nextMap;
|
|
302
302
|
}
|
|
303
303
|
// decrement the index if the child being removed is in the target parent and before the target index
|
|
304
|
-
|
|
304
|
+
// the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
|
|
305
|
+
if ((child.parentKey === toParent || child.parentKey === (toParent === null || toParent === void 0 ? void 0 : toParent.key)) && keyArray.includes(child.key) && ((toParent === null || toParent === void 0 ? void 0 : toParent.children) ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) toIndex--;
|
|
305
306
|
}
|
|
306
307
|
traversal({
|
|
307
308
|
children: items
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAkIM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;oBAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CAAC,MAAM,SAAS,GAAG,CAAA,GAAA,qBAAO,EAAoB,IAAM,UAAU,cAAc,IAAI;IACpF,IAAI,SAAC,KAAK,WAAE,OAAO,EAAC,GAAG;IAEvB,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,qBAAO,EAAE,IAAI,IAAS,uBAAuB,EAAE;IAErF,SAAS,UAAU,eAA2B,EAAE,EAAE,GAA0B,EAAE,SAAsB;QAClG,IAAI,gBAAgB,MAClB,eAAe,EAAE;Q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AiB;YAAI;YAC9E,IAAI,EAAC,OAAO,SAAS,EAAE,SAAS,OAAO,EAAC,GAAG,WAAW,UAAU,MAAM,GAAG,EAAE,IAAM,MAAM;YACvF,WAAW;YACX,SAAS;QACX;QACA,qGAAqG;QACrG,IAAI,MAAM,SAAS,MAAK,qBAAA,+BAAA,SAAU,GAAG,KAChC,SAAS,QAAQ,CAAC,MAAM,GAAG,KAC3B,AAAC,CAAA,CAAA,qBAAA,+BAAA,SAAU,QAAQ,IAAG,SAAS,QAAQ,CAAC,OAAO,CAAC,SAAS,MAAM,OAAO,CAAC,MAAK,IAAK,iBACpF;IAEJ;IAEA,UAAU;QAAC,UAAU;IAAK,GAAG;iBAAC;mBAAS;IAAS;IAEhD,IAAI,eAAe,aAAa,IAAI,CAAC,CAAC,GAAG,IAAM,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,IAAI;IACvG,8CAA8C;IAC9C,IAAI,CAAC,YAAY,SAAS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YAAC,OAAO;mBACV,SAAS,KAAK,CAAC,GAAG;mBAClB;mBACA,SAAS,KAAK,CAAC;aACnB;YAAE,SAAS;QAAM;IACpB;IAEA,uDAAuD;IACvD,OAAO,WAAW,UAAU,SAAS,GAAG,EAAE,CAAA,aAAe,CAAA;YACvD,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GAAI;AACN","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 | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\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> | undefined,\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 * 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 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\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[],\n nodeMap: Map<Key, TreeNode<T>>\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<TreeDataState<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 ?? null,\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 | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\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 && 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 && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\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 // newNode cannot be null here due to the above filter.\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 if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\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 ?? null, 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 ?? null, 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 addNode(movedNode, newMap);\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 moveBefore(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\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\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n if (child.parentKey === toParent?.key\n && keyArray.includes(child.key)\n && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap); \n });\n return {items: [\n ...newItems.slice(0, toIndex),\n ...inOrderItems,\n ...newItems.slice(toIndex)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParent.key, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }), newMap);\n}\n"],"names":[],"version":3,"file":"useTreeData.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAkIM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;oBAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CAAC,MAAM,SAAS,GAAG,CAAA,GAAA,qBAAO,EAAoB,IAAM,UAAU,cAAc,IAAI;IACpF,IAAI,SAAC,KAAK,WAAE,OAAO,EAAC,GAAG;IAEvB,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,qBAAO,EAAE,IAAI,IAAS,uBAAuB,EAAE;IAErF,SAAS,UAAU,eAA2B,EAAE,EAAE,GAA0B,EAAE,SAAsB;QAClG,IAAI,gBAAgB,MAClB,eAAe,EAAE;QAEnB,OAAO;YACL,OAAO,aAAa,GAAG,CAAC,CAAA;gBACtB,IAAI,OAAoB;oBACtB,KAAK,OAAO;oBACZ,WAAW,sBAAA,uBAAA,YAAa;o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AAW;YACX,SAAS;QACX;QACA,qGAAqG;QACrG,4GAA4G;QAC5G,IAAI,AAAC,CAAA,MAAM,SAAS,KAAK,YACnB,MAAM,SAAS,MAAK,qBAAA,+BAAA,SAAU,GAAG,CAAD,KACjC,SAAS,QAAQ,CAAC,MAAM,GAAG,KAC3B,AAAC,CAAA,CAAA,qBAAA,+BAAA,SAAU,QAAQ,IAAG,SAAS,QAAQ,CAAC,OAAO,CAAC,SAAS,MAAM,OAAO,CAAC,MAAK,IAAK,iBACpF;IAEJ;IAEA,UAAU;QAAC,UAAU;IAAK,GAAG;iBAAC;mBAAS;IAAS;IAEhD,IAAI,eAAe,aAAa,IAAI,CAAC,CAAC,GAAG,IAAM,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,IAAI;IACvG,8CAA8C;IAC9C,IAAI,CAAC,YAAY,SAAS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YAAC,OAAO;mBACV,SAAS,KAAK,CAAC,GAAG;mBAClB;mBACA,SAAS,KAAK,CAAC;aACnB;YAAE,SAAS;QAAM;IACpB;IAEA,uDAAuD;IACvD,OAAO,WAAW,UAAU,SAAS,GAAG,EAAE,CAAA,aAAe,CAAA;YACvD,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GAAI;AACN","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 | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\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> | undefined,\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 * 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 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\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[],\n nodeMap: Map<Key, TreeNode<T>>\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<TreeDataState<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 ?? null,\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 | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\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 && 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 && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\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 // newNode cannot be null here due to the above filter.\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 if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\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 ?? null, 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 ?? null, 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 addNode(movedNode, newMap);\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 moveBefore(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\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\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it\n if ((child.parentKey === toParent\n || child.parentKey === toParent?.key)\n && keyArray.includes(child.key)\n && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap); \n });\n return {items: [\n ...newItems.slice(0, toIndex),\n ...inOrderItems,\n ...newItems.slice(toIndex)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParent.key, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }), newMap);\n}\n"],"names":[],"version":3,"file":"useTreeData.main.js.map"}
|
package/dist/useTreeData.mjs
CHANGED
|
@@ -295,7 +295,8 @@ function $be2ea0343af54212$var$moveItems(state, keys, toParent, toIndex, updateT
|
|
|
295
295
|
newMap = nextMap;
|
|
296
296
|
}
|
|
297
297
|
// decrement the index if the child being removed is in the target parent and before the target index
|
|
298
|
-
|
|
298
|
+
// the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
|
|
299
|
+
if ((child.parentKey === toParent || child.parentKey === (toParent === null || toParent === void 0 ? void 0 : toParent.key)) && keyArray.includes(child.key) && ((toParent === null || toParent === void 0 ? void 0 : toParent.children) ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) toIndex--;
|
|
299
300
|
}
|
|
300
301
|
traversal({
|
|
301
302
|
children: items
|
|
@@ -295,7 +295,8 @@ function $be2ea0343af54212$var$moveItems(state, keys, toParent, toIndex, updateT
|
|
|
295
295
|
newMap = nextMap;
|
|
296
296
|
}
|
|
297
297
|
// decrement the index if the child being removed is in the target parent and before the target index
|
|
298
|
-
|
|
298
|
+
// the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
|
|
299
|
+
if ((child.parentKey === toParent || child.parentKey === (toParent === null || toParent === void 0 ? void 0 : toParent.key)) && keyArray.includes(child.key) && ((toParent === null || toParent === void 0 ? void 0 : toParent.children) ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) toIndex--;
|
|
299
300
|
}
|
|
300
301
|
traversal({
|
|
301
302
|
children: items
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAAA;;;;;;;;;;CAUC;AAkIM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;oBAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CAAC,MAAM,SAAS,GAAG,CAAA,GAAA,eAAO,EAAoB,IAAM,UAAU,cAAc,IAAI;IACpF,IAAI,SAAC,KAAK,WAAE,OAAO,EAAC,GAAG;IAEvB,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,eAAO,EAAE,IAAI,IAAS,uBAAuB,EAAE;IAErF,SAAS,UAAU,eAA2B,EAAE,EAAE,GAA0B,EAAE,SAAsB;QAClG,IAAI,gBAAgB,MAClB,eAAe,EAAE;QAEnB,OAAO;YACL,OAAO,aAAa,GAAG,CAAC,CAAA;gBACtB,IAAI,OAAoB;oBACtB,KAAK,OAAO;oBACZ,WAAW,sBAAA,uBAAA,YAAa;oBACxB,OAAO;oBACP,UAAU;gBACZ;gBAEA,KAAK,QAAQ,GAAG,UAAU,YAAY,OAAO,KAAK,KAAK,GAAG,EAAE,KAAK;gBACjE,IAAI,GAAG,CAAC,KAAK,GAAG,EAAE;gBAClB,OAAO;YACT;YACA,SAAS;QACX;IACF;IAEA,SAAS,WAAW,KAAoB,EAAE,GAAe,EAAE,MAAiD,EAAE,WAAkC;QAC9I,IAAI,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG,CAAC;QAChD,IAAI,QAAQ,MACV,OA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AAI,eAAe,aAAa,IAAI,CAAC,CAAC,GAAG,IAAM,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,IAAI;IACvG,8CAA8C;IAC9C,IAAI,CAAC,YAAY,SAAS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YAAC,OAAO;mBACV,SAAS,KAAK,CAAC,GAAG;mBAClB;mBACA,SAAS,KAAK,CAAC;aACnB;YAAE,SAAS;QAAM;IACpB;IAEA,uDAAuD;IACvD,OAAO,WAAW,UAAU,SAAS,GAAG,EAAE,CAAA,aAAe,CAAA;YACvD,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GAAI;AACN","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 | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\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> | undefined,\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 * 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 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\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[],\n nodeMap: Map<Key, TreeNode<T>>\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<TreeDataState<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 ?? null,\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 | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\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 && 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 && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\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 // newNode cannot be null here due to the above filter.\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 if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\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 ?? null, 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 ?? null, 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 addNode(movedNode, newMap);\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 moveBefore(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\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\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n if (child.parentKey === toParent?.key\n && keyArray.includes(child.key)\n && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap); \n });\n return {items: [\n ...newItems.slice(0, toIndex),\n ...inOrderItems,\n ...newItems.slice(toIndex)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParent.key, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]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AG,IAAK,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,IAAI;IACvG,8CAA8C;IAC9C,IAAI,CAAC,YAAY,SAAS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YAAC,OAAO;mBACV,SAAS,KAAK,CAAC,GAAG;mBAClB;mBACA,SAAS,KAAK,CAAC;aACnB;YAAE,SAAS;QAAM;IACpB;IAEA,uDAAuD;IACvD,OAAO,WAAW,UAAU,SAAS,GAAG,EAAE,CAAA,aAAe,CAAA;YACvD,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GAAI;AACN","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 | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\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> | undefined,\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 * 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 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\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[],\n nodeMap: Map<Key, TreeNode<T>>\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<TreeDataState<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 ?? null,\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 | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\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 && 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 && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\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 // newNode cannot be null here due to the above filter.\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 if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\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 ?? null, 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 ?? null, 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 addNode(movedNode, newMap);\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 moveBefore(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\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\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it\n if ((child.parentKey === toParent\n || child.parentKey === toParent?.key)\n && keyArray.includes(child.key)\n && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap); \n });\n return {items: [\n ...newItems.slice(0, toIndex),\n ...inOrderItems,\n ...newItems.slice(toIndex)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParent.key, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }), newMap);\n}\n"],"names":[],"version":3,"file":"useTreeData.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/data",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"url": "https://github.com/adobe/react-spectrum"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@react-types/shared": "^3.
|
|
29
|
+
"@react-types/shared": "^3.32.0",
|
|
30
30
|
"@swc/helpers": "^0.5.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "2c58ed3ddd9be9100af9b1d0cfd137fcdc95ba2d"
|
|
39
39
|
}
|
package/src/useAsyncList.ts
CHANGED
|
@@ -339,7 +339,7 @@ export function useAsyncList<T, C = string>(options: AsyncListOptions<T, C>): As
|
|
|
339
339
|
},
|
|
340
340
|
loadMore() {
|
|
341
341
|
// Ignore if already loading more or if performing server side filtering.
|
|
342
|
-
if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {
|
|
342
|
+
if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {
|
|
343
343
|
return;
|
|
344
344
|
}
|
|
345
345
|
|
package/src/useListData.ts
CHANGED
|
@@ -36,6 +36,12 @@ export interface ListData<T> {
|
|
|
36
36
|
/** Sets the selected keys. */
|
|
37
37
|
setSelectedKeys(keys: Selection): void,
|
|
38
38
|
|
|
39
|
+
/** Adds the given keys to the current selected keys. */
|
|
40
|
+
addKeysToSelection(keys: Selection): void,
|
|
41
|
+
|
|
42
|
+
/** Removes the given keys from the current selected keys. */
|
|
43
|
+
removeKeysFromSelection(keys: Selection): void,
|
|
44
|
+
|
|
39
45
|
/** The current filter text. */
|
|
40
46
|
filterText: string,
|
|
41
47
|
|
|
@@ -175,6 +181,43 @@ export function createListActions<T, C>(opts: CreateListOptions<T, C>, dispatch:
|
|
|
175
181
|
selectedKeys
|
|
176
182
|
}));
|
|
177
183
|
},
|
|
184
|
+
addKeysToSelection(selectedKeys: Selection) {
|
|
185
|
+
dispatch(state => {
|
|
186
|
+
if (state.selectedKeys === 'all') {
|
|
187
|
+
return state;
|
|
188
|
+
}
|
|
189
|
+
if (selectedKeys === 'all') {
|
|
190
|
+
return {
|
|
191
|
+
...state,
|
|
192
|
+
selectedKeys: 'all'
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
...state,
|
|
198
|
+
selectedKeys: new Set([...state.selectedKeys, ...selectedKeys])
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
removeKeysFromSelection(selectedKeys: Selection) {
|
|
203
|
+
dispatch(state => {
|
|
204
|
+
if (selectedKeys === 'all') {
|
|
205
|
+
return {
|
|
206
|
+
...state,
|
|
207
|
+
selectedKeys: new Set()
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
let selection: Selection = state.selectedKeys === 'all' ? new Set(state.items.map(getKey!)) : new Set(state.selectedKeys);
|
|
212
|
+
for (let key of selectedKeys) {
|
|
213
|
+
selection.delete(key);
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
...state,
|
|
217
|
+
selectedKeys: selection
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
},
|
|
178
221
|
setFilterText(filterText: string) {
|
|
179
222
|
dispatch(state => ({
|
|
180
223
|
...state,
|
package/src/useTreeData.ts
CHANGED
|
@@ -502,7 +502,9 @@ function moveItems<T extends object>(
|
|
|
502
502
|
newMap = nextMap;
|
|
503
503
|
}
|
|
504
504
|
// decrement the index if the child being removed is in the target parent and before the target index
|
|
505
|
-
|
|
505
|
+
// the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
|
|
506
|
+
if ((child.parentKey === toParent
|
|
507
|
+
|| child.parentKey === toParent?.key)
|
|
506
508
|
&& keyArray.includes(child.key)
|
|
507
509
|
&& (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {
|
|
508
510
|
toIndex--;
|