Skip to content

Commit ad9367f

Browse files
committed
chore: update react col examples
1 parent 1ff06b7 commit ad9367f

File tree

7 files changed

+106
-59
lines changed

7 files changed

+106
-59
lines changed

examples/react/column-pinning-sticky/src/main.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import React, { type CSSProperties } from 'react'
22
import ReactDOM from 'react-dom/client'
3-
4-
import './index.css'
5-
6-
import { createCoreRowModel, flexRender, useTable } from '@tanstack/react-table'
3+
import {
4+
type Column,
5+
type ColumnDef,
6+
ColumnOrdering,
7+
ColumnPinning,
8+
ColumnVisibility,
9+
flexRender,
10+
tableFeatures,
11+
useTable,
12+
} from '@tanstack/react-table'
713
import { faker } from '@faker-js/faker'
814
import { makeData } from './makeData'
915
import type { Person } from './makeData'
10-
import type { Column, ColumnDef } from '@tanstack/react-table'
16+
import './index.css'
17+
18+
const _features = tableFeatures({
19+
ColumnVisibility,
20+
ColumnPinning,
21+
ColumnOrdering,
22+
})
1123

1224
//These are the important styles to make sticky column pinning work!
1325
//Apply styles like this using your CSS strategy of choice with this kind of logic to head cells, data cells, footer cells, etc.
@@ -88,9 +100,8 @@ function App() {
88100
const rerender = () => setData(() => makeData(30))
89101

90102
const table = useTable({
91-
_rowModels: {
92-
Core: createCoreRowModel(),
93-
},
103+
_features,
104+
_rowModels: {},
94105
columns,
95106
data,
96107
debugTable: true,

examples/react/column-pinning/src/main.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { faker } from '@faker-js/faker'
44

55
import './index.css'
66

7-
import { createCoreRowModel, flexRender, useTable } from '@tanstack/react-table'
7+
import {
8+
ColumnOrdering,
9+
ColumnPinning,
10+
ColumnVisibility,
11+
flexRender,
12+
useTable,
13+
} from '@tanstack/react-table'
814
import { makeData } from './makeData'
915
import type {
1016
ColumnDef,
@@ -13,7 +19,9 @@ import type {
1319
} from '@tanstack/react-table'
1420
import type { Person } from './makeData'
1521

16-
const defaultColumns: Array<ColumnDef<any, Person>> = [
22+
const _features = { ColumnVisibility, ColumnPinning, ColumnOrdering }
23+
24+
const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
1725
{
1826
header: 'Name',
1927
footer: (props) => props.column.id,
@@ -78,9 +86,8 @@ function App() {
7886
const rerender = () => setData(() => makeData(5000))
7987

8088
const table = useTable({
81-
_rowModels: {
82-
Core: createCoreRowModel(),
83-
},
89+
_features,
90+
_rowModels: {},
8491
columns,
8592
data,
8693
state: {

examples/react/column-resizing-performant/src/main.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
3-
4-
import './index.css'
5-
6-
import { createCoreRowModel, flexRender, useTable } from '@tanstack/react-table'
3+
import {
4+
ColumnResizing,
5+
ColumnSizing,
6+
flexRender,
7+
tableFeatures,
8+
useTable,
9+
} from '@tanstack/react-table'
710
import { makeData } from './makeData'
811
import type { ColumnDef, Table } from '@tanstack/react-table'
12+
import './index.css'
13+
14+
const _features = tableFeatures({ ColumnSizing, ColumnResizing })
915

1016
type Person = {
1117
firstName: string
@@ -16,7 +22,7 @@ type Person = {
1622
progress: number
1723
}
1824

19-
const defaultColumns: Array<ColumnDef<any, Person>> = [
25+
const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
2026
{
2127
header: 'Name',
2228
footer: (props) => props.column.id,
@@ -72,9 +78,8 @@ function App() {
7278
const rerender = React.useReducer(() => ({}), {})[1]
7379

7480
const table = useTable({
75-
_rowModels: {
76-
Core: createCoreRowModel(),
77-
},
81+
_features,
82+
_rowModels: {},
7883
columns,
7984
data,
8085
defaultColumn: {
@@ -96,8 +101,7 @@ function App() {
96101
const columnSizeVars = React.useMemo(() => {
97102
const headers = table.getFlatHeaders()
98103
const colSizes: { [key: string]: number } = {}
99-
for (let i = 0; i < headers.length; i++) {
100-
const header = headers[i]
104+
for (const header of headers) {
101105
colSizes[`--header-${header.id}-size`] = header.getSize()
102106
colSizes[`--col-${header.column.id}-size`] = header.column.getSize()
103107
}
@@ -215,7 +219,7 @@ function TableBody({ table }: { table: Table<any, Person> }) {
215219
>
216220
{row.getVisibleCells().map((cell) => {
217221
//simulate expensive render
218-
for (let i = 0; i < 10000; i++) {
222+
for (const _ of Array(10000)) {
219223
Math.random()
220224
}
221225

examples/react/column-sizing/src/main.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
3-
4-
import './index.css'
5-
6-
import { createCoreRowModel, flexRender, useTable } from '@tanstack/react-table'
3+
import {
4+
ColumnResizing,
5+
ColumnSizing,
6+
flexRender,
7+
tableFeatures,
8+
useTable,
9+
} from '@tanstack/react-table'
710
import type {
811
ColumnDef,
912
ColumnResizeDirection,
1013
ColumnResizeMode,
1114
} from '@tanstack/react-table'
15+
import './index.css'
16+
17+
const _features = tableFeatures({ ColumnResizing, ColumnSizing })
1218

1319
type Person = {
1420
firstName: string
@@ -46,7 +52,7 @@ const defaultData: Array<Person> = [
4652
},
4753
]
4854

49-
const defaultColumns: Array<ColumnDef<any, Person>> = [
55+
const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
5056
{
5157
header: 'Name',
5258
footer: (props) => props.column.id,
@@ -113,9 +119,8 @@ function App() {
113119
const rerender = React.useReducer(() => ({}), {})[1]
114120

115121
const table = useTable({
116-
_rowModels: {
117-
Core: createCoreRowModel(),
118-
},
122+
_features,
123+
_rowModels: {},
119124
columns,
120125
data,
121126
columnResizeMode,

examples/react/column-visibility/src/main.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
3-
3+
import {
4+
ColumnVisibility,
5+
flexRender,
6+
tableFeatures,
7+
useTable,
8+
} from '@tanstack/react-table'
9+
import type { ColumnDef } from '@tanstack/react-table'
410
import './index.css'
511

6-
import { createCoreRowModel, flexRender, useTable } from '@tanstack/react-table'
7-
import type { ColumnDef } from '@tanstack/react-table'
12+
const _features = tableFeatures({ ColumnVisibility })
813

914
type Person = {
1015
firstName: string
@@ -42,7 +47,7 @@ const defaultData: Array<Person> = [
4247
},
4348
]
4449

45-
const defaultColumns: Array<ColumnDef<any, Person>> = [
50+
const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
4651
{
4752
header: 'Name',
4853
footer: (props) => props.column.id,
@@ -95,7 +100,7 @@ const defaultColumns: Array<ColumnDef<any, Person>> = [
95100
]
96101

97102
function App() {
98-
const [data, setData] = React.useState(() => [...defaultData])
103+
const [data, _setData] = React.useState(() => [...defaultData])
99104
const [columns] = React.useState<typeof defaultColumns>(() => [
100105
...defaultColumns,
101106
])
@@ -104,9 +109,8 @@ function App() {
104109
const rerender = React.useReducer(() => ({}), {})[1]
105110

106111
const table = useTable({
107-
_rowModels: {
108-
Core: createCoreRowModel(),
109-
},
112+
_features,
113+
_rowModels: {},
110114
columns,
111115
data,
112116
state: {

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
3-
4-
//
5-
import './index.css'
6-
7-
//
83
import {
9-
createCoreRowModel,
4+
ColumnFiltering,
5+
RowPagination,
106
createFilteredRowModel,
117
createPaginatedRowModel,
128
flexRender,
9+
tableOptions,
1310
useTable,
1411
} from '@tanstack/react-table'
1512
import { makeData } from './makeData'
@@ -21,6 +18,15 @@ import type {
2118
TableFeatures,
2219
} from '@tanstack/react-table'
2320
import type { Person } from './makeData'
21+
import './index.css'
22+
23+
const options = tableOptions({
24+
_features: { RowPagination, ColumnFiltering },
25+
_rowModels: {
26+
Filtered: createFilteredRowModel(),
27+
Paginated: createPaginatedRowModel(),
28+
},
29+
})
2430

2531
declare module '@tanstack/react-table' {
2632
interface TableMeta<TFeatures extends TableFeatures, TData extends RowData> {
@@ -29,7 +35,7 @@ declare module '@tanstack/react-table' {
2935
}
3036

3137
// Give our default column cell renderer editing superpowers!
32-
const defaultColumn: Partial<ColumnDef<any, any>> = {
38+
const defaultColumn: Partial<ColumnDef<typeof options._features, Person>> = {
3339
cell: ({ getValue, row: { index }, column: { id }, table }) => {
3440
const initialValue = getValue()
3541
// We need to keep and update the state of the cell normally
@@ -74,7 +80,9 @@ function useSkipper() {
7480
function App() {
7581
const rerender = React.useReducer(() => ({}), {})[1]
7682

77-
const columns = React.useMemo<Array<ColumnDef<any, Person>>>(
83+
const columns = React.useMemo<
84+
Array<ColumnDef<typeof options._features, Person>>
85+
>(
7886
() => [
7987
{
8088
header: 'Name',
@@ -133,11 +141,7 @@ function App() {
133141
const [autoResetPageIndex, skipAutoResetPageIndex] = useSkipper()
134142

135143
const table = useTable({
136-
_rowModels: {
137-
Core: createCoreRowModel(),
138-
Filtered: createFilteredRowModel(),
139-
Paginated: createPaginatedRowModel(),
140-
},
144+
...options,
141145
columns,
142146
data,
143147
defaultColumn,

packages/table-core/src/helpers/tableOptions.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ export function tableOptions<
66
TFeatures extends TableFeatures,
77
TData extends RowData = any,
88
>(
9-
options: Omit<TableOptions<TFeatures, TData>, 'columns'>,
10-
): Omit<TableOptions<TFeatures, TData>, 'columns'>
9+
options: Omit<TableOptions<TFeatures, TData>, 'columns'> & {
10+
_features: TFeatures
11+
},
12+
): Omit<TableOptions<TFeatures, TData>, 'columns' | '_features'> & {
13+
_features: TFeatures
14+
}
1115

1216
export function tableOptions<
1317
TFeatures extends TableFeatures,
1418
TData extends RowData = any,
1519
>(
16-
options: Omit<TableOptions<TFeatures, TData>, 'data'>,
17-
): Omit<TableOptions<TFeatures, TData>, 'data'>
20+
options: Omit<TableOptions<TFeatures, TData>, 'data'> & {
21+
_features: TFeatures
22+
},
23+
): Omit<TableOptions<TFeatures, TData>, 'data' | '_features'> & {
24+
_features: TFeatures
25+
}
1826

1927
export function tableOptions<
2028
TFeatures extends TableFeatures,
@@ -27,8 +35,12 @@ export function tableOptions<
2735
TFeatures extends TableFeatures,
2836
TData extends RowData = any,
2937
>(
30-
options: Omit<TableOptions<TFeatures, TData>, 'data' | 'columns'>,
31-
): Omit<TableOptions<TFeatures, TData>, 'data' | 'columns'>
38+
options: Omit<TableOptions<TFeatures, TData>, 'data' | 'columns'> & {
39+
_features: TFeatures
40+
},
41+
): Omit<TableOptions<TFeatures, TData>, 'data' | 'columns' | '_features'> & {
42+
_features: TFeatures
43+
}
3244

3345
export function tableOptions<
3446
TFeatures extends TableFeatures,

0 commit comments

Comments
 (0)