Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autoResetSorting #5414

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/api/features/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Returns whether this column is sorted.
### `getFirstSortDir`
```tsx
```tsx
getFirstSortDir: () => SortDirection
```
Expand Down Expand Up @@ -312,6 +312,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
Expand Down
31 changes: 31 additions & 0 deletions packages/table-core/src/features/RowSorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -246,6 +252,7 @@ export interface SortingOptions<TData extends RowData>
ResolvedSortingFns {}

export interface SortingInstance<TData extends RowData> {
_autoResetSorting: () => void
_getSortedRowModel?: () => RowModel<TData>
/**
* Returns the row model for the table before any sorting has been applied.
Expand Down Expand Up @@ -522,6 +529,30 @@ export const RowSorting: TableFeature = {
},

createTable: <TData extends RowData>(table: Table<TData>): 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 ?? [])
Expand Down
5 changes: 3 additions & 2 deletions packages/table-core/src/utils/getSortedRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ export function getSortedRowModel<TData extends RowData>(): (
rowsById: rowModel.rowsById,
}
},
getMemoOptions(table.options, 'debugTable', 'getSortedRowModel', () =>
getMemoOptions(table.options, 'debugTable', 'getSortedRowModel', () => {
table._autoResetSorting()
table._autoResetPageIndex()
)
})
)
}