@tanstack/react-table 9.0.0-alpha.10 → 9.0.0-alpha.12

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.
@@ -0,0 +1,1112 @@
1
+ 'use client'
2
+ /* eslint-disable @eslint-react/no-context-provider */
3
+ import React, { createContext, use, useMemo } from 'react'
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 { ComponentType, ReactNode } from 'react'
31
+ import type { ReactTable } 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: () => ReactNode }
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: () => ReactNode }
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: ReactNode
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) => ReactNode
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: () => ReactNode },
306
+ ) => ReactNode
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: () => ReactNode },
324
+ state: TSelected,
325
+ ) => ReactNode
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: () => ReactNode },
342
+ ) => ReactNode
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: () => ReactNode },
360
+ state: TSelected,
361
+ ) => ReactNode
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
+ ): ReactNode
381
+ <TValue extends CellData = CellData, TSelected = unknown>(
382
+ props: AppCellPropsWithSelector<
383
+ TFeatures,
384
+ TData,
385
+ TValue,
386
+ TCellComponents,
387
+ TSelected
388
+ >,
389
+ ): ReactNode
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
+ ): ReactNode
408
+ <TValue extends CellData = CellData, TSelected = unknown>(
409
+ props: AppHeaderPropsWithSelector<
410
+ TFeatures,
411
+ TData,
412
+ TValue,
413
+ THeaderComponents,
414
+ TSelected
415
+ >,
416
+ ): ReactNode
417
+ }
418
+
419
+ /**
420
+ * Component type for AppTable - root wrapper with optional Subscribe
421
+ */
422
+ export interface AppTableComponent<TFeatures extends TableFeatures> {
423
+ (props: AppTablePropsWithoutSelector): ReactNode
424
+ <TSelected>(props: AppTablePropsWithSelector<TFeatures, TSelected>): ReactNode
425
+ }
426
+
427
+ /**
428
+ * Extended table API returned by useAppTable with all App wrapper components
429
+ */
430
+ export type AppReactTable<
431
+ TFeatures extends TableFeatures,
432
+ TData extends RowData,
433
+ TSelected,
434
+ TTableComponents extends Record<string, ComponentType<any>>,
435
+ TCellComponents extends Record<string, ComponentType<any>>,
436
+ THeaderComponents extends Record<string, ComponentType<any>>,
437
+ > = ReactTable<TFeatures, TData, TSelected> &
438
+ NoInfer<TTableComponents> & {
439
+ /**
440
+ * Root wrapper component that provides table context with optional Subscribe.
441
+ * @example
442
+ * ```tsx
443
+ * // Without selector - children is ReactNode
444
+ * <table.AppTable>
445
+ * <table>...</table>
446
+ * </table.AppTable>
447
+ *
448
+ * // With selector - children receives selected state
449
+ * <table.AppTable selector={(s) => s.pagination}>
450
+ * {(pagination) => <div>Page {pagination.pageIndex}</div>}
451
+ * </table.AppTable>
452
+ * ```
453
+ */
454
+ AppTable: AppTableComponent<TFeatures>
455
+ /**
456
+ * Wraps a cell and provides cell context with pre-bound cellComponents.
457
+ * Optionally accepts a selector for Subscribe functionality.
458
+ * @example
459
+ * ```tsx
460
+ * // Without selector
461
+ * <table.AppCell cell={cell}>
462
+ * {(c) => <td><c.TextCell /></td>}
463
+ * </table.AppCell>
464
+ *
465
+ * // With selector - children receives cell and selected state
466
+ * <table.AppCell cell={cell} selector={(s) => s.columnFilters}>
467
+ * {(c, filters) => <td>{filters.length}</td>}
468
+ * </table.AppCell>
469
+ * ```
470
+ */
471
+ AppCell: AppCellComponent<TFeatures, TData, NoInfer<TCellComponents>>
472
+ /**
473
+ * Wraps a header and provides header context with pre-bound headerComponents.
474
+ * Optionally accepts a selector for Subscribe functionality.
475
+ * @example
476
+ * ```tsx
477
+ * // Without selector
478
+ * <table.AppHeader header={header}>
479
+ * {(h) => <th><h.SortIndicator /></th>}
480
+ * </table.AppHeader>
481
+ *
482
+ * // With selector
483
+ * <table.AppHeader header={header} selector={(s) => s.sorting}>
484
+ * {(h, sorting) => <th>{sorting.length} sorted</th>}
485
+ * </table.AppHeader>
486
+ * ```
487
+ */
488
+ AppHeader: AppHeaderComponent<TFeatures, TData, NoInfer<THeaderComponents>>
489
+ /**
490
+ * Wraps a footer and provides header context with pre-bound headerComponents.
491
+ * Optionally accepts a selector for Subscribe functionality.
492
+ * @example
493
+ * ```tsx
494
+ * <table.AppFooter header={footer}>
495
+ * {(f) => <td><table.FlexRender footer={footer} /></td>}
496
+ * </table.AppFooter>
497
+ * ```
498
+ */
499
+ AppFooter: AppHeaderComponent<TFeatures, TData, NoInfer<THeaderComponents>>
500
+ }
501
+
502
+ /**
503
+ * Creates a custom table hook with pre-bound components for composition.
504
+ *
505
+ * This is the table equivalent of TanStack Form's `createFormHook`. It allows you to:
506
+ * - Define features, row models, and default options once, shared across all tables
507
+ * - Register reusable table, cell, and header components
508
+ * - Access table/cell/header instances via context in those components
509
+ * - Get a `useAppTable` hook that returns an extended table with App wrapper components
510
+ * - Get a `createAppColumnHelper` function pre-bound to your features
511
+ *
512
+ * @example
513
+ * ```tsx
514
+ * // hooks/table.ts
515
+ * export const {
516
+ * useAppTable,
517
+ * createAppColumnHelper,
518
+ * useTableContext,
519
+ * useCellContext,
520
+ * useHeaderContext,
521
+ * } = createTableHook({
522
+ * _features: tableFeatures({
523
+ * rowPaginationFeature,
524
+ * rowSortingFeature,
525
+ * columnFilteringFeature,
526
+ * }),
527
+ * _rowModels: {
528
+ * paginatedRowModel: createPaginatedRowModel(),
529
+ * sortedRowModel: createSortedRowModel(sortFns),
530
+ * filteredRowModel: createFilteredRowModel(filterFns),
531
+ * },
532
+ * tableComponents: { PaginationControls, RowCount },
533
+ * cellComponents: { TextCell, NumberCell },
534
+ * headerComponents: { SortIndicator, ColumnFilter },
535
+ * })
536
+ *
537
+ * // Create column helper with TFeatures already bound
538
+ * const columnHelper = createAppColumnHelper<Person>()
539
+ *
540
+ * // components/table-components.tsx
541
+ * function PaginationControls() {
542
+ * const table = useTableContext() // TFeatures already known!
543
+ * return <table.Subscribe selector={(s) => s.pagination}>...</table.Subscribe>
544
+ * }
545
+ *
546
+ * // features/users.tsx
547
+ * function UsersTable({ data }: { data: Person[] }) {
548
+ * const table = useAppTable({
549
+ * columns,
550
+ * data, // TData inferred from Person[]
551
+ * })
552
+ *
553
+ * return (
554
+ * <table.AppTable>
555
+ * <table>
556
+ * <thead>
557
+ * {table.getHeaderGroups().map(headerGroup => (
558
+ * <tr key={headerGroup.id}>
559
+ * {headerGroup.headers.map(h => (
560
+ * <table.AppHeader header={h} key={h.id}>
561
+ * {(header) => (
562
+ * <th>
563
+ * <table.FlexRender header={h} />
564
+ * <header.SortIndicator />
565
+ * </th>
566
+ * )}
567
+ * </table.AppHeader>
568
+ * ))}
569
+ * </tr>
570
+ * ))}
571
+ * </thead>
572
+ * <tbody>
573
+ * {table.getRowModel().rows.map(row => (
574
+ * <tr key={row.id}>
575
+ * {row.getAllCells().map(c => (
576
+ * <table.AppCell cell={c} key={c.id}>
577
+ * {(cell) => <td><cell.TextCell /></td>}
578
+ * </table.AppCell>
579
+ * ))}
580
+ * </tr>
581
+ * ))}
582
+ * </tbody>
583
+ * </table>
584
+ * <table.PaginationControls />
585
+ * </table.AppTable>
586
+ * )
587
+ * }
588
+ * ```
589
+ */
590
+ export function createTableHook<
591
+ TFeatures extends TableFeatures,
592
+ const TTableComponents extends Record<string, ComponentType<any>>,
593
+ const TCellComponents extends Record<string, ComponentType<any>>,
594
+ const THeaderComponents extends Record<string, ComponentType<any>>,
595
+ >({
596
+ tableComponents,
597
+ cellComponents,
598
+ headerComponents,
599
+ ...defaultTableOptions
600
+ }: CreateTableHookOptions<
601
+ TFeatures,
602
+ TTableComponents,
603
+ TCellComponents,
604
+ THeaderComponents
605
+ >) {
606
+ // Create contexts internally with TFeatures baked in
607
+ const TableContext = createContext<ReactTable<TFeatures, any, any>>(
608
+ null as never,
609
+ )
610
+ const CellContext = createContext<Cell<TFeatures, any, any>>(null as never)
611
+ const HeaderContext = createContext<Header<TFeatures, any, any>>(
612
+ null as never,
613
+ )
614
+
615
+ /**
616
+ * Create a column helper pre-bound to the features and components configured in this table hook.
617
+ * The cell, header, and footer contexts include pre-bound components (e.g., `cell.TextCell`).
618
+ * @example
619
+ * ```tsx
620
+ * const columnHelper = createAppColumnHelper<Person>()
621
+ *
622
+ * const columns = [
623
+ * columnHelper.accessor('firstName', {
624
+ * header: 'First Name',
625
+ * cell: ({ cell }) => <cell.TextCell />, // cell has pre-bound components!
626
+ * }),
627
+ * columnHelper.accessor('age', {
628
+ * header: 'Age',
629
+ * cell: ({ cell }) => <cell.NumberCell />,
630
+ * }),
631
+ * ]
632
+ * ```
633
+ */
634
+ function createAppColumnHelper<TData extends RowData>(): AppColumnHelper<
635
+ TFeatures,
636
+ TData,
637
+ TCellComponents,
638
+ THeaderComponents
639
+ > {
640
+ // The runtime implementation is the same - components are attached at render time
641
+ // This cast provides the enhanced types for column definitions
642
+ return coreCreateColumnHelper<TFeatures, TData>() as AppColumnHelper<
643
+ TFeatures,
644
+ TData,
645
+ TCellComponents,
646
+ THeaderComponents
647
+ >
648
+ }
649
+
650
+ /**
651
+ * Access the table instance from within an `AppTable` wrapper.
652
+ * Use this in custom `tableComponents` passed to `createTableHook`.
653
+ * TFeatures is already known from the createTableHook call.
654
+ *
655
+ * @example
656
+ * ```tsx
657
+ * function PaginationControls() {
658
+ * const table = useTableContext()
659
+ * return (
660
+ * <table.Subscribe selector={(s) => s.pagination}>
661
+ * {(pagination) => (
662
+ * <div>
663
+ * <button onClick={() => table.previousPage()}>Prev</button>
664
+ * <span>Page {pagination.pageIndex + 1}</span>
665
+ * <button onClick={() => table.nextPage()}>Next</button>
666
+ * </div>
667
+ * )}
668
+ * </table.Subscribe>
669
+ * )
670
+ * }
671
+ * ```
672
+ */
673
+ function useTableContext<TData extends RowData = RowData>(): ReactTable<
674
+ TFeatures,
675
+ TData
676
+ > {
677
+ const table = use(TableContext)
678
+
679
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
680
+ if (!table) {
681
+ throw new Error(
682
+ '`useTableContext` must be used within an `AppTable` component. ' +
683
+ 'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.',
684
+ )
685
+ }
686
+
687
+ return table as ReactTable<TFeatures, TData>
688
+ }
689
+
690
+ /**
691
+ * Access the cell instance from within an `AppCell` wrapper.
692
+ * Use this in custom `cellComponents` passed to `createTableHook`.
693
+ * TFeatures is already known from the createTableHook call.
694
+ *
695
+ * @example
696
+ * ```tsx
697
+ * function TextCell() {
698
+ * const cell = useCellContext<string>()
699
+ * return <span>{cell.getValue()}</span>
700
+ * }
701
+ *
702
+ * function NumberCell({ format }: { format?: Intl.NumberFormatOptions }) {
703
+ * const cell = useCellContext<number>()
704
+ * return <span>{cell.getValue().toLocaleString(undefined, format)}</span>
705
+ * }
706
+ * ```
707
+ */
708
+ function useCellContext<TValue extends CellData = CellData>() {
709
+ const cell = use(CellContext)
710
+
711
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
712
+ if (!cell) {
713
+ throw new Error(
714
+ '`useCellContext` must be used within an `AppCell` component. ' +
715
+ 'Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.',
716
+ )
717
+ }
718
+
719
+ return cell as Cell<TFeatures, any, TValue>
720
+ }
721
+
722
+ /**
723
+ * Access the header instance from within an `AppHeader` or `AppFooter` wrapper.
724
+ * Use this in custom `headerComponents` passed to `createTableHook`.
725
+ * TFeatures is already known from the createTableHook call.
726
+ *
727
+ * @example
728
+ * ```tsx
729
+ * function SortIndicator() {
730
+ * const header = useHeaderContext()
731
+ * const sorted = header.column.getIsSorted()
732
+ * return sorted === 'asc' ? '🔼' : sorted === 'desc' ? '🔽' : null
733
+ * }
734
+ *
735
+ * function ColumnFilter() {
736
+ * const header = useHeaderContext()
737
+ * if (!header.column.getCanFilter()) return null
738
+ * return (
739
+ * <input
740
+ * value={(header.column.getFilterValue() ?? '') as string}
741
+ * onChange={(e) => header.column.setFilterValue(e.target.value)}
742
+ * placeholder="Filter..."
743
+ * />
744
+ * )
745
+ * }
746
+ * ```
747
+ */
748
+ function useHeaderContext<TValue extends CellData = CellData>() {
749
+ const header = use(HeaderContext)
750
+
751
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
752
+ if (!header) {
753
+ throw new Error(
754
+ '`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.',
755
+ )
756
+ }
757
+
758
+ return header as Header<TFeatures, any, TValue>
759
+ }
760
+
761
+ /**
762
+ * Context-aware FlexRender component for cells.
763
+ * Uses the cell from context, so no need to pass cell prop.
764
+ */
765
+ function CellFlexRender() {
766
+ const cell = useCellContext()
767
+ return <FlexRender cell={cell} />
768
+ }
769
+
770
+ /**
771
+ * Context-aware FlexRender component for headers.
772
+ * Uses the header from context, so no need to pass header prop.
773
+ */
774
+ function HeaderFlexRender() {
775
+ const header = useHeaderContext()
776
+ return <FlexRender header={header} />
777
+ }
778
+
779
+ /**
780
+ * Context-aware FlexRender component for footers.
781
+ * Uses the header from context, so no need to pass footer prop.
782
+ */
783
+ function FooterFlexRender() {
784
+ const header = useHeaderContext()
785
+ return <FlexRender footer={header} />
786
+ }
787
+
788
+ /**
789
+ * Enhanced useTable hook that returns a table with App wrapper components
790
+ * and pre-bound tableComponents attached directly to the table object.
791
+ *
792
+ * Default options from createTableHook are automatically merged with
793
+ * the options passed here. Options passed here take precedence.
794
+ *
795
+ * TFeatures is already known from the createTableHook call; TData is inferred from the data prop.
796
+ */
797
+ function useAppTable<TData extends RowData, TSelected = {}>(
798
+ tableOptions: Omit<
799
+ TableOptions<TFeatures, TData>,
800
+ '_features' | '_rowModels'
801
+ >,
802
+ selector?: (state: TableState<TFeatures>) => TSelected,
803
+ ): AppReactTable<
804
+ TFeatures,
805
+ TData,
806
+ TSelected,
807
+ TTableComponents,
808
+ TCellComponents,
809
+ THeaderComponents
810
+ > {
811
+ // Merge default options with provided options (provided takes precedence)
812
+ const table = useTable<TFeatures, TData, TSelected>(
813
+ { ...defaultTableOptions, ...tableOptions } as TableOptions<
814
+ TFeatures,
815
+ TData
816
+ >,
817
+ selector,
818
+ )
819
+
820
+ // AppTable - Root wrapper that provides table context with optional Subscribe
821
+ const AppTable = useMemo(() => {
822
+ function AppTableImpl(props: AppTablePropsWithoutSelector): ReactNode
823
+ function AppTableImpl<TAppTableSelected>(
824
+ props: AppTablePropsWithSelector<TFeatures, TAppTableSelected>,
825
+ ): ReactNode
826
+ function AppTableImpl<TAppTableSelected>(
827
+ props:
828
+ | AppTablePropsWithoutSelector
829
+ | AppTablePropsWithSelector<TFeatures, TAppTableSelected>,
830
+ ): ReactNode {
831
+ const { children, selector: appTableSelector } = props as any
832
+
833
+ return (
834
+ <TableContext.Provider value={table}>
835
+ {appTableSelector ? (
836
+ <table.Subscribe selector={appTableSelector}>
837
+ {(state: TAppTableSelected) =>
838
+ (children as (state: TAppTableSelected) => ReactNode)(state)
839
+ }
840
+ </table.Subscribe>
841
+ ) : (
842
+ children
843
+ )}
844
+ </TableContext.Provider>
845
+ )
846
+ }
847
+ return AppTableImpl as AppTableComponent<TFeatures>
848
+ }, [table])
849
+
850
+ // AppCell - Wraps cell with context, pre-bound cellComponents, and optional Subscribe
851
+ const AppCell = useMemo(() => {
852
+ function AppCellImpl<TValue extends CellData = CellData>(
853
+ props: AppCellPropsWithoutSelector<
854
+ TFeatures,
855
+ TData,
856
+ TValue,
857
+ TCellComponents
858
+ >,
859
+ ): ReactNode
860
+ function AppCellImpl<
861
+ TValue extends CellData = CellData,
862
+ TAppCellSelected = unknown,
863
+ >(
864
+ props: AppCellPropsWithSelector<
865
+ TFeatures,
866
+ TData,
867
+ TValue,
868
+ TCellComponents,
869
+ TAppCellSelected
870
+ >,
871
+ ): ReactNode
872
+ function AppCellImpl<
873
+ TValue extends CellData = CellData,
874
+ TAppCellSelected = unknown,
875
+ >(
876
+ props:
877
+ | AppCellPropsWithoutSelector<
878
+ TFeatures,
879
+ TData,
880
+ TValue,
881
+ TCellComponents
882
+ >
883
+ | AppCellPropsWithSelector<
884
+ TFeatures,
885
+ TData,
886
+ TValue,
887
+ TCellComponents,
888
+ TAppCellSelected
889
+ >,
890
+ ): ReactNode {
891
+ const { cell, children, selector: appCellSelector } = props as any
892
+ const extendedCell = Object.assign(cell, {
893
+ FlexRender: CellFlexRender,
894
+ ...cellComponents,
895
+ })
896
+
897
+ return (
898
+ <CellContext.Provider value={cell}>
899
+ {appCellSelector ? (
900
+ <table.Subscribe selector={appCellSelector}>
901
+ {(state: TAppCellSelected) =>
902
+ (
903
+ children as (
904
+ cell: Cell<TFeatures, TData, TValue> &
905
+ TCellComponents & { FlexRender: () => ReactNode },
906
+ state: TAppCellSelected,
907
+ ) => ReactNode
908
+ )(extendedCell, state)
909
+ }
910
+ </table.Subscribe>
911
+ ) : (
912
+ (
913
+ children as (
914
+ cell: Cell<TFeatures, TData, TValue> &
915
+ TCellComponents & { FlexRender: () => ReactNode },
916
+ ) => ReactNode
917
+ )(extendedCell)
918
+ )}
919
+ </CellContext.Provider>
920
+ )
921
+ }
922
+ return AppCellImpl as AppCellComponent<TFeatures, TData, TCellComponents>
923
+ }, [table])
924
+
925
+ // AppHeader - Wraps header with context, pre-bound headerComponents, and optional Subscribe
926
+ const AppHeader = useMemo(() => {
927
+ function AppHeaderImpl<TValue extends CellData = CellData>(
928
+ props: AppHeaderPropsWithoutSelector<
929
+ TFeatures,
930
+ TData,
931
+ TValue,
932
+ THeaderComponents
933
+ >,
934
+ ): ReactNode
935
+ function AppHeaderImpl<
936
+ TValue extends CellData = CellData,
937
+ TAppHeaderSelected = unknown,
938
+ >(
939
+ props: AppHeaderPropsWithSelector<
940
+ TFeatures,
941
+ TData,
942
+ TValue,
943
+ THeaderComponents,
944
+ TAppHeaderSelected
945
+ >,
946
+ ): ReactNode
947
+ function AppHeaderImpl<
948
+ TValue extends CellData = CellData,
949
+ TAppHeaderSelected = unknown,
950
+ >(
951
+ props:
952
+ | AppHeaderPropsWithoutSelector<
953
+ TFeatures,
954
+ TData,
955
+ TValue,
956
+ THeaderComponents
957
+ >
958
+ | AppHeaderPropsWithSelector<
959
+ TFeatures,
960
+ TData,
961
+ TValue,
962
+ THeaderComponents,
963
+ TAppHeaderSelected
964
+ >,
965
+ ): ReactNode {
966
+ const { header, children, selector: appHeaderSelector } = props as any
967
+ const extendedHeader = Object.assign(header, {
968
+ FlexRender: HeaderFlexRender,
969
+ ...headerComponents,
970
+ })
971
+
972
+ return (
973
+ <HeaderContext.Provider value={header}>
974
+ {appHeaderSelector ? (
975
+ <table.Subscribe selector={appHeaderSelector}>
976
+ {(state: TAppHeaderSelected) =>
977
+ (
978
+ children as (
979
+ header: Header<TFeatures, TData, TValue> &
980
+ THeaderComponents & { FlexRender: () => ReactNode },
981
+ state: TAppHeaderSelected,
982
+ ) => ReactNode
983
+ )(extendedHeader, state)
984
+ }
985
+ </table.Subscribe>
986
+ ) : (
987
+ (
988
+ children as (
989
+ header: Header<TFeatures, TData, TValue> &
990
+ THeaderComponents & { FlexRender: () => ReactNode },
991
+ ) => ReactNode
992
+ )(extendedHeader)
993
+ )}
994
+ </HeaderContext.Provider>
995
+ )
996
+ }
997
+ return AppHeaderImpl as AppHeaderComponent<
998
+ TFeatures,
999
+ TData,
1000
+ THeaderComponents
1001
+ >
1002
+ }, [table])
1003
+
1004
+ // AppFooter - Same as AppHeader (footers use Header type)
1005
+ const AppFooter = useMemo(() => {
1006
+ function AppFooterImpl<TValue extends CellData = CellData>(
1007
+ props: AppHeaderPropsWithoutSelector<
1008
+ TFeatures,
1009
+ TData,
1010
+ TValue,
1011
+ THeaderComponents
1012
+ >,
1013
+ ): ReactNode
1014
+ function AppFooterImpl<
1015
+ TValue extends CellData = CellData,
1016
+ TAppFooterSelected = unknown,
1017
+ >(
1018
+ props: AppHeaderPropsWithSelector<
1019
+ TFeatures,
1020
+ TData,
1021
+ TValue,
1022
+ THeaderComponents,
1023
+ TAppFooterSelected
1024
+ >,
1025
+ ): ReactNode
1026
+ function AppFooterImpl<
1027
+ TValue extends CellData = CellData,
1028
+ TAppFooterSelected = unknown,
1029
+ >(
1030
+ props:
1031
+ | AppHeaderPropsWithoutSelector<
1032
+ TFeatures,
1033
+ TData,
1034
+ TValue,
1035
+ THeaderComponents
1036
+ >
1037
+ | AppHeaderPropsWithSelector<
1038
+ TFeatures,
1039
+ TData,
1040
+ TValue,
1041
+ THeaderComponents,
1042
+ TAppFooterSelected
1043
+ >,
1044
+ ): ReactNode {
1045
+ const { header, children, selector: appFooterSelector } = props as any
1046
+ const extendedHeader = Object.assign(header, {
1047
+ FlexRender: FooterFlexRender,
1048
+ ...headerComponents,
1049
+ })
1050
+
1051
+ return (
1052
+ <HeaderContext.Provider value={header}>
1053
+ {appFooterSelector ? (
1054
+ <table.Subscribe selector={appFooterSelector}>
1055
+ {(state: TAppFooterSelected) =>
1056
+ (
1057
+ children as (
1058
+ header: Header<TFeatures, TData, TValue> &
1059
+ THeaderComponents & { FlexRender: () => ReactNode },
1060
+ state: TAppFooterSelected,
1061
+ ) => ReactNode
1062
+ )(extendedHeader, state)
1063
+ }
1064
+ </table.Subscribe>
1065
+ ) : (
1066
+ (
1067
+ children as (
1068
+ header: Header<TFeatures, TData, TValue> &
1069
+ THeaderComponents & { FlexRender: () => ReactNode },
1070
+ ) => ReactNode
1071
+ )(extendedHeader)
1072
+ )}
1073
+ </HeaderContext.Provider>
1074
+ )
1075
+ }
1076
+ return AppFooterImpl as AppHeaderComponent<
1077
+ TFeatures,
1078
+ TData,
1079
+ THeaderComponents
1080
+ >
1081
+ }, [table])
1082
+
1083
+ // Combine everything into the extended table API
1084
+ const extendedTable = useMemo(() => {
1085
+ return Object.assign(table, {
1086
+ AppTable,
1087
+ AppCell,
1088
+ AppHeader,
1089
+ AppFooter,
1090
+ ...tableComponents,
1091
+ }) as AppReactTable<
1092
+ TFeatures,
1093
+ TData,
1094
+ TSelected,
1095
+ TTableComponents,
1096
+ TCellComponents,
1097
+ THeaderComponents
1098
+ >
1099
+ }, [table, AppTable, AppCell, AppHeader, AppFooter])
1100
+
1101
+ return extendedTable
1102
+ }
1103
+
1104
+ return {
1105
+ appFeatures: defaultTableOptions._features as TFeatures,
1106
+ createAppColumnHelper,
1107
+ useAppTable,
1108
+ useTableContext,
1109
+ useCellContext,
1110
+ useHeaderContext,
1111
+ }
1112
+ }