@react-stately/data 3.14.1 → 3.15.1

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 CHANGED
@@ -90,9 +90,9 @@ export interface ListData<T> {
90
90
  /**
91
91
  * Updates an item in the list.
92
92
  * @param key - The key of the item to update.
93
- * @param newValue - The new value for the item.
93
+ * @param newValue - The new value for the item, or a function that returns the new value based on the previous value.
94
94
  */
95
- update(key: Key, newValue: T): void;
95
+ update(key: Key, newValue: T | ((prev: T) => T)): void;
96
96
  }
97
97
  /**
98
98
  * Manages state for an immutable list data structure, and provides convenience methods to
@@ -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;IAC9C,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,+DAA+D;IAC/D,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,GAAG,CAAC;IAC1B,8EAA8E;IAC9E,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAA;CAClD;AAED,0BAA0B,CAAC;IACzB,6BAA6B;IAC7B,KAAK,EAAE,CAAC,EAAE,CAAC;IAEX,4DAA4D;IAC5D,YAAY,EAAE,SAAS,CAAC;IAExB,8BAA8B;IAC9B,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;IAEvC,wDAAwD;IACxD,kBAAkB,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;IAE1C,6DAA6D;IAC7D,uBAAuB,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;IAE/C,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAC;IAEnB,4BAA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AC;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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AC,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"}
@@ -90,7 +90,7 @@ function $1cb48366e5c5533f$var$reducer(data, action) {
90
90
  var // We're already loading, and another load was triggered at the same time.
91
91
  // We need to abort the previous load and start a new one.
92
92
  _data_abortController;
93
- (_data_abortController = data.abortController) === null || _data_abortController === void 0 ? void 0 : _data_abortController.abort();
93
+ (_data_abortController = data.abortController) === null || _data_abortController === void 0 ? void 0 : _data_abortController.abort('aborting current load and starting new one');
94
94
  var _action_filterText2;
95
95
  return {
96
96
  ...data,
@@ -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;gBACL,KAAK;oBACH,OAAO;gBACT;oBACE,MAAM,IAAI,MAAM,CAAC,gBAAgB,EAAE,OAAO,IAAI,CAAC,YAAY,EAAE,KAAK,KAAK,CAAC,CAAC,CAAC;YAC9E;QACF,KAAK;QACL,KAAK;QACL,KAAK;YACH,OAAQ,OAAO,IAAI;gBACjB,KAAK;oBACH,uDAAuD;oBACvD,wDAAwD;oBACxD,0CAA0C;oBAC1C,IAAI,OAAO,eAAe,KAAK,KAAK,eAAe,EACjD,OAAO;wBAGM;oBAAf,eAAe,CAAA,uBAAA,OAAO,YAAY,cAAnB,kCAAA,uBAAuB,KAAK,YAAY;wBAGzC,qBAEA,eAEI;oBANlB,OAAO;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M;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":[],"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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('aborting current load and starting new one');\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"}
@@ -84,7 +84,7 @@ function $f86e6c1ec7da6ebb$var$reducer(data, action) {
84
84
  var // We're already loading, and another load was triggered at the same time.
85
85
  // We need to abort the previous load and start a new one.
86
86
  _data_abortController;
87
- (_data_abortController = data.abortController) === null || _data_abortController === void 0 ? void 0 : _data_abortController.abort();
87
+ (_data_abortController = data.abortController) === null || _data_abortController === void 0 ? void 0 : _data_abortController.abort('aborting current load and starting new one');
88
88
  var _action_filterText2;
89
89
  return {
90
90
  ...data,
@@ -84,7 +84,7 @@ function $f86e6c1ec7da6ebb$var$reducer(data, action) {
84
84
  var // We're already loading, and another load was triggered at the same time.
85
85
  // We need to abort the previous load and start a new one.
86
86
  _data_abortController;
87
- (_data_abortController = data.abortController) === null || _data_abortController === void 0 ? void 0 : _data_abortController.abort();
87
+ (_data_abortController = data.abortController) === null || _data_abortController === void 0 ? void 0 : _data_abortController.abort('aborting current load and starting new one');
88
88
  var _action_filterText2;
89
89
  return {
90
90
  ...data,
@@ -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,K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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"}
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,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ACA;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('aborting current load and starting new one');\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"}
@@ -192,11 +192,14 @@ function $fbc1d750f520c94e$export$79c0c687a5963b0a(opts, dispatch) {
192
192
  dispatch((state)=>{
193
193
  let index = state.items.findIndex((item)=>getKey(item) === key);
194
194
  if (index === -1) return state;
195
+ let updatedValue;
196
+ if (typeof newValue === 'function') updatedValue = newValue(state.items[index]);
197
+ else updatedValue = newValue;
195
198
  return {
196
199
  ...state,
197
200
  items: [
198
201
  ...state.items.slice(0, index),
199
- newValue,
202
+ updatedValue,
200
203
  ...state.items.slice(index + 1)
201
204
  ]
202
205
  };
@@ -1 +1 @@
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,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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C,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.main.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,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,SAAS;QACxC,SAAQ,GAAQ;YACd,OAAO,MAAM,KAAK,CAAC,IAAI,CAAC,CAAA,OAAQ,OAAO,UAAU;QACnD;IACF;AACF;AAEO,SAAS,0CAAwB,IAA6B,EAAE,QAAkE;IACvI,IAAI,UAAC,MAAM,UAAE,MAAM,EAAC,GAAG;IACvB,OAAO;QACL,iBAAgB,YAAuB;YACrC,SAAS,CAAA,QAAU,CAAA;oBACjB,GAAG,KAAK;kCACR;gBACF,CAAA;QACF;QACA,oBAAmB,YAAuB;YACxC,SAAS,CAAA;gBACP,IAAI,MAAM,YAAY,KAAK,OACzB,OAAO;gBAET,IAAI,iBAAiB,OACnB,OAAO;oBACL,GAAG,KAAK;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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, or a function that returns the new value based on the previous value.\n */\n update(key: Key, newValue: T | ((prev: T) => 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 | ((prev: T) => T)) {\n dispatch(state => {\n let index = state.items.findIndex(item => getKey!(item) === key);\n if (index === -1) {\n return state;\n }\n\n let updatedValue: T;\n if (typeof newValue === 'function') {\n updatedValue = (newValue as (prev: T) => T)(state.items[index]);\n } else {\n updatedValue = newValue;\n }\n\n return {\n ...state,\n items: [\n ...state.items.slice(0, index),\n updatedValue,\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"}
@@ -185,11 +185,14 @@ function $0d86e9c8f07f9a7b$export$79c0c687a5963b0a(opts, dispatch) {
185
185
  dispatch((state)=>{
186
186
  let index = state.items.findIndex((item)=>getKey(item) === key);
187
187
  if (index === -1) return state;
188
+ let updatedValue;
189
+ if (typeof newValue === 'function') updatedValue = newValue(state.items[index]);
190
+ else updatedValue = newValue;
188
191
  return {
189
192
  ...state,
190
193
  items: [
191
194
  ...state.items.slice(0, index),
192
- newValue,
195
+ updatedValue,
193
196
  ...state.items.slice(index + 1)
194
197
  ]
195
198
  };
@@ -185,11 +185,14 @@ function $0d86e9c8f07f9a7b$export$79c0c687a5963b0a(opts, dispatch) {
185
185
  dispatch((state)=>{
186
186
  let index = state.items.findIndex((item)=>getKey(item) === key);
187
187
  if (index === -1) return state;
188
+ let updatedValue;
189
+ if (typeof newValue === 'function') updatedValue = newValue(state.items[index]);
190
+ else updatedValue = newValue;
188
191
  return {
189
192
  ...state,
190
193
  items: [
191
194
  ...state.items.slice(0, index),
192
- newValue,
195
+ updatedValue,
193
196
  ...state.items.slice(index + 1)
194
197
  ]
195
198
  };
@@ -1 +1 @@
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,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,SAAS;QACxC,SAAQ,GAAQ;YACd,OAAO,MAAM,KAAK,CAAC,IAAI,CAAC,CAAA,OAAQ,OAAO,UAAU;QACnD;IACF;AACF;AAEO,SAAS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,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"}
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,GAA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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 /** 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, or a function that returns the new value based on the previous value.\n */\n update(key: Key, newValue: T | ((prev: T) => 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 | ((prev: T) => T)) {\n dispatch(state => {\n let index = state.items.findIndex(item => getKey!(item) === key);\n if (index === -1) {\n return state;\n }\n\n let updatedValue: T;\n if (typeof newValue === 'function') {\n updatedValue = (newValue as (prev: T) => T)(state.items[index]);\n } else {\n updatedValue = newValue;\n }\n\n return {\n ...state,\n items: [\n ...state.items.slice(0, index),\n updatedValue,\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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/data",
3
- "version": "3.14.1",
3
+ "version": "3.15.1",
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.32.1",
29
+ "@react-types/shared": "^3.33.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": "0bda51183baa23306342af32a82012ea0fe0f2dc"
38
+ "gitHead": "66e51757606b43a89ed02c574ca24517323a2ab9"
39
39
  }
@@ -181,7 +181,7 @@ function reducer<T, C>(data: AsyncListState<T, C>, action: Action<T, C>): AsyncL
181
181
  case 'filtering':
182
182
  // We're already loading, and another load was triggered at the same time.
183
183
  // We need to abort the previous load and start a new one.
184
- data.abortController?.abort();
184
+ data.abortController?.abort('aborting current load and starting new one');
185
185
  return {
186
186
  ...data,
187
187
  filterText: action.filterText ?? data.filterText,
@@ -123,9 +123,9 @@ export interface ListData<T> {
123
123
  /**
124
124
  * Updates an item in the list.
125
125
  * @param key - The key of the item to update.
126
- * @param newValue - The new value for the item.
126
+ * @param newValue - The new value for the item, or a function that returns the new value based on the previous value.
127
127
  */
128
- update(key: Key, newValue: T): void
128
+ update(key: Key, newValue: T | ((prev: T) => T)): void
129
129
  }
130
130
 
131
131
  export interface ListState<T> {
@@ -344,18 +344,25 @@ export function createListActions<T, C>(opts: CreateListOptions<T, C>, dispatch:
344
344
  return move(state, indices, toIndex + 1);
345
345
  });
346
346
  },
347
- update(key: Key, newValue: T) {
347
+ update(key: Key, newValue: T | ((prev: T) => T)) {
348
348
  dispatch(state => {
349
349
  let index = state.items.findIndex(item => getKey!(item) === key);
350
350
  if (index === -1) {
351
351
  return state;
352
352
  }
353
353
 
354
+ let updatedValue: T;
355
+ if (typeof newValue === 'function') {
356
+ updatedValue = (newValue as (prev: T) => T)(state.items[index]);
357
+ } else {
358
+ updatedValue = newValue;
359
+ }
360
+
354
361
  return {
355
362
  ...state,
356
363
  items: [
357
364
  ...state.items.slice(0, index),
358
- newValue,
365
+ updatedValue,
359
366
  ...state.items.slice(index + 1)
360
367
  ]
361
368
  };