@tanstack/preact-table 0.0.1 → 9.0.0-alpha.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/esm/FlexRender.d.ts +46 -0
- package/dist/esm/FlexRender.js +41 -0
- package/dist/esm/FlexRender.js.map +1 -0
- package/dist/esm/Subscribe.d.ts +44 -0
- package/dist/esm/Subscribe.js +9 -0
- package/dist/esm/Subscribe.js.map +1 -0
- package/dist/esm/createTableHook.d.ts +349 -0
- package/dist/esm/createTableHook.js +133 -0
- package/dist/esm/createTableHook.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/useTable.d.ts +52 -0
- package/dist/esm/useTable.js +46 -0
- package/dist/esm/useTable.js.map +1 -0
- package/package.json +61 -7
- package/src/FlexRender.tsx +119 -0
- package/src/Subscribe.ts +67 -0
- package/src/createTableHook.tsx +1120 -0
- package/src/index.ts +6 -0
- package/src/useTable.ts +132 -0
- package/README.md +0 -45
|
@@ -0,0 +1,1120 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { createContext } from 'preact'
|
|
3
|
+
import { useContext, useMemo } from 'preact/hooks'
|
|
4
|
+
import { createColumnHelper as coreCreateColumnHelper } from '@tanstack/table-core'
|
|
5
|
+
import { useTable } from './useTable'
|
|
6
|
+
import { FlexRender } from './FlexRender'
|
|
7
|
+
import type {
|
|
8
|
+
AccessorFn,
|
|
9
|
+
AccessorFnColumnDef,
|
|
10
|
+
AccessorKeyColumnDef,
|
|
11
|
+
Cell,
|
|
12
|
+
CellContext,
|
|
13
|
+
CellData,
|
|
14
|
+
Column,
|
|
15
|
+
ColumnDef,
|
|
16
|
+
DeepKeys,
|
|
17
|
+
DeepValue,
|
|
18
|
+
DisplayColumnDef,
|
|
19
|
+
GroupColumnDef,
|
|
20
|
+
Header,
|
|
21
|
+
IdentifiedColumnDef,
|
|
22
|
+
NoInfer,
|
|
23
|
+
Row,
|
|
24
|
+
RowData,
|
|
25
|
+
Table,
|
|
26
|
+
TableFeatures,
|
|
27
|
+
TableOptions,
|
|
28
|
+
TableState,
|
|
29
|
+
} from '@tanstack/table-core'
|
|
30
|
+
import type { ComponentChildren, ComponentType } from 'preact'
|
|
31
|
+
import type { PreactTable } from './useTable'
|
|
32
|
+
|
|
33
|
+
// =============================================================================
|
|
34
|
+
// Enhanced Context Types with Pre-bound Components
|
|
35
|
+
// =============================================================================
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Enhanced CellContext with pre-bound cell components.
|
|
39
|
+
* The `cell` property includes the registered cellComponents.
|
|
40
|
+
*/
|
|
41
|
+
export type AppCellContext<
|
|
42
|
+
TFeatures extends TableFeatures,
|
|
43
|
+
TData extends RowData,
|
|
44
|
+
TValue extends CellData,
|
|
45
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
46
|
+
> = {
|
|
47
|
+
cell: Cell<TFeatures, TData, TValue> &
|
|
48
|
+
TCellComponents & { FlexRender: () => ComponentChildren }
|
|
49
|
+
column: Column<TFeatures, TData, TValue>
|
|
50
|
+
getValue: CellContext<TFeatures, TData, TValue>['getValue']
|
|
51
|
+
renderValue: CellContext<TFeatures, TData, TValue>['renderValue']
|
|
52
|
+
row: Row<TFeatures, TData>
|
|
53
|
+
table: Table<TFeatures, TData>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Enhanced HeaderContext with pre-bound header components.
|
|
58
|
+
* The `header` property includes the registered headerComponents.
|
|
59
|
+
*/
|
|
60
|
+
export type AppHeaderContext<
|
|
61
|
+
TFeatures extends TableFeatures,
|
|
62
|
+
TData extends RowData,
|
|
63
|
+
TValue extends CellData,
|
|
64
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
65
|
+
> = {
|
|
66
|
+
column: Column<TFeatures, TData, TValue>
|
|
67
|
+
header: Header<TFeatures, TData, TValue> &
|
|
68
|
+
THeaderComponents & { FlexRender: () => ComponentChildren }
|
|
69
|
+
table: Table<TFeatures, TData>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// =============================================================================
|
|
73
|
+
// Enhanced Column Definition Types
|
|
74
|
+
// =============================================================================
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Template type for column definitions that can be a string or a function.
|
|
78
|
+
*/
|
|
79
|
+
type AppColumnDefTemplate<TProps extends object> =
|
|
80
|
+
| string
|
|
81
|
+
| ((props: TProps) => any)
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Enhanced column definition base with pre-bound components in cell/header/footer contexts.
|
|
85
|
+
*/
|
|
86
|
+
type AppColumnDefBase<
|
|
87
|
+
TFeatures extends TableFeatures,
|
|
88
|
+
TData extends RowData,
|
|
89
|
+
TValue extends CellData,
|
|
90
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
91
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
92
|
+
> = Omit<
|
|
93
|
+
IdentifiedColumnDef<TFeatures, TData, TValue>,
|
|
94
|
+
'cell' | 'header' | 'footer'
|
|
95
|
+
> & {
|
|
96
|
+
cell?: AppColumnDefTemplate<
|
|
97
|
+
AppCellContext<TFeatures, TData, TValue, TCellComponents>
|
|
98
|
+
>
|
|
99
|
+
header?: AppColumnDefTemplate<
|
|
100
|
+
AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>
|
|
101
|
+
>
|
|
102
|
+
footer?: AppColumnDefTemplate<
|
|
103
|
+
AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>
|
|
104
|
+
>
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Enhanced display column definition with pre-bound components.
|
|
109
|
+
*/
|
|
110
|
+
type AppDisplayColumnDef<
|
|
111
|
+
TFeatures extends TableFeatures,
|
|
112
|
+
TData extends RowData,
|
|
113
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
114
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
115
|
+
> = Omit<
|
|
116
|
+
DisplayColumnDef<TFeatures, TData, unknown>,
|
|
117
|
+
'cell' | 'header' | 'footer'
|
|
118
|
+
> & {
|
|
119
|
+
cell?: AppColumnDefTemplate<
|
|
120
|
+
AppCellContext<TFeatures, TData, unknown, TCellComponents>
|
|
121
|
+
>
|
|
122
|
+
header?: AppColumnDefTemplate<
|
|
123
|
+
AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
|
|
124
|
+
>
|
|
125
|
+
footer?: AppColumnDefTemplate<
|
|
126
|
+
AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
|
|
127
|
+
>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Enhanced group column definition with pre-bound components.
|
|
132
|
+
*/
|
|
133
|
+
type AppGroupColumnDef<
|
|
134
|
+
TFeatures extends TableFeatures,
|
|
135
|
+
TData extends RowData,
|
|
136
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
137
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
138
|
+
> = Omit<
|
|
139
|
+
GroupColumnDef<TFeatures, TData, unknown>,
|
|
140
|
+
'cell' | 'header' | 'footer' | 'columns'
|
|
141
|
+
> & {
|
|
142
|
+
cell?: AppColumnDefTemplate<
|
|
143
|
+
AppCellContext<TFeatures, TData, unknown, TCellComponents>
|
|
144
|
+
>
|
|
145
|
+
header?: AppColumnDefTemplate<
|
|
146
|
+
AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
|
|
147
|
+
>
|
|
148
|
+
footer?: AppColumnDefTemplate<
|
|
149
|
+
AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
|
|
150
|
+
>
|
|
151
|
+
columns?: Array<ColumnDef<TFeatures, TData, unknown>>
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// =============================================================================
|
|
155
|
+
// Enhanced Column Helper Type
|
|
156
|
+
// =============================================================================
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Enhanced column helper with pre-bound components in cell/header/footer contexts.
|
|
160
|
+
* This enables TypeScript to know about the registered components when defining columns.
|
|
161
|
+
*/
|
|
162
|
+
export type AppColumnHelper<
|
|
163
|
+
TFeatures extends TableFeatures,
|
|
164
|
+
TData extends RowData,
|
|
165
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
166
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
167
|
+
> = {
|
|
168
|
+
/**
|
|
169
|
+
* Creates a data column definition with an accessor key or function.
|
|
170
|
+
* The cell, header, and footer contexts include pre-bound components.
|
|
171
|
+
*/
|
|
172
|
+
accessor: <
|
|
173
|
+
TAccessor extends AccessorFn<TData> | DeepKeys<TData>,
|
|
174
|
+
TValue extends TAccessor extends AccessorFn<TData, infer TReturn>
|
|
175
|
+
? TReturn
|
|
176
|
+
: TAccessor extends DeepKeys<TData>
|
|
177
|
+
? DeepValue<TData, TAccessor>
|
|
178
|
+
: never,
|
|
179
|
+
>(
|
|
180
|
+
accessor: TAccessor,
|
|
181
|
+
column: TAccessor extends AccessorFn<TData>
|
|
182
|
+
? AppColumnDefBase<
|
|
183
|
+
TFeatures,
|
|
184
|
+
TData,
|
|
185
|
+
TValue,
|
|
186
|
+
TCellComponents,
|
|
187
|
+
THeaderComponents
|
|
188
|
+
> & { id: string }
|
|
189
|
+
: AppColumnDefBase<
|
|
190
|
+
TFeatures,
|
|
191
|
+
TData,
|
|
192
|
+
TValue,
|
|
193
|
+
TCellComponents,
|
|
194
|
+
THeaderComponents
|
|
195
|
+
>,
|
|
196
|
+
) => TAccessor extends AccessorFn<TData>
|
|
197
|
+
? AccessorFnColumnDef<TFeatures, TData, TValue>
|
|
198
|
+
: AccessorKeyColumnDef<TFeatures, TData, TValue>
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Wraps an array of column definitions to preserve each column's individual TValue type.
|
|
202
|
+
*/
|
|
203
|
+
columns: <TColumns extends ReadonlyArray<ColumnDef<TFeatures, TData, any>>>(
|
|
204
|
+
columns: [...TColumns],
|
|
205
|
+
) => Array<ColumnDef<TFeatures, TData, any>> & [...TColumns]
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Creates a display column definition for non-data columns.
|
|
209
|
+
* The cell, header, and footer contexts include pre-bound components.
|
|
210
|
+
*/
|
|
211
|
+
display: (
|
|
212
|
+
column: AppDisplayColumnDef<
|
|
213
|
+
TFeatures,
|
|
214
|
+
TData,
|
|
215
|
+
TCellComponents,
|
|
216
|
+
THeaderComponents
|
|
217
|
+
>,
|
|
218
|
+
) => DisplayColumnDef<TFeatures, TData, unknown>
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Creates a group column definition with nested child columns.
|
|
222
|
+
* The cell, header, and footer contexts include pre-bound components.
|
|
223
|
+
*/
|
|
224
|
+
group: (
|
|
225
|
+
column: AppGroupColumnDef<
|
|
226
|
+
TFeatures,
|
|
227
|
+
TData,
|
|
228
|
+
TCellComponents,
|
|
229
|
+
THeaderComponents
|
|
230
|
+
>,
|
|
231
|
+
) => GroupColumnDef<TFeatures, TData, unknown>
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// =============================================================================
|
|
235
|
+
// CreateTableHook Options and Props
|
|
236
|
+
// =============================================================================
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Options for creating a table hook with pre-bound components and default table options.
|
|
240
|
+
* Extends all TableOptions except 'columns' | 'data' | 'store' | 'state' | 'initialState'.
|
|
241
|
+
*/
|
|
242
|
+
export type CreateTableHookOptions<
|
|
243
|
+
TFeatures extends TableFeatures,
|
|
244
|
+
TTableComponents extends Record<string, ComponentType<any>>,
|
|
245
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
246
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
247
|
+
> = Omit<
|
|
248
|
+
TableOptions<TFeatures, any>,
|
|
249
|
+
'columns' | 'data' | 'store' | 'state' | 'initialState'
|
|
250
|
+
> & {
|
|
251
|
+
/**
|
|
252
|
+
* Table-level components that need access to the table instance.
|
|
253
|
+
* These are available directly on the table object returned by useAppTable.
|
|
254
|
+
* Use `useTableContext()` inside these components.
|
|
255
|
+
* @example { PaginationControls, GlobalFilter, RowCount }
|
|
256
|
+
*/
|
|
257
|
+
tableComponents?: TTableComponents
|
|
258
|
+
/**
|
|
259
|
+
* Cell-level components that need access to the cell instance.
|
|
260
|
+
* These are available on the cell object passed to AppCell's children.
|
|
261
|
+
* Use `useCellContext()` inside these components.
|
|
262
|
+
* @example { TextCell, NumberCell, DateCell, CurrencyCell }
|
|
263
|
+
*/
|
|
264
|
+
cellComponents?: TCellComponents
|
|
265
|
+
/**
|
|
266
|
+
* Header-level components that need access to the header instance.
|
|
267
|
+
* These are available on the header object passed to AppHeader/AppFooter's children.
|
|
268
|
+
* Use `useHeaderContext()` inside these components.
|
|
269
|
+
* @example { SortIndicator, ColumnFilter, ResizeHandle }
|
|
270
|
+
*/
|
|
271
|
+
headerComponents?: THeaderComponents
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Props for AppTable component - without selector
|
|
276
|
+
*/
|
|
277
|
+
export interface AppTablePropsWithoutSelector {
|
|
278
|
+
children: ComponentChildren
|
|
279
|
+
selector?: never
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Props for AppTable component - with selector
|
|
284
|
+
*/
|
|
285
|
+
export interface AppTablePropsWithSelector<
|
|
286
|
+
TFeatures extends TableFeatures,
|
|
287
|
+
TSelected,
|
|
288
|
+
> {
|
|
289
|
+
children: (state: TSelected) => ComponentChildren
|
|
290
|
+
selector: (state: TableState<TFeatures>) => TSelected
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Props for AppCell component - without selector
|
|
295
|
+
*/
|
|
296
|
+
export interface AppCellPropsWithoutSelector<
|
|
297
|
+
TFeatures extends TableFeatures,
|
|
298
|
+
TData extends RowData,
|
|
299
|
+
TValue extends CellData,
|
|
300
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
301
|
+
> {
|
|
302
|
+
cell: Cell<TFeatures, TData, TValue>
|
|
303
|
+
children: (
|
|
304
|
+
cell: Cell<TFeatures, TData, TValue> &
|
|
305
|
+
TCellComponents & { FlexRender: () => ComponentChildren },
|
|
306
|
+
) => ComponentChildren
|
|
307
|
+
selector?: never
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Props for AppCell component - with selector
|
|
312
|
+
*/
|
|
313
|
+
export interface AppCellPropsWithSelector<
|
|
314
|
+
TFeatures extends TableFeatures,
|
|
315
|
+
TData extends RowData,
|
|
316
|
+
TValue extends CellData,
|
|
317
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
318
|
+
TSelected,
|
|
319
|
+
> {
|
|
320
|
+
cell: Cell<TFeatures, TData, TValue>
|
|
321
|
+
children: (
|
|
322
|
+
cell: Cell<TFeatures, TData, TValue> &
|
|
323
|
+
TCellComponents & { FlexRender: () => ComponentChildren },
|
|
324
|
+
state: TSelected,
|
|
325
|
+
) => ComponentChildren
|
|
326
|
+
selector: (state: TableState<TFeatures>) => TSelected
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Props for AppHeader/AppFooter component - without selector
|
|
331
|
+
*/
|
|
332
|
+
export interface AppHeaderPropsWithoutSelector<
|
|
333
|
+
TFeatures extends TableFeatures,
|
|
334
|
+
TData extends RowData,
|
|
335
|
+
TValue extends CellData,
|
|
336
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
337
|
+
> {
|
|
338
|
+
header: Header<TFeatures, TData, TValue>
|
|
339
|
+
children: (
|
|
340
|
+
header: Header<TFeatures, TData, TValue> &
|
|
341
|
+
THeaderComponents & { FlexRender: () => ComponentChildren },
|
|
342
|
+
) => ComponentChildren
|
|
343
|
+
selector?: never
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Props for AppHeader/AppFooter component - with selector
|
|
348
|
+
*/
|
|
349
|
+
export interface AppHeaderPropsWithSelector<
|
|
350
|
+
TFeatures extends TableFeatures,
|
|
351
|
+
TData extends RowData,
|
|
352
|
+
TValue extends CellData,
|
|
353
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
354
|
+
TSelected,
|
|
355
|
+
> {
|
|
356
|
+
header: Header<TFeatures, TData, TValue>
|
|
357
|
+
children: (
|
|
358
|
+
header: Header<TFeatures, TData, TValue> &
|
|
359
|
+
THeaderComponents & { FlexRender: () => ComponentChildren },
|
|
360
|
+
state: TSelected,
|
|
361
|
+
) => ComponentChildren
|
|
362
|
+
selector: (state: TableState<TFeatures>) => TSelected
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Component type for AppCell - wraps a cell and provides cell context with optional Subscribe
|
|
367
|
+
*/
|
|
368
|
+
export interface AppCellComponent<
|
|
369
|
+
TFeatures extends TableFeatures,
|
|
370
|
+
TData extends RowData,
|
|
371
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
372
|
+
> {
|
|
373
|
+
<TValue extends CellData = CellData>(
|
|
374
|
+
props: AppCellPropsWithoutSelector<
|
|
375
|
+
TFeatures,
|
|
376
|
+
TData,
|
|
377
|
+
TValue,
|
|
378
|
+
TCellComponents
|
|
379
|
+
>,
|
|
380
|
+
): ComponentChildren
|
|
381
|
+
<TValue extends CellData = CellData, TSelected = unknown>(
|
|
382
|
+
props: AppCellPropsWithSelector<
|
|
383
|
+
TFeatures,
|
|
384
|
+
TData,
|
|
385
|
+
TValue,
|
|
386
|
+
TCellComponents,
|
|
387
|
+
TSelected
|
|
388
|
+
>,
|
|
389
|
+
): ComponentChildren
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Component type for AppHeader/AppFooter - wraps a header and provides header context with optional Subscribe
|
|
394
|
+
*/
|
|
395
|
+
export interface AppHeaderComponent<
|
|
396
|
+
TFeatures extends TableFeatures,
|
|
397
|
+
TData extends RowData,
|
|
398
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
399
|
+
> {
|
|
400
|
+
<TValue extends CellData = CellData>(
|
|
401
|
+
props: AppHeaderPropsWithoutSelector<
|
|
402
|
+
TFeatures,
|
|
403
|
+
TData,
|
|
404
|
+
TValue,
|
|
405
|
+
THeaderComponents
|
|
406
|
+
>,
|
|
407
|
+
): ComponentChildren
|
|
408
|
+
<TValue extends CellData = CellData, TSelected = unknown>(
|
|
409
|
+
props: AppHeaderPropsWithSelector<
|
|
410
|
+
TFeatures,
|
|
411
|
+
TData,
|
|
412
|
+
TValue,
|
|
413
|
+
THeaderComponents,
|
|
414
|
+
TSelected
|
|
415
|
+
>,
|
|
416
|
+
): ComponentChildren
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Component type for AppTable - root wrapper with optional Subscribe
|
|
421
|
+
*/
|
|
422
|
+
export interface AppTableComponent<TFeatures extends TableFeatures> {
|
|
423
|
+
(props: AppTablePropsWithoutSelector): ComponentChildren
|
|
424
|
+
<TSelected>(
|
|
425
|
+
props: AppTablePropsWithSelector<TFeatures, TSelected>,
|
|
426
|
+
): ComponentChildren
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Extended table API returned by useAppTable with all App wrapper components
|
|
431
|
+
*/
|
|
432
|
+
export type AppPreactTable<
|
|
433
|
+
TFeatures extends TableFeatures,
|
|
434
|
+
TData extends RowData,
|
|
435
|
+
TSelected,
|
|
436
|
+
TTableComponents extends Record<string, ComponentType<any>>,
|
|
437
|
+
TCellComponents extends Record<string, ComponentType<any>>,
|
|
438
|
+
THeaderComponents extends Record<string, ComponentType<any>>,
|
|
439
|
+
> = PreactTable<TFeatures, TData, TSelected> &
|
|
440
|
+
NoInfer<TTableComponents> & {
|
|
441
|
+
/**
|
|
442
|
+
* Root wrapper component that provides table context with optional Subscribe.
|
|
443
|
+
* @example
|
|
444
|
+
* ```tsx
|
|
445
|
+
* // Without selector - children is ComponentChildren
|
|
446
|
+
* <table.AppTable>
|
|
447
|
+
* <table>...</table>
|
|
448
|
+
* </table.AppTable>
|
|
449
|
+
*
|
|
450
|
+
* // With selector - children receives selected state
|
|
451
|
+
* <table.AppTable selector={(s) => s.pagination}>
|
|
452
|
+
* {(pagination) => <div>Page {pagination.pageIndex}</div>}
|
|
453
|
+
* </table.AppTable>
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
456
|
+
AppTable: AppTableComponent<TFeatures>
|
|
457
|
+
/**
|
|
458
|
+
* Wraps a cell and provides cell context with pre-bound cellComponents.
|
|
459
|
+
* Optionally accepts a selector for Subscribe functionality.
|
|
460
|
+
* @example
|
|
461
|
+
* ```tsx
|
|
462
|
+
* // Without selector
|
|
463
|
+
* <table.AppCell cell={cell}>
|
|
464
|
+
* {(c) => <td><c.TextCell /></td>}
|
|
465
|
+
* </table.AppCell>
|
|
466
|
+
*
|
|
467
|
+
* // With selector - children receives cell and selected state
|
|
468
|
+
* <table.AppCell cell={cell} selector={(s) => s.columnFilters}>
|
|
469
|
+
* {(c, filters) => <td>{filters.length}</td>}
|
|
470
|
+
* </table.AppCell>
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
473
|
+
AppCell: AppCellComponent<TFeatures, TData, NoInfer<TCellComponents>>
|
|
474
|
+
/**
|
|
475
|
+
* Wraps a header and provides header context with pre-bound headerComponents.
|
|
476
|
+
* Optionally accepts a selector for Subscribe functionality.
|
|
477
|
+
* @example
|
|
478
|
+
* ```tsx
|
|
479
|
+
* // Without selector
|
|
480
|
+
* <table.AppHeader header={header}>
|
|
481
|
+
* {(h) => <th><h.SortIndicator /></th>}
|
|
482
|
+
* </table.AppHeader>
|
|
483
|
+
*
|
|
484
|
+
* // With selector
|
|
485
|
+
* <table.AppHeader header={header} selector={(s) => s.sorting}>
|
|
486
|
+
* {(h, sorting) => <th>{sorting.length} sorted</th>}
|
|
487
|
+
* </table.AppHeader>
|
|
488
|
+
* ```
|
|
489
|
+
*/
|
|
490
|
+
AppHeader: AppHeaderComponent<TFeatures, TData, NoInfer<THeaderComponents>>
|
|
491
|
+
/**
|
|
492
|
+
* Wraps a footer and provides header context with pre-bound headerComponents.
|
|
493
|
+
* Optionally accepts a selector for Subscribe functionality.
|
|
494
|
+
* @example
|
|
495
|
+
* ```tsx
|
|
496
|
+
* <table.AppFooter header={footer}>
|
|
497
|
+
* {(f) => <td><table.FlexRender footer={footer} /></td>}
|
|
498
|
+
* </table.AppFooter>
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
AppFooter: AppHeaderComponent<TFeatures, TData, NoInfer<THeaderComponents>>
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Creates a custom table hook with pre-bound components for composition.
|
|
506
|
+
*
|
|
507
|
+
* This is the table equivalent of TanStack Form's `createFormHook`. It allows you to:
|
|
508
|
+
* - Define features, row models, and default options once, shared across all tables
|
|
509
|
+
* - Register reusable table, cell, and header components
|
|
510
|
+
* - Access table/cell/header instances via context in those components
|
|
511
|
+
* - Get a `useAppTable` hook that returns an extended table with App wrapper components
|
|
512
|
+
* - Get a `createAppColumnHelper` function pre-bound to your features
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* ```tsx
|
|
516
|
+
* // hooks/table.ts
|
|
517
|
+
* export const {
|
|
518
|
+
* useAppTable,
|
|
519
|
+
* createAppColumnHelper,
|
|
520
|
+
* useTableContext,
|
|
521
|
+
* useCellContext,
|
|
522
|
+
* useHeaderContext,
|
|
523
|
+
* } = createTableHook({
|
|
524
|
+
* _features: tableFeatures({
|
|
525
|
+
* rowPaginationFeature,
|
|
526
|
+
* rowSortingFeature,
|
|
527
|
+
* columnFilteringFeature,
|
|
528
|
+
* }),
|
|
529
|
+
* _rowModels: {
|
|
530
|
+
* paginatedRowModel: createPaginatedRowModel(),
|
|
531
|
+
* sortedRowModel: createSortedRowModel(sortFns),
|
|
532
|
+
* filteredRowModel: createFilteredRowModel(filterFns),
|
|
533
|
+
* },
|
|
534
|
+
* tableComponents: { PaginationControls, RowCount },
|
|
535
|
+
* cellComponents: { TextCell, NumberCell },
|
|
536
|
+
* headerComponents: { SortIndicator, ColumnFilter },
|
|
537
|
+
* })
|
|
538
|
+
*
|
|
539
|
+
* // Create column helper with TFeatures already bound
|
|
540
|
+
* const columnHelper = createAppColumnHelper<Person>()
|
|
541
|
+
*
|
|
542
|
+
* // components/table-components.tsx
|
|
543
|
+
* function PaginationControls() {
|
|
544
|
+
* const table = useTableContext() // TFeatures already known!
|
|
545
|
+
* return <table.Subscribe selector={(s) => s.pagination}>...</table.Subscribe>
|
|
546
|
+
* }
|
|
547
|
+
*
|
|
548
|
+
* // features/users.tsx
|
|
549
|
+
* function UsersTable({ data }: { data: Person[] }) {
|
|
550
|
+
* const table = useAppTable({
|
|
551
|
+
* columns,
|
|
552
|
+
* data, // TData inferred from Person[]
|
|
553
|
+
* })
|
|
554
|
+
*
|
|
555
|
+
* return (
|
|
556
|
+
* <table.AppTable>
|
|
557
|
+
* <table>
|
|
558
|
+
* <thead>
|
|
559
|
+
* {table.getHeaderGroups().map(headerGroup => (
|
|
560
|
+
* <tr key={headerGroup.id}>
|
|
561
|
+
* {headerGroup.headers.map(h => (
|
|
562
|
+
* <table.AppHeader header={h} key={h.id}>
|
|
563
|
+
* {(header) => (
|
|
564
|
+
* <th>
|
|
565
|
+
* <table.FlexRender header={h} />
|
|
566
|
+
* <header.SortIndicator />
|
|
567
|
+
* </th>
|
|
568
|
+
* )}
|
|
569
|
+
* </table.AppHeader>
|
|
570
|
+
* ))}
|
|
571
|
+
* </tr>
|
|
572
|
+
* ))}
|
|
573
|
+
* </thead>
|
|
574
|
+
* <tbody>
|
|
575
|
+
* {table.getRowModel().rows.map(row => (
|
|
576
|
+
* <tr key={row.id}>
|
|
577
|
+
* {row.getAllCells().map(c => (
|
|
578
|
+
* <table.AppCell cell={c} key={c.id}>
|
|
579
|
+
* {(cell) => <td><cell.TextCell /></td>}
|
|
580
|
+
* </table.AppCell>
|
|
581
|
+
* ))}
|
|
582
|
+
* </tr>
|
|
583
|
+
* ))}
|
|
584
|
+
* </tbody>
|
|
585
|
+
* </table>
|
|
586
|
+
* <table.PaginationControls />
|
|
587
|
+
* </table.AppTable>
|
|
588
|
+
* )
|
|
589
|
+
* }
|
|
590
|
+
* ```
|
|
591
|
+
*/
|
|
592
|
+
export function createTableHook<
|
|
593
|
+
TFeatures extends TableFeatures,
|
|
594
|
+
const TTableComponents extends Record<string, ComponentType<any>>,
|
|
595
|
+
const TCellComponents extends Record<string, ComponentType<any>>,
|
|
596
|
+
const THeaderComponents extends Record<string, ComponentType<any>>,
|
|
597
|
+
>({
|
|
598
|
+
tableComponents,
|
|
599
|
+
cellComponents,
|
|
600
|
+
headerComponents,
|
|
601
|
+
...defaultTableOptions
|
|
602
|
+
}: CreateTableHookOptions<
|
|
603
|
+
TFeatures,
|
|
604
|
+
TTableComponents,
|
|
605
|
+
TCellComponents,
|
|
606
|
+
THeaderComponents
|
|
607
|
+
>) {
|
|
608
|
+
// Create contexts internally with TFeatures baked in
|
|
609
|
+
const TableContext = createContext<PreactTable<TFeatures, any, any>>(
|
|
610
|
+
null as never,
|
|
611
|
+
)
|
|
612
|
+
const CellContext = createContext<Cell<TFeatures, any, any>>(null as never)
|
|
613
|
+
const HeaderContext = createContext<Header<TFeatures, any, any>>(
|
|
614
|
+
null as never,
|
|
615
|
+
)
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Create a column helper pre-bound to the features and components configured in this table hook.
|
|
619
|
+
* The cell, header, and footer contexts include pre-bound components (e.g., `cell.TextCell`).
|
|
620
|
+
* @example
|
|
621
|
+
* ```tsx
|
|
622
|
+
* const columnHelper = createAppColumnHelper<Person>()
|
|
623
|
+
*
|
|
624
|
+
* const columns = [
|
|
625
|
+
* columnHelper.accessor('firstName', {
|
|
626
|
+
* header: 'First Name',
|
|
627
|
+
* cell: ({ cell }) => <cell.TextCell />, // cell has pre-bound components!
|
|
628
|
+
* }),
|
|
629
|
+
* columnHelper.accessor('age', {
|
|
630
|
+
* header: 'Age',
|
|
631
|
+
* cell: ({ cell }) => <cell.NumberCell />,
|
|
632
|
+
* }),
|
|
633
|
+
* ]
|
|
634
|
+
* ```
|
|
635
|
+
*/
|
|
636
|
+
function createAppColumnHelper<TData extends RowData>(): AppColumnHelper<
|
|
637
|
+
TFeatures,
|
|
638
|
+
TData,
|
|
639
|
+
TCellComponents,
|
|
640
|
+
THeaderComponents
|
|
641
|
+
> {
|
|
642
|
+
// The runtime implementation is the same - components are attached at render time
|
|
643
|
+
// This cast provides the enhanced types for column definitions
|
|
644
|
+
return coreCreateColumnHelper<TFeatures, TData>() as AppColumnHelper<
|
|
645
|
+
TFeatures,
|
|
646
|
+
TData,
|
|
647
|
+
TCellComponents,
|
|
648
|
+
THeaderComponents
|
|
649
|
+
>
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Access the table instance from within an `AppTable` wrapper.
|
|
654
|
+
* Use this in custom `tableComponents` passed to `createTableHook`.
|
|
655
|
+
* TFeatures is already known from the createTableHook call.
|
|
656
|
+
*
|
|
657
|
+
* @example
|
|
658
|
+
* ```tsx
|
|
659
|
+
* function PaginationControls() {
|
|
660
|
+
* const table = useTableContext()
|
|
661
|
+
* return (
|
|
662
|
+
* <table.Subscribe selector={(s) => s.pagination}>
|
|
663
|
+
* {(pagination) => (
|
|
664
|
+
* <div>
|
|
665
|
+
* <button onClick={() => table.previousPage()}>Prev</button>
|
|
666
|
+
* <span>Page {pagination.pageIndex + 1}</span>
|
|
667
|
+
* <button onClick={() => table.nextPage()}>Next</button>
|
|
668
|
+
* </div>
|
|
669
|
+
* )}
|
|
670
|
+
* </table.Subscribe>
|
|
671
|
+
* )
|
|
672
|
+
* }
|
|
673
|
+
* ```
|
|
674
|
+
*/
|
|
675
|
+
function useTableContext<TData extends RowData = RowData>(): PreactTable<
|
|
676
|
+
TFeatures,
|
|
677
|
+
TData
|
|
678
|
+
> {
|
|
679
|
+
const table = useContext(TableContext)
|
|
680
|
+
|
|
681
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
682
|
+
if (!table) {
|
|
683
|
+
throw new Error(
|
|
684
|
+
'`useTableContext` must be used within an `AppTable` component. ' +
|
|
685
|
+
'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.',
|
|
686
|
+
)
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
return table as PreactTable<TFeatures, TData>
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Access the cell instance from within an `AppCell` wrapper.
|
|
694
|
+
* Use this in custom `cellComponents` passed to `createTableHook`.
|
|
695
|
+
* TFeatures is already known from the createTableHook call.
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```tsx
|
|
699
|
+
* function TextCell() {
|
|
700
|
+
* const cell = useCellContext<string>()
|
|
701
|
+
* return <span>{cell.getValue()}</span>
|
|
702
|
+
* }
|
|
703
|
+
*
|
|
704
|
+
* function NumberCell({ format }: { format?: Intl.NumberFormatOptions }) {
|
|
705
|
+
* const cell = useCellContext<number>()
|
|
706
|
+
* return <span>{cell.getValue().toLocaleString(undefined, format)}</span>
|
|
707
|
+
* }
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
function useCellContext<TValue extends CellData = CellData>() {
|
|
711
|
+
const cell = useContext(CellContext)
|
|
712
|
+
|
|
713
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
714
|
+
if (!cell) {
|
|
715
|
+
throw new Error(
|
|
716
|
+
'`useCellContext` must be used within an `AppCell` component. ' +
|
|
717
|
+
'Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.',
|
|
718
|
+
)
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
return cell as Cell<TFeatures, any, TValue>
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Access the header instance from within an `AppHeader` or `AppFooter` wrapper.
|
|
726
|
+
* Use this in custom `headerComponents` passed to `createTableHook`.
|
|
727
|
+
* TFeatures is already known from the createTableHook call.
|
|
728
|
+
*
|
|
729
|
+
* @example
|
|
730
|
+
* ```tsx
|
|
731
|
+
* function SortIndicator() {
|
|
732
|
+
* const header = useHeaderContext()
|
|
733
|
+
* const sorted = header.column.getIsSorted()
|
|
734
|
+
* return sorted === 'asc' ? '🔼' : sorted === 'desc' ? '🔽' : null
|
|
735
|
+
* }
|
|
736
|
+
*
|
|
737
|
+
* function ColumnFilter() {
|
|
738
|
+
* const header = useHeaderContext()
|
|
739
|
+
* if (!header.column.getCanFilter()) return null
|
|
740
|
+
* return (
|
|
741
|
+
* <input
|
|
742
|
+
* value={(header.column.getFilterValue() ?? '') as string}
|
|
743
|
+
* onChange={(e) => header.column.setFilterValue(e.target.value)}
|
|
744
|
+
* placeholder="Filter..."
|
|
745
|
+
* />
|
|
746
|
+
* )
|
|
747
|
+
* }
|
|
748
|
+
* ```
|
|
749
|
+
*/
|
|
750
|
+
function useHeaderContext<TValue extends CellData = CellData>() {
|
|
751
|
+
const header = useContext(HeaderContext)
|
|
752
|
+
|
|
753
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
754
|
+
if (!header) {
|
|
755
|
+
throw new Error(
|
|
756
|
+
'`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.',
|
|
757
|
+
)
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
return header as Header<TFeatures, any, TValue>
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Context-aware FlexRender component for cells.
|
|
765
|
+
* Uses the cell from context, so no need to pass cell prop.
|
|
766
|
+
*/
|
|
767
|
+
function CellFlexRender() {
|
|
768
|
+
const cell = useCellContext()
|
|
769
|
+
return <FlexRender cell={cell} />
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Context-aware FlexRender component for headers.
|
|
774
|
+
* Uses the header from context, so no need to pass header prop.
|
|
775
|
+
*/
|
|
776
|
+
function HeaderFlexRender() {
|
|
777
|
+
const header = useHeaderContext()
|
|
778
|
+
return <FlexRender header={header} />
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Context-aware FlexRender component for footers.
|
|
783
|
+
* Uses the header from context, so no need to pass footer prop.
|
|
784
|
+
*/
|
|
785
|
+
function FooterFlexRender() {
|
|
786
|
+
const header = useHeaderContext()
|
|
787
|
+
return <FlexRender footer={header} />
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Enhanced useTable hook that returns a table with App wrapper components
|
|
792
|
+
* and pre-bound tableComponents attached directly to the table object.
|
|
793
|
+
*
|
|
794
|
+
* Default options from createTableHook are automatically merged with
|
|
795
|
+
* the options passed here. Options passed here take precedence.
|
|
796
|
+
*
|
|
797
|
+
* TFeatures is already known from the createTableHook call; TData is inferred from the data prop.
|
|
798
|
+
*/
|
|
799
|
+
function useAppTable<TData extends RowData, TSelected = {}>(
|
|
800
|
+
tableOptions: Omit<
|
|
801
|
+
TableOptions<TFeatures, TData>,
|
|
802
|
+
'_features' | '_rowModels'
|
|
803
|
+
>,
|
|
804
|
+
selector?: (state: TableState<TFeatures>) => TSelected,
|
|
805
|
+
): AppPreactTable<
|
|
806
|
+
TFeatures,
|
|
807
|
+
TData,
|
|
808
|
+
TSelected,
|
|
809
|
+
TTableComponents,
|
|
810
|
+
TCellComponents,
|
|
811
|
+
THeaderComponents
|
|
812
|
+
> {
|
|
813
|
+
// Merge default options with provided options (provided takes precedence)
|
|
814
|
+
const table = useTable<TFeatures, TData, TSelected>(
|
|
815
|
+
{ ...defaultTableOptions, ...tableOptions } as TableOptions<
|
|
816
|
+
TFeatures,
|
|
817
|
+
TData
|
|
818
|
+
>,
|
|
819
|
+
selector,
|
|
820
|
+
)
|
|
821
|
+
|
|
822
|
+
// AppTable - Root wrapper that provides table context with optional Subscribe
|
|
823
|
+
const AppTable = useMemo(() => {
|
|
824
|
+
function AppTableImpl(
|
|
825
|
+
props: AppTablePropsWithoutSelector,
|
|
826
|
+
): ComponentChildren
|
|
827
|
+
function AppTableImpl<TAppTableSelected>(
|
|
828
|
+
props: AppTablePropsWithSelector<TFeatures, TAppTableSelected>,
|
|
829
|
+
): ComponentChildren
|
|
830
|
+
function AppTableImpl<TAppTableSelected>(
|
|
831
|
+
props:
|
|
832
|
+
| AppTablePropsWithoutSelector
|
|
833
|
+
| AppTablePropsWithSelector<TFeatures, TAppTableSelected>,
|
|
834
|
+
): ComponentChildren {
|
|
835
|
+
const { children, selector: appTableSelector } = props as any
|
|
836
|
+
|
|
837
|
+
return (
|
|
838
|
+
<TableContext.Provider value={table}>
|
|
839
|
+
{appTableSelector ? (
|
|
840
|
+
<table.Subscribe selector={appTableSelector}>
|
|
841
|
+
{(state: TAppTableSelected) =>
|
|
842
|
+
(children as (state: TAppTableSelected) => ComponentChildren)(
|
|
843
|
+
state,
|
|
844
|
+
)
|
|
845
|
+
}
|
|
846
|
+
</table.Subscribe>
|
|
847
|
+
) : (
|
|
848
|
+
children
|
|
849
|
+
)}
|
|
850
|
+
</TableContext.Provider>
|
|
851
|
+
)
|
|
852
|
+
}
|
|
853
|
+
return AppTableImpl as AppTableComponent<TFeatures>
|
|
854
|
+
}, [table])
|
|
855
|
+
|
|
856
|
+
// AppCell - Wraps cell with context, pre-bound cellComponents, and optional Subscribe
|
|
857
|
+
const AppCell = useMemo(() => {
|
|
858
|
+
function AppCellImpl<TValue extends CellData = CellData>(
|
|
859
|
+
props: AppCellPropsWithoutSelector<
|
|
860
|
+
TFeatures,
|
|
861
|
+
TData,
|
|
862
|
+
TValue,
|
|
863
|
+
TCellComponents
|
|
864
|
+
>,
|
|
865
|
+
): ComponentChildren
|
|
866
|
+
function AppCellImpl<
|
|
867
|
+
TValue extends CellData = CellData,
|
|
868
|
+
TAppCellSelected = unknown,
|
|
869
|
+
>(
|
|
870
|
+
props: AppCellPropsWithSelector<
|
|
871
|
+
TFeatures,
|
|
872
|
+
TData,
|
|
873
|
+
TValue,
|
|
874
|
+
TCellComponents,
|
|
875
|
+
TAppCellSelected
|
|
876
|
+
>,
|
|
877
|
+
): ComponentChildren
|
|
878
|
+
function AppCellImpl<
|
|
879
|
+
TValue extends CellData = CellData,
|
|
880
|
+
TAppCellSelected = unknown,
|
|
881
|
+
>(
|
|
882
|
+
props:
|
|
883
|
+
| AppCellPropsWithoutSelector<
|
|
884
|
+
TFeatures,
|
|
885
|
+
TData,
|
|
886
|
+
TValue,
|
|
887
|
+
TCellComponents
|
|
888
|
+
>
|
|
889
|
+
| AppCellPropsWithSelector<
|
|
890
|
+
TFeatures,
|
|
891
|
+
TData,
|
|
892
|
+
TValue,
|
|
893
|
+
TCellComponents,
|
|
894
|
+
TAppCellSelected
|
|
895
|
+
>,
|
|
896
|
+
): ComponentChildren {
|
|
897
|
+
const { cell, children, selector: appCellSelector } = props as any
|
|
898
|
+
const extendedCell = Object.assign(cell, {
|
|
899
|
+
FlexRender: CellFlexRender,
|
|
900
|
+
...cellComponents,
|
|
901
|
+
})
|
|
902
|
+
|
|
903
|
+
return (
|
|
904
|
+
<CellContext.Provider value={cell}>
|
|
905
|
+
{appCellSelector ? (
|
|
906
|
+
<table.Subscribe selector={appCellSelector}>
|
|
907
|
+
{(state: TAppCellSelected) =>
|
|
908
|
+
(
|
|
909
|
+
children as (
|
|
910
|
+
cell: Cell<TFeatures, TData, TValue> &
|
|
911
|
+
TCellComponents & {
|
|
912
|
+
FlexRender: () => ComponentChildren
|
|
913
|
+
},
|
|
914
|
+
state: TAppCellSelected,
|
|
915
|
+
) => ComponentChildren
|
|
916
|
+
)(extendedCell, state)
|
|
917
|
+
}
|
|
918
|
+
</table.Subscribe>
|
|
919
|
+
) : (
|
|
920
|
+
(
|
|
921
|
+
children as (
|
|
922
|
+
cell: Cell<TFeatures, TData, TValue> &
|
|
923
|
+
TCellComponents & { FlexRender: () => ComponentChildren },
|
|
924
|
+
) => ComponentChildren
|
|
925
|
+
)(extendedCell)
|
|
926
|
+
)}
|
|
927
|
+
</CellContext.Provider>
|
|
928
|
+
)
|
|
929
|
+
}
|
|
930
|
+
return AppCellImpl as AppCellComponent<TFeatures, TData, TCellComponents>
|
|
931
|
+
}, [table])
|
|
932
|
+
|
|
933
|
+
// AppHeader - Wraps header with context, pre-bound headerComponents, and optional Subscribe
|
|
934
|
+
const AppHeader = useMemo(() => {
|
|
935
|
+
function AppHeaderImpl<TValue extends CellData = CellData>(
|
|
936
|
+
props: AppHeaderPropsWithoutSelector<
|
|
937
|
+
TFeatures,
|
|
938
|
+
TData,
|
|
939
|
+
TValue,
|
|
940
|
+
THeaderComponents
|
|
941
|
+
>,
|
|
942
|
+
): ComponentChildren
|
|
943
|
+
function AppHeaderImpl<
|
|
944
|
+
TValue extends CellData = CellData,
|
|
945
|
+
TAppHeaderSelected = unknown,
|
|
946
|
+
>(
|
|
947
|
+
props: AppHeaderPropsWithSelector<
|
|
948
|
+
TFeatures,
|
|
949
|
+
TData,
|
|
950
|
+
TValue,
|
|
951
|
+
THeaderComponents,
|
|
952
|
+
TAppHeaderSelected
|
|
953
|
+
>,
|
|
954
|
+
): ComponentChildren
|
|
955
|
+
function AppHeaderImpl<
|
|
956
|
+
TValue extends CellData = CellData,
|
|
957
|
+
TAppHeaderSelected = unknown,
|
|
958
|
+
>(
|
|
959
|
+
props:
|
|
960
|
+
| AppHeaderPropsWithoutSelector<
|
|
961
|
+
TFeatures,
|
|
962
|
+
TData,
|
|
963
|
+
TValue,
|
|
964
|
+
THeaderComponents
|
|
965
|
+
>
|
|
966
|
+
| AppHeaderPropsWithSelector<
|
|
967
|
+
TFeatures,
|
|
968
|
+
TData,
|
|
969
|
+
TValue,
|
|
970
|
+
THeaderComponents,
|
|
971
|
+
TAppHeaderSelected
|
|
972
|
+
>,
|
|
973
|
+
): ComponentChildren {
|
|
974
|
+
const { header, children, selector: appHeaderSelector } = props as any
|
|
975
|
+
const extendedHeader = Object.assign(header, {
|
|
976
|
+
FlexRender: HeaderFlexRender,
|
|
977
|
+
...headerComponents,
|
|
978
|
+
})
|
|
979
|
+
|
|
980
|
+
return (
|
|
981
|
+
<HeaderContext.Provider value={header}>
|
|
982
|
+
{appHeaderSelector ? (
|
|
983
|
+
<table.Subscribe selector={appHeaderSelector}>
|
|
984
|
+
{(state: TAppHeaderSelected) =>
|
|
985
|
+
(
|
|
986
|
+
children as (
|
|
987
|
+
header: Header<TFeatures, TData, TValue> &
|
|
988
|
+
THeaderComponents,
|
|
989
|
+
state: TAppHeaderSelected,
|
|
990
|
+
) => ComponentChildren
|
|
991
|
+
)(extendedHeader, state)
|
|
992
|
+
}
|
|
993
|
+
</table.Subscribe>
|
|
994
|
+
) : (
|
|
995
|
+
(
|
|
996
|
+
children as (
|
|
997
|
+
header: Header<TFeatures, TData, TValue> &
|
|
998
|
+
THeaderComponents & { FlexRender: () => ComponentChildren },
|
|
999
|
+
) => ComponentChildren
|
|
1000
|
+
)(extendedHeader)
|
|
1001
|
+
)}
|
|
1002
|
+
</HeaderContext.Provider>
|
|
1003
|
+
)
|
|
1004
|
+
}
|
|
1005
|
+
return AppHeaderImpl as AppHeaderComponent<
|
|
1006
|
+
TFeatures,
|
|
1007
|
+
TData,
|
|
1008
|
+
THeaderComponents
|
|
1009
|
+
>
|
|
1010
|
+
}, [table])
|
|
1011
|
+
|
|
1012
|
+
// AppFooter - Same as AppHeader (footers use Header type)
|
|
1013
|
+
const AppFooter = useMemo(() => {
|
|
1014
|
+
function AppFooterImpl<TValue extends CellData = CellData>(
|
|
1015
|
+
props: AppHeaderPropsWithoutSelector<
|
|
1016
|
+
TFeatures,
|
|
1017
|
+
TData,
|
|
1018
|
+
TValue,
|
|
1019
|
+
THeaderComponents
|
|
1020
|
+
>,
|
|
1021
|
+
): ComponentChildren
|
|
1022
|
+
function AppFooterImpl<
|
|
1023
|
+
TValue extends CellData = CellData,
|
|
1024
|
+
TAppFooterSelected = unknown,
|
|
1025
|
+
>(
|
|
1026
|
+
props: AppHeaderPropsWithSelector<
|
|
1027
|
+
TFeatures,
|
|
1028
|
+
TData,
|
|
1029
|
+
TValue,
|
|
1030
|
+
THeaderComponents,
|
|
1031
|
+
TAppFooterSelected
|
|
1032
|
+
>,
|
|
1033
|
+
): ComponentChildren
|
|
1034
|
+
function AppFooterImpl<
|
|
1035
|
+
TValue extends CellData = CellData,
|
|
1036
|
+
TAppFooterSelected = unknown,
|
|
1037
|
+
>(
|
|
1038
|
+
props:
|
|
1039
|
+
| AppHeaderPropsWithoutSelector<
|
|
1040
|
+
TFeatures,
|
|
1041
|
+
TData,
|
|
1042
|
+
TValue,
|
|
1043
|
+
THeaderComponents
|
|
1044
|
+
>
|
|
1045
|
+
| AppHeaderPropsWithSelector<
|
|
1046
|
+
TFeatures,
|
|
1047
|
+
TData,
|
|
1048
|
+
TValue,
|
|
1049
|
+
THeaderComponents,
|
|
1050
|
+
TAppFooterSelected
|
|
1051
|
+
>,
|
|
1052
|
+
): ComponentChildren {
|
|
1053
|
+
const { header, children, selector: appFooterSelector } = props as any
|
|
1054
|
+
const extendedHeader = Object.assign(header, {
|
|
1055
|
+
FlexRender: FooterFlexRender,
|
|
1056
|
+
...headerComponents,
|
|
1057
|
+
})
|
|
1058
|
+
|
|
1059
|
+
return (
|
|
1060
|
+
<HeaderContext.Provider value={header}>
|
|
1061
|
+
{appFooterSelector ? (
|
|
1062
|
+
<table.Subscribe selector={appFooterSelector}>
|
|
1063
|
+
{(state: TAppFooterSelected) =>
|
|
1064
|
+
(
|
|
1065
|
+
children as (
|
|
1066
|
+
header: Header<TFeatures, TData, TValue> &
|
|
1067
|
+
THeaderComponents,
|
|
1068
|
+
state: TAppFooterSelected,
|
|
1069
|
+
) => ComponentChildren
|
|
1070
|
+
)(extendedHeader, state)
|
|
1071
|
+
}
|
|
1072
|
+
</table.Subscribe>
|
|
1073
|
+
) : (
|
|
1074
|
+
(
|
|
1075
|
+
children as (
|
|
1076
|
+
header: Header<TFeatures, TData, TValue> &
|
|
1077
|
+
THeaderComponents & { FlexRender: () => ComponentChildren },
|
|
1078
|
+
) => ComponentChildren
|
|
1079
|
+
)(extendedHeader)
|
|
1080
|
+
)}
|
|
1081
|
+
</HeaderContext.Provider>
|
|
1082
|
+
)
|
|
1083
|
+
}
|
|
1084
|
+
return AppFooterImpl as AppHeaderComponent<
|
|
1085
|
+
TFeatures,
|
|
1086
|
+
TData,
|
|
1087
|
+
THeaderComponents
|
|
1088
|
+
>
|
|
1089
|
+
}, [table])
|
|
1090
|
+
|
|
1091
|
+
// Combine everything into the extended table API
|
|
1092
|
+
const extendedTable = useMemo(() => {
|
|
1093
|
+
return Object.assign(table, {
|
|
1094
|
+
AppTable,
|
|
1095
|
+
AppCell,
|
|
1096
|
+
AppHeader,
|
|
1097
|
+
AppFooter,
|
|
1098
|
+
...tableComponents,
|
|
1099
|
+
}) as AppPreactTable<
|
|
1100
|
+
TFeatures,
|
|
1101
|
+
TData,
|
|
1102
|
+
TSelected,
|
|
1103
|
+
TTableComponents,
|
|
1104
|
+
TCellComponents,
|
|
1105
|
+
THeaderComponents
|
|
1106
|
+
>
|
|
1107
|
+
}, [table, AppTable, AppCell, AppHeader, AppFooter])
|
|
1108
|
+
|
|
1109
|
+
return extendedTable
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
return {
|
|
1113
|
+
appFeatures: defaultTableOptions._features as TFeatures,
|
|
1114
|
+
createAppColumnHelper,
|
|
1115
|
+
useAppTable,
|
|
1116
|
+
useTableContext,
|
|
1117
|
+
useCellContext,
|
|
1118
|
+
useHeaderContext,
|
|
1119
|
+
}
|
|
1120
|
+
}
|