Skip to content

Commit 324a042

Browse files
committed
update vanilla examples to v9
1 parent 9330b1d commit 324a042

6 files changed

Lines changed: 176 additions & 143 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { atom } from 'nanostores'
2+
import {
3+
constructTable,
4+
coreFeatures,
5+
getInitialTableState,
6+
} from '@tanstack/table-core'
7+
import type {
8+
RowData,
9+
Table,
10+
TableFeatures,
11+
TableOptions,
12+
TableState,
13+
} from '@tanstack/table-core'
14+
15+
export const flexRender = <TProps extends object>(comp: any, props: TProps) => {
16+
if (typeof comp === 'function') {
17+
return comp(props)
18+
}
19+
return comp
20+
}
21+
22+
export const createTable = <
23+
TFeatures extends TableFeatures,
24+
TData extends RowData,
25+
>(
26+
tableOptions: TableOptions<TFeatures, TData>,
27+
): Table<TFeatures, TData> => {
28+
const _features = { ...coreFeatures, ...tableOptions._features }
29+
30+
// const initialState = getInitialTableState(_features)
31+
const state = atom(getInitialTableState(_features, tableOptions.initialState))
32+
33+
// Compose in the generic options to the user options
34+
const statefulOptions: TableOptions<TFeatures, TData> = {
35+
...tableOptions,
36+
_features,
37+
state: { ...state, ...tableOptions.state },
38+
}
39+
40+
// Create a new table
41+
const table = constructTable(statefulOptions)
42+
43+
// Subscribe to state changes
44+
state.subscribe((currentState) => {
45+
table.setOptions((prev) => ({
46+
...prev,
47+
...tableOptions,
48+
state: {
49+
...(currentState as TableState<TFeatures>),
50+
...tableOptions.state,
51+
},
52+
// Similarly, we'll maintain both our internal state and any user-provided state
53+
onStateChange: (updater) => {
54+
if (typeof updater === 'function') {
55+
const newState = updater(currentState as TableState<TFeatures>)
56+
state.set(newState)
57+
} else {
58+
state.set(updater)
59+
}
60+
tableOptions.onStateChange?.(updater)
61+
},
62+
}))
63+
})
64+
65+
return table
66+
}

examples/vanilla/basic/src/main.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import './index.css'
2-
3-
import { createColumnHelper, getCoreRowModel } from '@tanstack/table-core'
4-
5-
import { flexRender, useTable } from './useTable'
2+
import { createColumnHelper, tableFeatures } from '@tanstack/table-core'
3+
import { createTable, flexRender } from './createTable'
64

