-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathmain.tsx
More file actions
211 lines (196 loc) · 6.22 KB
/
main.tsx
File metadata and controls
211 lines (196 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React from 'react'
import ReactDOM from 'react-dom/client'
import {
columnResizingFeature,
columnSizingFeature,
createColumnHelper,
tableFeatures,
useTable,
} from '@tanstack/react-table'
import { makeData } from './makeData'
import type { Person } from './makeData'
import './index.css'
const _features = tableFeatures({ columnSizingFeature, columnResizingFeature })
const columnHelper = createColumnHelper<typeof _features, Person>()
const columns = columnHelper.columns([
columnHelper.accessor('firstName', {
cell: (info) => info.getValue(),
header: 'First Name',
size: 150,
}),
columnHelper.accessor('lastName', {
cell: (info) => info.getValue(),
header: 'Last Name',
size: 150,
}),
columnHelper.accessor('age', {
header: 'Age',
size: 80,
}),
columnHelper.accessor('visits', {
header: 'Visits',
size: 100,
}),
columnHelper.accessor('status', {
header: 'Status',
size: 150,
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
size: 180,
}),
])
function App() {
const [data] = React.useState(() => makeData(20))
const tableContainerRef = React.useRef<HTMLDivElement>(null)
const [containerWidth, setContainerWidth] = React.useState(0)
// Track container width with ResizeObserver
React.useEffect(() => {
const container = tableContainerRef.current
if (!container) return
const observer = new ResizeObserver((entries) => {
for (const entry of entries) {
setContainerWidth(entry.contentRect.width)
}
})
observer.observe(container)
return () => observer.disconnect()
}, [])
const table = useTable(
{
_features,
_rowModels: {},
columns,
data,
defaultColumn: {
minSize: 50,
maxSize: 800,
},
columnResizeMode: 'onChange',
},
(state) => ({
columnSizing: state.columnSizing,
columnResizing: state.columnResizing,
}),
)
const visibleColumns = table.getVisibleLeafColumns()
const totalColumnsWidth = table.getTotalSize()
// Determine if the last column should stretch to fill remaining space
const shouldExtendLastColumn = totalColumnsWidth < containerWidth
// Compute the width for each column, stretching the last one if needed
const getColumnWidth = React.useCallback(
(columnId: string, index: number, baseWidth: number) => {
if (shouldExtendLastColumn && index === visibleColumns.length - 1) {
const otherColumnsWidth = visibleColumns
.slice(0, -1)
.reduce((sum, col) => sum + col.getSize(), 0)
return Math.max(baseWidth, containerWidth - otherColumnsWidth)
}
return baseWidth
},
[shouldExtendLastColumn, visibleColumns, containerWidth],
)
// Pre-compute column sizes as CSS variables for performant resizing
const columnSizeVars = React.useMemo(() => {
const headers = table.getFlatHeaders()
const colSizes: Record<string, number> = {}
for (let i = 0; i < headers.length; i++) {
const header = headers[i]
const width = getColumnWidth(
header.column.id,
i,
header.column.getSize(),
)
colSizes[`--header-${header.id}-size`] = width
colSizes[`--col-${header.column.id}-size`] = width
}
return colSizes
}, [table.state.columnResizing, table.state.columnSizing, getColumnWidth])
// Table width: always at least the container width
const tableWidth = Math.max(totalColumnsWidth, containerWidth)
return (
<div className="p-2">
<h3>Full-Width Column Resizing</h3>
<p>
The table fills its container. The last column stretches to fill any
remaining space. Try resizing individual columns — the last column
adjusts automatically. Resize the browser window to see the table adapt.
</p>
<div className="h-4" />
<div ref={tableContainerRef} className="table-container">
<div
className="divTable"
style={{
...columnSizeVars,
width: tableWidth,
}}
>
<div className="thead" style={{ position: 'sticky', top: 0 }}>
{table.getHeaderGroups().map((headerGroup) => (
<div key={headerGroup.id} className="tr">
{headerGroup.headers.map((header) => (
<div
key={header.id}
className="th"
style={{
width: `calc(var(--header-${header.id}-size) * 1px)`,
}}
>
{header.isPlaceholder ? null : (
<table.FlexRender header={header} />
)}
<div
onDoubleClick={() => header.column.resetSize()}
onMouseDown={header.getResizeHandler()}
onTouchStart={header.getResizeHandler()}
className={`resizer ${
header.column.getIsResizing() ? 'isResizing' : ''
}`}
/>
</div>
))}
</div>
))}
</div>
<div className="tbody">
{table.getRowModel().rows.map((row) => (
<div key={row.id} className="tr">
{row.getAllCells().map((cell) => (
<div
key={cell.id}
className="td"
style={{
width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
}}
>
{cell.renderValue<string>()}
</div>
))}
</div>
))}
</div>
</div>
</div>
<div className="h-4" />
<pre style={{ fontSize: '12px' }}>
{JSON.stringify(
{
containerWidth,
totalColumnsWidth,
shouldExtendLastColumn,
columnSizing: table.state.columnSizing,
},
null,
2,
)}
</pre>
</div>
)
}
const rootElement = document.getElementById('root')
if (!rootElement) throw new Error('Failed to find the root element')
ReactDOM.createRoot(rootElement).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)