Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -207,7 +207,7 @@ Returns whether this column is sorted.

### `getFirstSortDir`

```tsx
```tsx
getFirstSortDir: () => SortDirection
```

Expand Down Expand Up @@ -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.

Comment on lines +321 to +328
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix the misleading description to accurately describe when auto-reset occurs.

The description states "when sorting state changes," which is circular and inaccurate. The auto-reset is triggered when the underlying data changes (when getPreSortedRowModel() returns a new value), not when the sorting state itself changes.

Apply this diff to fix the documentation:

-Enable this setting to automatically reset the sorting state of the table when sorting state changes.
+Enable this setting to automatically reset the sorting state of the table when the underlying data changes.

Based on static analysis hints.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
### `autoResetSorting`
```tsx
autoResetSorting?: boolean
```
Enable this setting to automatically reset the sorting state of the table when sorting state changes.
### `autoResetSorting`
🧰 Tools
🪛 LanguageTool

[grammar] ~327-~327: There might be a mistake here.
Context: ...ly reset the sorting state of the table when sorting state changes. ### `sortDescFi...

(QB_NEW_EN)

🤖 Prompt for AI Agents
In docs/api/features/sorting.md around lines 321 to 328, the description for
`autoResetSorting` is misleading: it says it resets "when sorting state changes"
but actually triggers when the underlying data changes (i.e., when
getPreSortedRowModel() returns a new value). Update the wording to state that
enabling `autoResetSorting` will automatically reset the table's sorting state
when the underlying row data changes (pre-sorted row model changes), not when
the sorting state itself changes, and keep the prop signature and example
intact.

### `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
Comment on lines +198 to +203
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Clarify the documentation to accurately describe when auto-reset occurs.

The comment states "when sorting state changes," which is circular and misleading. Based on the implementation, this feature resets sorting when the underlying data changes (specifically when getPreSortedRowModel() returns a new value), not when the sorting state itself changes.

Apply this diff to clarify the documentation:

   /**
-   * Enable this setting to automatically reset the sorting state of the table when sorting state changes.
+   * Enable this setting to automatically reset the sorting state of the table when the underlying data changes.
    * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/sorting#autoresetsorting)
    * @link [Guide](https://tanstack.com/table/v8/docs/guide/sorting)
    */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* 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
/**
* Enable this setting to automatically reset the sorting state of the table when the underlying data 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
🤖 Prompt for AI Agents
In packages/table-core/src/features/RowSorting.ts around lines 198 to 203, the
JSDoc for autoResetSorting is misleading because it says "when sorting state
changes" — update the comment to state that autoResetSorting controls whether
sorting is automatically reset when the underlying row data changes
(specifically when getPreSortedRowModel() returns a new value), not when the
sorting state itself changes; keep the existing API Docs and Guide links and
wording about enabling the setting but replace the phrase to reference
data/pre-sorted row model changes.

/**
* 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
})
}
}
Comment on lines +532 to +555
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

The default behavior contradicts the requirement that auto-reset should not be enabled by default.

The condition on lines 543-546 evaluates to true by default when neither autoResetAll nor autoResetSorting is set and manualSorting is not enabled. This means the feature is enabled by default, which contradicts the maintainer's explicit requirement in the PR comments that "the feature should not be enabled by default, similar to other autoReset options."

Apply this diff to disable the feature by default:

       if (
         table.options.autoResetAll ??
-        table.options.autoResetSorting ??
-        !table.options.manualSorting
+        table.options.autoResetSorting
       ) {

This change ensures that auto-reset only occurs when:

  1. autoResetAll is explicitly set to true, OR
  2. autoResetSorting is explicitly set to true

Without this change, existing applications not using manualSorting will experience unexpected sorting resets when data changes, which could be a breaking change.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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
})
}
}
let registered = false
let queued = false
table._autoResetSorting = () => {
if (!registered) {
table._queue(() => {
registered = true
})
return
}
if (
table.options.autoResetAll ??
table.options.autoResetSorting
) {
if (queued) return
queued = true
table._queue(() => {
table.resetSorting()
queued = false
})
}
}
🤖 Prompt for AI Agents
In packages/table-core/src/features/RowSorting.ts around lines 532 to 555, the
current condition enables auto-reset by default when neither autoResetAll nor
autoResetSorting is set and manualSorting is false; change the conditional so
auto-reset only runs when autoResetAll or autoResetSorting are explicitly true
(e.g. replace the truthy-coalescing check with an explicit equality check:
table.options.autoResetAll === true || table.options.autoResetSorting === true),
leaving the registered/queued logic and table._queue call intact.

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 @@ -114,8 +114,9 @@ export function getSortedRowModel<TData extends RowData>(): (
rowsById: rowModel.rowsById,
}
},
getMemoOptions(table.options, 'debugTable', 'getSortedRowModel', () =>
getMemoOptions(table.options, 'debugTable', 'getSortedRowModel', () => {
table._autoResetSorting()
table._autoResetPageIndex()
)
})
)
}
Loading