75
type Person = {
86
firstName: string
@@ -13,7 +11,7 @@ type Person = {
1311
progress: number
1412
}
1513

16-
const data: Person[] = [
14+
const data: Array<Person> = [
1715
{
1816
firstName: 'tanner',
1917
lastName: 'linsley',
@@ -40,9 +38,11 @@ const data: Person[] = [
4038
},
4139
]
4240

43-
const columnHelper = createColumnHelper<Person>()
41+
const _features = tableFeatures({})
42+
43+
const columnHelper = createColumnHelper<typeof _features, Person>()
4444

45-
const columns = [
45+
const columns = columnHelper.columns([
4646
columnHelper.accessor('firstName', {
4747
cell: (info) => info.getValue(),
4848
footer: (info) => info.column.id,
@@ -70,7 +70,7 @@ const columns = [
7070
header: 'Profile Progress',
7171
footer: (info) => info.column.id,
7272
}),
73-
]
73+
])
7474

7575
const renderTable = () => {
7676
// Create table elements
@@ -99,7 +99,7 @@ const renderTable = () => {
9999
// Render table rows
100100
table.getRowModel().rows.forEach((row) => {
101101
const trElement = document.createElement('tr')
102-
row.getVisibleCells().forEach((cell) => {
102+
row.getAllCells().forEach((cell) => {
103103
const tdElement = document.createElement('td')
104104
tdElement.innerHTML = flexRender(
105105
cell.column.columnDef.cell,
@@ -129,10 +129,12 @@ const renderTable = () => {
129129
wrapperElement.appendChild(tableElement)
130130
}
131131

132-
const table = useTable<Person>({
133-
data,
132+
const table = createTable({
133+
_features,
134+
_rowModels: {},
134135
columns,
135-
getCoreRowModel: getCoreRowModel(),
136+
data,
137+
debugAll: true,
136138
})
137139

138140
renderTable()

examples/vanilla/basic/src/useTable.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { atom } from 'nanostores'
2+
import {
3+
constructTable,
4+
coreFeatures,
5+
getInitialTableState,
6+
} from '@tanstack/table-core'
7+
import type {
8+
RowData,
9+
Table,
10+
TableFeatures,
11+
TableOptions,
12+
TableState,
13+
} from '@tanstack/table-core'
14+
15+
export const flexRender = <TProps extends object>(comp: any, props: TProps) => {
16+
if (typeof comp === 'function') {
17+
return comp(props)
18+
}
19+
return comp
20+
}
21+
22+
export const createTable = <
23+
TFeatures extends TableFeatures,
24+
TData extends RowData,
25+
>(
26+
tableOptions: TableOptions<TFeatures, TData>,
27+
): Table<TFeatures, TData> => {
28+
const _features = { ...coreFeatures, ...tableOptions._features }
29+
30+
// const initialState = getInitialTableState(_features)
31+
const state = atom(getInitialTableState(_features, tableOptions.initialState))
32+
33+
// Compose in the generic options to the user options
34+
const statefulOptions: TableOptions<TFeatures, TData> = {
35+
...tableOptions,
36+
_features,
37+
state: { ...state, ...tableOptions.state },
38+
}
39+
40+
// Create a new table
41+
const table = constructTable(statefulOptions)
42+
43+
// Subscribe to state changes
44+
state.subscribe((currentState) => {
45+
table.setOptions((prev) => ({
46+
...prev,
47+
...tableOptions,
48+
state: {
49+
...(currentState as TableState<TFeatures>),
50+
...tableOptions.state,
51+
},
52+
// Similarly, we'll maintain both our internal state and any user-provided state
53+
onStateChange: (updater) => {
54+
if (typeof updater === 'function') {
55+
const newState = updater(currentState as TableState<TFeatures>)
56+
state.set(newState)
57+
} else {
58+
state.set(updater)
59+
}
60+
tableOptions.onStateChange?.(updater)
61+
},
62+
}))
63+
})
64+
65+
return table
66+
}

examples/vanilla/pagination/src/main.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@ import './index.css'
22

33
import {
44
createColumnHelper,
5-
getCoreRowModel,
6-
getPaginationRowModel,
7-
getSortedRowModel,
5+
createPaginatedRowModel,
6+
createSortedRowModel,
7+
rowPaginationFeature,
8+
rowSortingFeature,
9+
sortFns,
10+
tableFeatures,
811
} from '@tanstack/table-core'
9-
10-
import { makeData, Person } from './makeData'
11-
import { flexRender, useTable } from './useTable'
12+
import { makeData } from './makeData'
13+
import { createTable, flexRender } from './createTable'
14+
import type { Person } from './makeData'
15+
import type { Table } from '@tanstack/table-core'
1216

1317
const data = makeData(100000)
1418

15-
const columnHelper = createColumnHelper<Person>()
19+
const _features = tableFeatures({
20+
rowPaginationFeature,
21+
rowSortingFeature,
22+
})
23+
24+
const columnHelper = createColumnHelper<typeof _features, Person>()
1625

17-
const columns = [
26+
const columns = columnHelper.columns([
1827
columnHelper.accessor('firstName', {
1928
cell: (info) => info.getValue(),
2029
footer: (info) => info.column.id,
@@ -42,9 +51,9 @@ const columns = [
4251
header: 'Profile Progress',
4352
footer: (info) => info.column.id,
4453
}),
45-
]
54+
])
4655

47-
const renderTable = () => {
56+
const renderTable = (table: Table<typeof _features, Person>) => {
4857
// Create table elements
4958
const tableElement = document.createElement('table')
5059
const theadElement = document.createElement('thead')
@@ -87,7 +96,7 @@ const renderTable = () => {
8796
// Render table rows
8897
table.getRowModel().rows.forEach((row) => {
8998
const trElement = document.createElement('tr')
90-
row.getVisibleCells().forEach((cell) => {
99+
row.getAllCells().forEach((cell) => {
91100
const tdElement = document.createElement('td')
92101
tdElement.innerHTML = flexRender(
93102
cell.column.columnDef.cell,
@@ -195,11 +204,17 @@ const renderTable = () => {
195204
wrapperElement.appendChild(stateInfoElement)
196205
}
197206

198-
const table = useTable<Person>({
207+
const table = createTable({
208+
_features,
209+
_rowModels: {
210+
paginatedRowModel: createPaginatedRowModel(),
211+
sortedRowModel: createSortedRowModel(sortFns),
212+
},
199213
data,
200214
columns,
201215
initialState: {
202216
pagination: {
217+
pageIndex: 0,
203218
pageSize: 10,
204219
},
205220
sorting: [
@@ -209,10 +224,8 @@ const table = useTable<Person>({
209224
},
210225
],
211226
},
212-
getCoreRowModel: getCoreRowModel(),
213-
getPaginationRowModel: getPaginationRowModel(),
214-
getSortedRowModel: getSortedRowModel(),
215-
onStateChange: () => renderTable(),
227+
onStateChange: () => renderTable(table),
228+
debugTable: true,
216229
})
217230

218-
renderTable()
231+
renderTable(table)

0 commit comments

Comments
 (0)