Skip to content

Commit 812a763

Browse files
committed
fix: prevent crash in createSortedRowModel for unknown sorting column ids
sorting.filter() force-cast table.getColumn(sort.id) to Column_Internal even when it returned undefined (e.g. a column removed while its sort state persisted), so column_getCanSort threw when dereferencing column.columnDef. Guard the lookup and treat unknown columns as unsortable instead of crashing.
1 parent 504f730 commit 812a763

2 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎packages/table-core/src/features/row-sorting/createSortedRowModel.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ function _createSortedRowModel<
5151
const sortedFlatRows: Array<Row<TFeatures, TData>> = []
5252

5353
// Filter out sortings that correspond to non existing columns
54-
const availableSorting = sorting.filter((sort) =>
55-
column_getCanSort(
56-
table.getColumn(sort.id) as Column_Internal<TFeatures, TData>,
57-
),
58-
)
54+
const availableSorting = sorting.filter((sort) => {
55+
const column = table.getColumn(sort.id)
56+
return column ? column_getCanSort(column) : false
57+
})
5958

6059
const resolvedSorting: Array<{
6160
id: string
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, expect, it } from 'vitest'
2+
import {
3+
constructTable,
4+
coreFeatures,
5+
createSortedRowModel,
6+
rowSortingFeature,
7+
tableFeatures,
8+
} from '../../../../src'
9+
import { storeReactivityBindings } from '../../../../src/store-reactivity-bindings'
10+
import type { ColumnDef } from '../../../../src'
11+
12+
type Person = {
13+
firstName: string
14+
age: number
15+
}
16+
17+
const features = tableFeatures({
18+
...coreFeatures,
19+
rowSortingFeature,
20+
sortedRowModel: createSortedRowModel(),
21+
})
22+
23+
const columns: Array<ColumnDef<typeof features, Person, any>> = [
24+
{ accessorKey: 'firstName', id: 'firstName' },
25+
{ accessorKey: 'age', id: 'age' },
26+
]
27+
28+
const data: Array<Person> = [
29+
{ firstName: 'amy', age: 20 },
30+
{ firstName: 'bob', age: 40 },
31+
{ firstName: 'alice', age: 30 },
32+
]
33+
34+
function makeTable(sorting: Array<{ id: string; desc: boolean }>) {
35+
return constructTable<typeof features, Person>({
36+
data,
37+
columns,
38+
features: {
39+
...features,
40+
coreReactivityFeature: storeReactivityBindings(),
41+
},
42+
initialState: { sorting },
43+
})
44+
}
45+
46+
describe('createSortedRowModel', () => {
47+
it('does not crash when the sorting state references a column that no longer exists', () => {
48+
const table = makeTable([{ id: 'thisColumnDoesNotExist', desc: false }])
49+
50+
expect(() => table.getSortedRowModel()).not.toThrow()
51+
})
52+
53+
it('falls back to the pre-sorted row order when only unknown columns are sorted', () => {
54+
const table = makeTable([{ id: 'thisColumnDoesNotExist', desc: false }])
55+
56+
expect(
57+
table.getSortedRowModel().rows.map((row) => row.original.firstName),
58+
).toEqual(['amy', 'bob', 'alice'])
59+
})
60+
61+
it('still sorts by the remaining known columns when one sort entry is unknown', () => {
62+
const table = makeTable([
63+
{ id: 'thisColumnDoesNotExist', desc: false },
64+
{ id: 'age', desc: false },
65+
])
66+
67+
expect(
68+
table.getSortedRowModel().rows.map((row) => row.original.firstName),
69+
).toEqual(['amy', 'alice', 'bob'])
70+
})
71+
})

0 commit comments

Comments
 (0)