Skip to content

Commit f3c37dd

Browse files
committed
move rowModelFns table option to create row models
1 parent 9e8029f commit f3c37dd

File tree

93 files changed

+510
-589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+510
-589
lines changed

docs/api/features/sorting.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,35 @@ Every sorting function receives 2 rows and a column ID and are expected to compa
5050
This is the type signature for every sorting function:
5151
5252
```tsx
53-
export type SortingFn<TData extends AnyData> = {
53+
export type SortFn<TData extends AnyData> = {
5454
(rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string): number
5555
}
5656
```
5757
5858
#### Using Sorting Functions
5959
60-
Sorting functions can be used/referenced/defined by passing the following to `columnDefinition.sortingFn`:
60+
Sorting functions can be used/referenced/defined by passing the following to `columnDefinition.sortFn`:
6161
6262
- A `string` that references a built-in sorting function
6363
- A `string` that references a custom sorting functions provided via the `tableOptions.sortFns` option
64-
- A function directly provided to the `columnDefinition.sortingFn` option
64+
- A function directly provided to the `columnDefinition.sortFn` option
6565
66-
The final list of sorting functions available for the `columnDef.sortingFn` use the following type:
66+
The final list of sorting functions available for the `columnDef.sortFn` use the following type:
6767
6868
```tsx
69-
export type SortingFnOption<TData extends AnyData> =
69+
export type SortFnOption<TData extends AnyData> =
7070
| 'auto'
7171
| SortFns
7272
| BuiltInSortFns
73-
| SortingFn<TFeatures, TData>
73+
| SortFn<TFeatures, TData>
7474
```
7575
7676
## Column Def Options
7777
78-
### `sortingFn`
78+
### `sortFn`
7979
8080
```tsx
81-
sortingFn?: SortingFn | keyof SortFns | keyof BuiltInSortFns
81+
sortFn?: SortFn | keyof SortFns | keyof BuiltInSortFns
8282
```
8383
8484
The sorting function to use with this column.
@@ -141,10 +141,10 @@ sortUndefined?: 'first' | 'last' | false | -1 | 1 // defaults to 1
141141
142142
## Column API
143143
144-
### `getAutoSortingFn`
144+
### `getAutoSortFn`
145145
146146
```tsx
147-
getAutoSortingFn: () => SortingFn<TFeatures, TData>
147+
getAutoSortFn: () => SortFn<TFeatures, TData>
148148
```
149149
150150
Returns a sorting function automatically inferred based on the columns values.
@@ -157,10 +157,10 @@ getAutoSortDir: () => SortDirection
157157
158158
Returns a sort direction automatically inferred based on the columns values.
159159
160-
### `getSortingFn`
160+
### `getSortFn`
161161
162162
```tsx
163-
getSortingFn: () => SortingFn<TFeatures, TData>
163+
getSortFn: () => SortFn<TFeatures, TData>
164164
```
165165
166166
Returns the resolved sorting function to be used for this column
@@ -242,21 +242,21 @@ Returns a function that can be used to toggle this column's sorting state. This
242242
### `sortFns`
243243
244244
```tsx
245-
sortFns?: Record<string, SortingFn>
245+
sortFns?: Record<string, SortFn>
246246
```
247247
248-
This option allows you to define custom sorting functions that can be referenced in a column's `sortingFn` option by their key.
248+
This option allows you to define custom sorting functions that can be referenced in a column's `sortFn` option by their key.
249249
Example:
250250
251251
```tsx
252252
declare module '@tanstack/table-core' {
253253
interface SortFns {
254-
myCustomSorting: SortingFn<unknown>
254+
myCustomSorting: SortFn<unknown>
255255
}
256256
}
257257

258258
const column = columnHelper.data('key', {
259-
sortingFn: 'myCustomSorting',
259+
sortFn: 'myCustomSorting',
260260
})
261261

262262
const table = useTable({

docs/guide/fuzzy-filtering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ When using fuzzy filtering with column filtering, you might also want to sort th
9292
import { compareItems } from '@tanstack/match-sorter-utils'
9393
import { sortFns } from '@tanstack/table'
9494

95-
const fuzzySort: SortingFn<any> = (rowA, rowB, columnId) => {
95+
const fuzzySort: SortFn<any> = (rowA, rowB, columnId) => {
9696
let dir = 0
9797

9898
// Only sort by rank if the column has ranking information

docs/guide/sorting.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const table = useTable({
134134

135135
The default sorting function for all columns is inferred from the data type of the column. However, it can be useful to define the exact sorting function that you want to use for a specific column, especially if any of your data is nullable or not a standard data type.
136136

137-
You can determine a custom sorting function on a per-column basis using the `sortingFn` column option.
137+
You can determine a custom sorting function on a per-column basis using the `sortFn` column option.
138138

139139
By default, there are 6 built-in sorting functions to choose from:
140140

@@ -145,20 +145,20 @@ By default, there are 6 built-in sorting functions to choose from:
145145
- `datetime` - Sorts by time, use this if your values are `Date` objects.
146146
- `basic` - Sorts using a basic/standard `a > b ? 1 : a < b ? -1 : 0` comparison. This is the fastest sorting function, but may not be the most accurate.
147147

148-
You can also define your own custom sorting functions either as the `sortingFn` column option, or as a global sorting function using the `sortFns` table option.
148+
You can also define your own custom sorting functions either as the `sortFn` column option, or as a global sorting function using the `sortFns` table option.
149149

150150
#### Custom Sorting Functions
151151

152-
When defining a custom sorting function in either the `sortFns` table option or as a `sortingFn` column option, it should have the following signature:
152+
When defining a custom sorting function in either the `sortFns` table option or as a `sortFn` column option, it should have the following signature:
153153

154154
```tsx
155-
//optionally use the SortingFn to infer the parameter types
156-
const myCustomSortingFn: SortingFn<TFeatures, TData> = (rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string) => {
155+
//optionally use the SortFn to infer the parameter types
156+
const myCustomSortFn: SortFn<TFeatures, TData> = (rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string) => {
157157
return //-1, 0, or 1 - access any row data using rowA.original and rowB.original
158158
}
159159
```
160160

161-
> Note: The comparison function does not need to take whether or not the column is in descending or ascending order into account. The row models will take of that logic. `sortingFn` functions only need to provide a consistent comparison.
161+
> Note: The comparison function does not need to take whether or not the column is in descending or ascending order into account. The row models will take of that logic. `sortFn` functions only need to provide a consistent comparison.
162162
163163
Every sorting function receives 2 rows and a column ID and are expected to compare the two rows using the column ID to return `-1`, `0`, or `1` in ascending order. Here's a cheat sheet:
164164

@@ -173,23 +173,23 @@ const columns = [
173173
{
174174
header: () => 'Name',
175175
accessorKey: 'name',
176-
sortingFn: 'alphanumeric', // use built-in sorting function by name
176+
sortFn: 'alphanumeric', // use built-in sorting function by name
177177
},
178178
{
179179
header: () => 'Age',
180180
accessorKey: 'age',
181-
sortingFn: 'myCustomSortingFn', // use custom global sorting function
181+
sortFn: 'myCustomSortFn', // use custom global sorting function
182182
},
183183
{
184184
header: () => 'Birthday',
185185
accessorKey: 'birthday',
186-
sortingFn: 'datetime', // recommended for date columns
186+
sortFn: 'datetime', // recommended for date columns
187187
},
188188
{
189189
header: () => 'Profile',
190190
accessorKey: 'profile',
191191
// use custom sorting function directly
192-
sortingFn: (rowA, rowB, columnId) => {
192+
sortFn: (rowA, rowB, columnId) => {
193193
return rowA.original.someProperty - rowB.original.someProperty
194194
},
195195
}
@@ -201,7 +201,7 @@ const table = useTable({
201201
getCoreRowModel: createCoreRowModel(),
202202
getSortedRowModel: createSortedRowModel(),
203203
sortFns: { //add a custom sorting function
204-
myCustomSortingFn: (rowA, rowB, columnId) => {
204+
myCustomSortFn: (rowA, rowB, columnId) => {
205205
return rowA.original[columnId] > rowB.original[columnId] ? 1 : rowA.original[columnId] < rowB.original[columnId] ? -1 : 0
206206
},
207207
},
@@ -408,8 +408,8 @@ There are a lot of sorting related APIs that you can use to hook up to your UI o
408408
- `column.getNextSortingOrder` - Useful for showing which direction the column will sort by next. (asc/desc/clear in a tooltip/menu item/aria-label or something)
409409
- `column.getFirstSortDir` - Useful for showing which direction the column will sort by first. (asc/desc in a tooltip/menu item/aria-label or something)
410410
- `column.getAutoSortDir` - Determines whether the first sorting direction will be ascending or descending for a column.
411-
- `column.getAutoSortingFn` - Used internally to find the default sorting function for a column if none is specified.
412-
- `column.getSortingFn` - Returns the exact sorting function being used for a column.
411+
- `column.getAutoSortFn` - Used internally to find the default sorting function for a column if none is specified.
412+
- `column.getSortFn` - Returns the exact sorting function being used for a column.
413413

414414
- `column.getCanMultiSort` - Useful for enabling/disabling the multi-sorting UI for a column.
415415
- `column.getSortIndex` - Useful for showing a badge or indicator of the column's sort order in a multi-sort scenario. i.e. whether or not it is the first, second, third, etc. column to be sorted.

examples/lit/sorting/src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
flexRender,
1010
} from '@tanstack/lit-table'
1111
import { Person, makeData } from './makeData'
12-
import type { ColumnDef, SortingFn, SortingState } from '@tanstack/lit-table'
12+
import type { ColumnDef, SortFn, SortingState } from '@tanstack/lit-table'
1313

14-
const sortStatusFn: SortingFn<any, Person> = (rowA, rowB, _columnId) => {
14+
const sortStatusFn: SortFn<any, Person> = (rowA, rowB, _columnId) => {
1515
const statusA = rowA.original.status
1616
const statusB = rowB.original.status
1717
const statusOrder = ['single', 'complicated', 'relationship']
@@ -45,7 +45,7 @@ const columns: Array<ColumnDef<any, Person>> = [
4545
{
4646
accessorKey: 'status',
4747
header: 'Status',
48-
sortingFn: sortStatusFn, // use our custom sorting function for this enum column
48+
sortFn: sortStatusFn, // use our custom sorting function for this enum column
4949
},
5050
{
5151
accessorKey: 'progress',
@@ -60,7 +60,7 @@ const columns: Array<ColumnDef<any, Person>> = [
6060
{
6161
accessorKey: 'createdAt',
6262
header: 'Created At',
63-
// sortingFn: 'datetime' //make sure table knows this is a datetime column (usually can detect if no null values)
63+
// sortFn: 'datetime' //make sure table knows this is a datetime column (usually can detect if no null values)
6464
},
6565
]
6666

examples/qwik/filters/src/main.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
Column,
2020
ColumnFiltersState,
2121
FilterFn,
22-
SortingFn,
22+
SortFn,
2323
Table,
2424
} from '@tanstack/qwik-table'
2525
import type { RankingInfo } from '@tanstack/match-sorter-utils'
@@ -46,7 +46,7 @@ const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
4646
return itemRank.passed
4747
}
4848

49-
const fuzzySort: SortingFn<any> = (rowA, rowB, columnId) => {
49+
const fuzzySort: SortFn<any> = (rowA, rowB, columnId) => {
5050
let dir = 0
5151

5252
// Only sort by rank if the column has ranking information
@@ -121,14 +121,14 @@ const columns = [
121121
cell: (info) => info.getValue(),
122122
header: () => <span>Last Name</span>,
123123
footer: (props) => props.column.id,
124-
sortingFn: fuzzySort,
124+
sortFn: fuzzySort,
125125
}),
126126
columnHelper.accessor((row) => `${row.firstName} ${row.lastName}`, {
127127
id: 'fullName',
128128
header: 'Full Name',
129129
cell: (info) => info.getValue(),
130130
footer: (props) => props.column.id,
131-
sortingFn: fuzzySort,
131+
sortFn: fuzzySort,
132132
}),
133133
],
134134
}),

examples/qwik/sorting/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const columns: Array<ColumnDef<any, Person>> = [
5757
accessorKey: 'createdAt',
5858
header: 'Created At',
5959
cell: (info) => info.getValue<Date>().toLocaleDateString(),
60-
// sortingFn: 'datetime' (inferred from the data)
60+
// sortFn: 'datetime' (inferred from the data)
6161
},
6262
]
6363

examples/react/basic-table-helper/src/main.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const defaultData: Array<Person> = [
5555
const tableHelper = createTableHelper({
5656
_features: {},
5757
_rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here
58-
_rowModelFns: {}, // client-side processing functions used by the row models (sorting, filtering, etc.). Not needed in this basic example
5958
debugTable: true,
6059
// TData: {} as Person, // optionally, set the TData type for the table helper. Omit if this will be a table helper for multiple tables of all different data types
6160
})

examples/react/custom-features/src/main.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,9 @@ function App() {
189189
const table = useTable({
190190
_features,
191191
_rowModels: {
192-
filteredRowModel: createFilteredRowModel(),
192+
filteredRowModel: createFilteredRowModel({ filterFns }),
193193
paginatedRowModel: createPaginatedRowModel(),
194-
sortedRowModel: createSortedRowModel(),
195-
},
196-
_rowModelFns: {
197-
filterFns,
198-
sortFns,
194+
sortedRowModel: createSortedRowModel({ sortFns }),
199195
},
200196
columns,
201197
data,

examples/react/expanding/src/main.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ const tableHelper = createTableHelper({
3333
rowSortingFeature,
3434
rowSelectionFeature,
3535
},
36-
_rowModelFns: {
37-
filterFns: filterFns,
38-
sortFns: sortFns,
39-
},
4036
_rowModels: {
41-
filteredRowModel: createFilteredRowModel(),
37+
filteredRowModel: createFilteredRowModel({ filterFns }),
4238
paginatedRowModel: createPaginatedRowModel(),
43-
sortedRowModel: createSortedRowModel(),
39+
sortedRowModel: createSortedRowModel({ sortFns }),
4440
},
4541
})
4642

examples/react/filters-faceted/src/main.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,10 @@ function App() {
102102

103103
const table = useTable({
104104
_features,
105-
_rowModelFns: {
106-
filterFns,
107-
sortFns,
108-
},
109105
_rowModels: {
110-
filteredRowModel: createFilteredRowModel(), // client-side filtering
106+
filteredRowModel: createFilteredRowModel({ filterFns }), // client-side filtering
111107
paginatedRowModel: createPaginatedRowModel(),
112-
sortedRowModel: createSortedRowModel(),
108+
sortedRowModel: createSortedRowModel({ sortFns }),
113109
facetedRowModel: createFacetedRowModel(), // client-side faceting
114110
facetedMinMaxValues: createFacetedMinMaxValues(), // generate min/max values for range filter
115111
facetedUniqueValues: createFacetedUniqueValues(), // generate unique values for select filter/autocomplete

0 commit comments

Comments
 (0)