MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useTableState

Function useTableState

packages/react-stately/src/table/useTableState.ts:123–189  ·  view source on GitHub ↗
(props: TableStateProps<T>)

Source from the content-addressed store, hash-verified

121 * rows from props. In addition, it tracks row selection and manages sort order changes.
122 */
123export function useTableState<T extends object>(props: TableStateProps<T>): TableState<T> {
124 let [isKeyboardNavigationDisabled, setKeyboardNavigationDisabled] = useState(false);
125 let {selectionMode = 'none', showSelectionCheckboxes, showDragButtons, treeColumn = null} = props;
126
127 let context = useMemo(
128 () => ({
129 showSelectionCheckboxes: showSelectionCheckboxes && selectionMode !== 'none',
130 showDragButtons: showDragButtons,
131 selectionMode,
132 columns: []
133 }),
134 // eslint-disable-next-line react-hooks/exhaustive-deps
135 // oxlint-disable-next-line react/react-compiler, react-hooks/exhaustive-deps
136 [props.children, showSelectionCheckboxes, selectionMode, showDragButtons]
137 );
138
139 let collection = useCollection<T, ITableCollection<T>>(
140 props,
141 useCallback(nodes => new TableCollection(nodes, null, context), [context]),
142 context
143 );
144 let {disabledKeys, selectionManager} = useGridState({
145 ...props,
146 collection,
147 disabledBehavior: props.disabledBehavior || 'selection'
148 });
149
150 let [expandedKeys, setExpandedKeys] = useControlledState(
151 props.expandedKeys ? new Set(props.expandedKeys) : undefined,
152 props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),
153 props.onExpandedChange
154 );
155
156 return {
157 collection,
158 disabledKeys,
159 selectionManager,
160 showSelectionCheckboxes: props.showSelectionCheckboxes || false,
161 sortDescriptor: props.sortDescriptor ?? null,
162 isKeyboardNavigationDisabled: collection.size === 0 || isKeyboardNavigationDisabled,
163 setKeyboardNavigationDisabled,
164 sort(columnKey: Key, direction?: 'ascending' | 'descending') {
165 props.onSortChange?.({
166 column: columnKey,
167 direction:
168 direction ??
169 (props.sortDescriptor?.column === columnKey
170 ? OPPOSITE_SORT_DIRECTION[props.sortDescriptor.direction]
171 : 'ascending')
172 });
173 },
174 expandedKeys,
175 toggleKey(key) {
176 setExpandedKeys(keys => {
177 let newKeys = new Set(keys);
178 if (newKeys.has(key)) {
179 newKeys.delete(key);
180 } else {

Callers 10

TableFunction · 0.90
TableFunction · 0.90
TableFunction · 0.90
TableFunction · 0.90
TableFunction · 0.90
TableFunction · 0.90
TableInnerFunction · 0.90
TableFunction · 0.90

Calls 3

useCollectionFunction · 0.90
useGridStateFunction · 0.90
useControlledStateFunction · 0.90

Tested by 2

TableFunction · 0.72
TableFunction · 0.72