MCPcopy Index your code
hub / github.com/adobe/react-spectrum / useListData

Function useListData

packages/react-stately/src/data/useListData.ts:157–186  ·  view source on GitHub ↗
(options: ListOptions<T>)

Source from the content-addressed store, hash-verified

155 * update the data over time.
156 */
157export function useListData<T>(options: ListOptions<T>): ListData<T> {
158 let {
159 initialItems = [],
160 initialSelectedKeys,
161 getKey = (item: any) => item.id ?? item.key,
162 filter,
163 initialFilterText = ''
164 } = options;
165
166 // Store both items and filteredItems in state so we can go back to the unfiltered list
167 let [state, setState] = useState<ListState<T>>({
168 items: initialItems,
169 selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys || []),
170 filterText: initialFilterText
171 });
172
173 let filteredItems = useMemo(
174 () => (filter ? state.items.filter(item => filter(item, state.filterText)) : state.items),
175 [state.items, state.filterText, filter]
176 );
177
178 return {
179 ...state,
180 items: filteredItems,
181 ...createListActions({getKey}, setState),
182 getItem(key: Key) {
183 return state.items.find(item => getKey(item) === key);
184 }
185 };
186}
187
188export function createListActions<T, C>(
189 opts: CreateListOptions<T, C>,

Callers 15

DroppableListBoxExampleFunction · 0.90
ReorderableGridExampleFunction · 0.90
DroppableGridExampleFunction · 0.90
TagGroup.test.jsFile · 0.90
ReorderableTableFunction · 0.90
TableExampleFunction · 0.90
FixedColumnWidthsFunction · 0.90
DndTableRenderFunction · 0.90

Calls 3

createListActionsFunction · 0.85
filterMethod · 0.65
filterFunction · 0.50

Tested by 4

ControlledItemsComboBoxFunction · 0.72
ExampleFunction · 0.72
EditableTableFunction · 0.72
ActionEditableTableFunction · 0.72