Skip to content

Commit fbb3bd1

Browse files
committed
chore: update more examples
1 parent ad9367f commit fbb3bd1

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

examples/react/editable-data/src/main.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createFilteredRowModel,
77
createPaginatedRowModel,
88
flexRender,
9+
tableFeatures,
910
tableOptions,
1011
useTable,
1112
} from '@tanstack/react-table'
@@ -20,8 +21,10 @@ import type {
2021
import type { Person } from './makeData'
2122
import './index.css'
2223

23-
const options = tableOptions({
24-
_features: { RowPagination, ColumnFiltering },
24+
const _features = tableFeatures({ RowPagination, ColumnFiltering })
25+
26+
const options = tableOptions<typeof _features, Person>({
27+
_features,
2528
_rowModels: {
2629
Filtered: createFilteredRowModel(),
2730
Paginated: createPaginatedRowModel(),

examples/react/expanding/src/main.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import React, { type HTMLProps } from 'react'
22
import ReactDOM from 'react-dom/client'
3-
4-
import './index.css'
5-
63
import {
4+
ColumnFiltering,
5+
RowExpanding,
6+
RowPagination,
7+
RowSorting,
78
createCoreRowModel,
89
createFilteredRowModel,
910
createPaginatedRowModel,
1011
createSortedRowModel,
1112
flexRender,
13+
tableFeatures,
1214
useTable,
1315
} from '@tanstack/react-table'
1416
import { makeData } from './makeData'
@@ -19,11 +21,19 @@ import type {
1921
ExpandedState,
2022
Table,
2123
} from '@tanstack/react-table'
24+
import './index.css'
25+
26+
const _features = tableFeatures({
27+
ColumnFiltering,
28+
RowExpanding,
29+
RowPagination,
30+
RowSorting,
31+
})
2232

2333
function App() {
2434
const rerender = React.useReducer(() => ({}), {})[1]
2535

26-
const columns = React.useMemo<Array<ColumnDef<any, Person>>>(
36+
const columns = React.useMemo<Array<ColumnDef<typeof _features, Person>>>(
2737
() => [
2838
{
2939
accessorKey: 'firstName',
@@ -119,6 +129,7 @@ function App() {
119129
const [expanded, setExpanded] = React.useState<ExpandedState>({})
120130

121131
const table = useTable({
132+
_features,
122133
_rowModels: {
123134
Core: createCoreRowModel(),
124135
Filtered: createFilteredRowModel(),
@@ -266,8 +277,8 @@ function Filter({
266277
column,
267278
table,
268279
}: {
269-
column: Column<any, any>
270-
table: Table<any, any>
280+
column: Column<typeof _features, Person>
281+
table: Table<typeof _features, Person>
271282
}) {
272283
const firstValue = table
273284
.getPreFilteredRowModel()

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import ReactDOM from 'react-dom/client'
44
import './index.css'
55

66
import {
7+
ColumnFiltering,
8+
RowPagination,
9+
RowSorting,
710
createCoreRowModel,
811
createFilteredRowModel,
912
createPaginatedRowModel,
1013
createSortedRowModel,
1114
flexRender,
1215
sortingFns,
16+
tableFeatures,
1317
useTable,
1418
} from '@tanstack/react-table'
1519
import { compareItems, rankItem } from '@tanstack/match-sorter-utils'
@@ -27,18 +31,29 @@ import type { RankingInfo } from '@tanstack/match-sorter-utils'
2731

2832
import type { Person } from './makeData'
2933

34+
const _features = tableFeatures({
35+
ColumnFiltering,
36+
RowSorting,
37+
RowPagination,
38+
})
39+
3040
declare module '@tanstack/react-table' {
3141
//add fuzzy filter to the filterFns
3242
interface FilterFns {
33-
fuzzy: FilterFn<unknown>
43+
fuzzy: FilterFn<typeof _features, unknown>
3444
}
3545
interface FilterMeta {
3646
itemRank?: RankingInfo
3747
}
3848
}
3949

4050
// Define a custom fuzzy filter function that will apply ranking info to rows (using match-sorter utils)
41-
const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
51+
const fuzzyFilter: FilterFn<typeof _features, any> = (
52+
row,
53+
columnId,
54+
value,
55+
addMeta,
56+
) => {
4257
// Rank the item
4358
const itemRank = rankItem(row.getValue(columnId), value)
4459

@@ -52,7 +67,7 @@ const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
5267
}
5368

5469
// Define a custom fuzzy sort function that will sort by rank if the row has ranking information
55-
const fuzzySort: SortingFn<any> = (rowA, rowB, columnId) => {
70+
const fuzzySort: SortingFn<typeof _features, any> = (rowA, rowB, columnId) => {
5671
let dir = 0
5772

5873
// Only sort by rank if the column has ranking information
@@ -75,7 +90,7 @@ function App() {
7590
)
7691
const [globalFilter, setGlobalFilter] = React.useState<string | undefined>('')
7792

78-
const columns = React.useMemo<Array<ColumnDef<any, Person>>>(
93+
const columns = React.useMemo<Array<ColumnDef<typeof _features, Person>>>(
7994
() => [
8095
{
8196
accessorKey: 'id',
@@ -109,7 +124,8 @@ function App() {
109124
const [data, setData] = React.useState<Array<Person>>(() => makeData(5_000))
110125
const refreshData = () => setData((_old) => makeData(50_000)) //stress test
111126

112-
const table = useTable({
127+
const table = useTable<typeof _features, Person>({
128+
_features,
113129
_rowModels: {
114130
Core: createCoreRowModel(),
115131
Filtered: createFilteredRowModel(),
@@ -294,7 +310,7 @@ function App() {
294310
)
295311
}
296312

297-
function Filter({ column }: { column: Column<any, unknown> }) {
313+
function Filter({ column }: { column: Column<typeof _features, Person> }) {
298314
const columnFilterValue = column.getFilterValue()
299315

300316
return (

examples/react/filters/src/main.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
createPaginatedRowModel,
1313
createSortedRowModel,
1414
flexRender,
15+
tableFeatures,
1516
useTable,
1617
} from '@tanstack/react-table'
1718
import { makeData } from './makeData'
@@ -37,14 +38,18 @@ declare module '@tanstack/react-table' {
3738
}
3839
}
3940

41+
const _features = tableFeatures({ ColumnFiltering, RowSorting, RowPagination })
42+
4043
function App() {
4144
const rerender = React.useReducer(() => ({}), {})[1]
4245

4346
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
4447
[],
4548
)
4649

47-
const columns = React.useMemo<Array<ColumnDef<any, Person, any>>>(
50+
const columns = React.useMemo<
51+
Array<ColumnDef<typeof _features, Person, any>>
52+
>(
4853
() => [
4954
{
5055
accessorKey: 'firstName',
@@ -98,7 +103,7 @@ function App() {
98103
const refreshData = () => setData((_old) => makeData(50_000)) //stress test
99104

100105
const table = useTable({
101-
_features: { ColumnFiltering, RowSorting, RowPagination },
106+
_features,
102107
_rowModels: {
103108
Core: createCoreRowModel(),
104109
Filtered: createFilteredRowModel(), //client side filtering

0 commit comments

Comments
 (0)