diff --git a/docs/api/features/sorting.md b/docs/api/features/sorting.md index e5b60eea50..29b2f8df76 100644 --- a/docs/api/features/sorting.md +++ b/docs/api/features/sorting.md @@ -207,7 +207,7 @@ Returns whether this column is sorted. ### `getFirstSortDir` -```tsx +```tsx getFirstSortDir: () => SortDirection ``` @@ -318,6 +318,14 @@ enableMultiSort?: boolean Enables/Disables multi-sorting for the table. +### `autoResetSorting` + +```tsx +autoResetSorting?: boolean +``` + +Enable this setting to automatically reset the sorting state of the table when sorting state changes. + ### `sortDescFirst` ```tsx diff --git a/packages/table-core/src/features/RowSorting.ts b/packages/table-core/src/features/RowSorting.ts index c2e7c32d53..65002a3ddf 100644 --- a/packages/table-core/src/features/RowSorting.ts +++ b/packages/table-core/src/features/RowSorting.ts @@ -195,6 +195,12 @@ interface SortingOptionsBase { * @link [Guide](https://tanstack.com/table/v8/docs/guide/sorting) */ enableSortingRemoval?: boolean + /** + * Enable this setting to automatically reset the sorting state of the table when sorting state changes. + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/sorting#autoresetsorting) + * @link [Guide](https://tanstack.com/table/v8/docs/guide/sorting) + */ + autoResetSorting?: boolean /** * This function is used to retrieve the sorted row model. If using server-side sorting, this function is not required. To use client-side sorting, pass the exported `getSortedRowModel()` from your adapter to your table or implement your own. * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/sorting#getsortedrowmodel) @@ -246,6 +252,7 @@ export interface SortingOptions ResolvedSortingFns {} export interface SortingInstance { + _autoResetSorting: () => void _getSortedRowModel?: () => RowModel /** * Returns the row model for the table before any sorting has been applied. @@ -522,6 +529,30 @@ export const RowSorting: TableFeature = { }, createTable: (table: Table): void => { + let registered = false + let queued = false + + table._autoResetSorting = () => { + if (!registered) { + table._queue(() => { + registered = true + }) + return + } + + if ( + table.options.autoResetAll ?? + table.options.autoResetSorting ?? + !table.options.manualSorting + ) { + if (queued) return + queued = true + table._queue(() => { + table.resetSorting() + queued = false + }) + } + } table.setSorting = updater => table.options.onSortingChange?.(updater) table.resetSorting = defaultState => { table.setSorting(defaultState ? [] : table.initialState?.sorting ?? []) diff --git a/packages/table-core/src/utils/getSortedRowModel.ts b/packages/table-core/src/utils/getSortedRowModel.ts index 928c26448f..e5508b29f2 100644 --- a/packages/table-core/src/utils/getSortedRowModel.ts +++ b/packages/table-core/src/utils/getSortedRowModel.ts @@ -114,8 +114,9 @@ export function getSortedRowModel(): ( rowsById: rowModel.rowsById, } }, - getMemoOptions(table.options, 'debugTable', 'getSortedRowModel', () => + getMemoOptions(table.options, 'debugTable', 'getSortedRowModel', () => { + table._autoResetSorting() table._autoResetPageIndex() - ) + }) ) }