Skip to content

Commit

Permalink
feat: add autoResetSorting
Browse files Browse the repository at this point in the history
  • Loading branch information
tychenjiajun committed Mar 16, 2024
1 parent 01129cb commit 86a854c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
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()
)
})
)
}

0 comments on commit 86a854c

Please sign in to comment.