Skip to content

Commit e50a7a7

Browse files
committed
new simpler column size example
1 parent 03af078 commit e50a7a7

File tree

17 files changed

+935
-391
lines changed

17 files changed

+935
-391
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react'
22
import ReactDOM from 'react-dom/client'
33
import { createTableHelper, flexRender } from '@tanstack/react-table'
4-
import type { ColumnDef } from '@tanstack/react-table'
54
import './index.css'
65

76
// This example uses the new `createTableHelper` method to create a re-usable table helper object instead of independently using the standalone `useTable` hook and `createColumnHelper` method. You can choose to use either way.
@@ -58,13 +57,14 @@ const tableHelper = createTableHelper({
5857
_rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here
5958
_processingFns: {}, // client-side processing functions used by the row models (sorting, filtering, etc.). Not needed in this basic example
6059
TData: {} as Person,
60+
debugTable: true,
6161
})
6262

6363
// 4. Create a helper object to help define our columns
6464
const { columnHelper } = tableHelper
6565

6666
// 5. Define the columns for your table with a stable reference (in this case, defined statically outside of a react component)
67-
const columns = [
67+
const columns = columnHelper.columns([
6868
// accessorKey method (most common for simple use-cases)
6969
columnHelper.accessor('firstName', {
7070
cell: (info) => info.getValue(),
@@ -96,7 +96,7 @@ const columns = [
9696
header: 'Profile Progress',
9797
footer: (info) => info.column.id,
9898
}),
99-
] as Array<ColumnDef<typeof tableHelper.features, Person, unknown>>
99+
])
100100

101101
function App() {
102102
// 6. Store data with a stable reference

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import React, { type CSSProperties } from 'react'
1+
import React from 'react'
22
import ReactDOM from 'react-dom/client'
33
import {
44
ColumnOrdering,
5+
ColumnSizing,
56
createTableHelper,
67
flexRender,
78
} from '@tanstack/react-table'
89
import {
910
DndContext,
10-
type DragEndEvent,
1111
KeyboardSensor,
1212
MouseSensor,
1313
TouchSensor,
@@ -24,12 +24,14 @@ import {
2424
} from '@dnd-kit/sortable'
2525
import { CSS } from '@dnd-kit/utilities'
2626
import { makeData } from './makeData'
27+
import type { DragEndEvent } from '@dnd-kit/core'
28+
import type { CSSProperties } from 'react'
2729
import type { Person } from './makeData'
2830
import type { Cell, ColumnDef, Header } from '@tanstack/react-table'
2931
import './index.css'
3032

3133
const tableHelper = createTableHelper({
32-
_features: { ColumnOrdering: ColumnOrdering },
34+
_features: { ColumnOrdering, ColumnSizing },
3335
_rowModels: {},
3436
TData: {} as Person,
3537
debugTable: true,

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
tableFeatures,
88
useTable,
99
} from '@tanstack/react-table'
10-
import type { ColumnDef } from '@tanstack/react-table'
1110

1211
type Person = {
1312
firstName: string
@@ -49,11 +48,11 @@ const _features = tableFeatures({})
4948

5049
const columnHelper = createColumnHelper<typeof _features, Person>()
5150

52-
const columns = [
51+
const columns = columnHelper.columns([
5352
columnHelper.group({
5453
id: 'hello',
5554
header: () => <span>Hello</span>,
56-
columns: [
55+
columns: columnHelper.columns([
5756
columnHelper.accessor('firstName', {
5857
cell: (info) => info.getValue(),
5958
footer: (props) => props.column.id,
@@ -64,19 +63,19 @@ const columns = [
6463
header: () => <span>Last Name</span>,
6564
footer: (props) => props.column.id,
6665
}),
67-
] as Array<ColumnDef<typeof _features, Person>>,
66+
]),
6867
}),
6968
columnHelper.group({
7069
header: 'Info',
7170
footer: (props) => props.column.id,
72-
columns: [
71+
columns: columnHelper.columns([
7372
columnHelper.accessor('age', {
7473
header: () => 'Age',
7574
footer: (props) => props.column.id,
7675
}),
7776
columnHelper.group({
7877
header: 'More Info',
79-
columns: [
78+
columns: columnHelper.columns([
8079
columnHelper.accessor('visits', {
8180
header: () => <span>Visits</span>,
8281
footer: (props) => props.column.id,
@@ -89,11 +88,11 @@ const columns = [
8988
header: 'Profile Progress',
9089
footer: (props) => props.column.id,
9190
}),
92-
] as Array<ColumnDef<typeof _features, Person>>,
91+
]),
9392
}),
94-
] as Array<ColumnDef<typeof _features, Person>>,
93+
]),
9594
}),
96-
]
95+
])
9796

9897
function App() {
9998
const [data, _setData] = React.useState(() => [...defaultData])
@@ -128,7 +127,7 @@ function App() {
128127
<tbody>
129128
{table.getRowModel().rows.map((row) => (
130129
<tr key={row.id}>
131-
{row.getVisibleCells().map((cell) => (
130+
{row.getAllCells().map((cell) => (
132131
<td key={cell.id}>
133132
{flexRender(cell.column.columnDef.cell, cell.getContext())}
134133
</td>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ function App() {
103103
)
104104
}
105105

106+
console.log(table.getFlatHeaders())
107+
106108
return (
107109
<div className="p-2">
108110
<div className="inline-block border border-black shadow rounded">
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
dist-ssr
5+
*.local
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example
2+
3+
To run this example:
4+
5+
- `npm install` or `yarn`
6+
- `npm run start` or `yarn start`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Vite App</title>
7+
<script type="module" src="https://cdn.skypack.dev/twind/shim"></script>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "tanstack-table-example-column-resizing",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"serve": "vite preview",
9+
"start": "vite",
10+
"lint": "eslint ./src"
11+
},
12+
"dependencies": {
13+
"@tanstack/react-table": "^9.0.0-alpha.10",
14+
"react": "^18.3.1",
15+
"react-dom": "^18.3.1"
16+
},
17+
"devDependencies": {
18+
"@rollup/plugin-replace": "^6.0.1",
19+
"@types/react": "^18.3.12",
20+
"@types/react-dom": "^18.3.1",
21+
"@vitejs/plugin-react": "^4.3.3",
22+
"typescript": "5.6.3",
23+
"vite": "^5.4.10"
24+
}
25+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
html {
6+
font-family: sans-serif;
7+
font-size: 14px;
8+
}
9+
10+
table,
11+
.divTable {
12+
border: 1px solid lightgray;
13+
width: fit-content;
14+
}
15+
16+
.tr {
17+
display: flex;
18+
}
19+
20+
tr,
21+
.tr {
22+
width: fit-content;
23+
height: 30px;
24+
}
25+
26+
th,
27+
.th,
28+
td,
29+
.td {
30+
box-shadow: inset 0 0 0 1px lightgray;
31+
padding: 0.25rem;
32+
}
33+
34+
th,
35+
.th {
36+
padding: 2px 4px;
37+
position: relative;
38+
font-weight: bold;
39+
text-align: center;
40+
height: 30px;
41+
}
42+
43+
td,
44+
.td {
45+
height: 30px;
46+
}
47+
48+
.resizer {
49+
position: absolute;
50+
top: 0;
51+
height: 100%;
52+
width: 5px;
53+
background: rgba(0, 0, 0, 0.5);
54+
cursor: col-resize;
55+
user-select: none;
56+
touch-action: none;
57+
}
58+
59+
.resizer.ltr {
60+
right: 0;
61+
}
62+
63+
.resizer.rtl {
64+
left: 0;
65+
}
66+
67+
.resizer.isResizing {
68+
background: blue;
69+
opacity: 1;
70+
}
71+
72+
@media (hover: hover) {
73+
.resizer {
74+
opacity: 0;
75+
}
76+
77+
*:hover > .resizer {
78+
opacity: 1;
79+
}
80+
}

0 commit comments

Comments
 (0)