@tanstack/svelte-table 9.0.0-beta.52 → 9.0.0-beta.53

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.
@@ -1,745 +0,0 @@
1
- import { getContext, setContext } from 'svelte'
2
- import { createColumnHelper as coreCreateColumnHelper } from '@tanstack/table-core'
3
- import { createTable } from './createTable.svelte'
4
- import { mergeObjects } from './merge-objects'
5
- import {
6
- cellContextKey,
7
- headerContextKey,
8
- tableContextKey,
9
- } from './context-keys.js'
10
- import AppTableSvelte from './AppTable.svelte'
11
- import AppCellSvelte from './AppCell.svelte'
12
- import AppHeaderSvelte from './AppHeader.svelte'
13
- import FlexRenderSvelte from './FlexRender.svelte'
14
- import type { SvelteTable } from './createTable.svelte'
15
- import type { Component, Snippet } from 'svelte'
16
- import type {
17
- AccessorFn,
18
- AccessorFnColumnDef,
19
- AccessorKeyColumnDef,
20
- Cell,
21
- CellContext,
22
- CellData,
23
- Column,
24
- ColumnDef,
25
- DeepKeys,
26
- DeepValue,
27
- DisplayColumnDef,
28
- GroupColumnDef,
29
- Header,
30
- IdentifiedColumnDef,
31
- NoInfer,
32
- Row,
33
- RowData,
34
- Table,
35
- TableFeatures,
36
- TableOptions,
37
- TableState,
38
- } from '@tanstack/table-core'
39
-
40
- export type ComponentType<T extends Record<string, any>> = Component<T>
41
-
42
- // =============================================================================
43
- // Enhanced Context Types with Pre-bound Components
44
- // =============================================================================
45
-
46
- /**
47
- * Enhanced CellContext with pre-bound cell components.
48
- * The `cell` property includes the registered cellComponents.
49
- */
50
- export type AppCellContext<
51
- TFeatures extends TableFeatures,
52
- TData extends RowData,
53
- TValue extends CellData,
54
- TCellComponents extends Record<string, ComponentType<any>>,
55
- > = {
56
- cell: Cell<TFeatures, TData, TValue> &
57
- TCellComponents & { FlexRender: typeof FlexRenderSvelte }
58
- column: Column<TFeatures, TData, TValue>
59
- getValue: CellContext<TFeatures, TData, TValue>['getValue']
60
- renderValue: CellContext<TFeatures, TData, TValue>['renderValue']
61
- row: Row<TFeatures, TData>
62
- table: Table<TFeatures, TData>
63
- }
64
-
65
- /**
66
- * Enhanced HeaderContext with pre-bound header components.
67
- * The `header` property includes the registered headerComponents.
68
- */
69
- export type AppHeaderContext<
70
- TFeatures extends TableFeatures,
71
- TData extends RowData,
72
- TValue extends CellData,
73
- THeaderComponents extends Record<string, ComponentType<any>>,
74
- > = {
75
- column: Column<TFeatures, TData, TValue>
76
- header: Header<TFeatures, TData, TValue> &
77
- THeaderComponents & { FlexRender: typeof FlexRenderSvelte }
78
- table: Table<TFeatures, TData>
79
- }
80
-
81
- // =============================================================================
82
- // Enhanced Column Definition Types
83
- // =============================================================================
84
-
85
- /**
86
- * Template type for column definitions that can be a string or a function.
87
- */
88
- export type AppColumnDefTemplate<TProps extends object> =
89
- | string
90
- | ((props: TProps) => any)
91
-
92
- /**
93
- * Enhanced column definition base with pre-bound components in cell/header/footer contexts.
94
- */
95
- export type AppColumnDefBase<
96
- TFeatures extends TableFeatures,
97
- TData extends RowData,
98
- TValue extends CellData,
99
- TCellComponents extends Record<string, ComponentType<any>>,
100
- THeaderComponents extends Record<string, ComponentType<any>>,
101
- > = Omit<
102
- IdentifiedColumnDef<TFeatures, TData, TValue>,
103
- 'cell' | 'header' | 'footer'
104
- > & {
105
- cell?: AppColumnDefTemplate<
106
- AppCellContext<TFeatures, TData, TValue, TCellComponents>
107
- >
108
- header?: AppColumnDefTemplate<
109
- AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>
110
- >
111
- footer?: AppColumnDefTemplate<
112
- AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>
113
- >
114
- }
115
-
116
- /**
117
- * Enhanced display column definition with pre-bound components.
118
- */
119
- export type AppDisplayColumnDef<
120
- TFeatures extends TableFeatures,
121
- TData extends RowData,
122
- TCellComponents extends Record<string, ComponentType<any>>,
123
- THeaderComponents extends Record<string, ComponentType<any>>,
124
- > = Omit<
125
- DisplayColumnDef<TFeatures, TData, unknown>,
126
- 'cell' | 'header' | 'footer'
127
- > & {
128
- cell?: AppColumnDefTemplate<
129
- AppCellContext<TFeatures, TData, unknown, TCellComponents>
130
- >
131
- header?: AppColumnDefTemplate<
132
- AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
133
- >
134
- footer?: AppColumnDefTemplate<
135
- AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
136
- >
137
- }
138
-
139
- /**
140
- * Enhanced group column definition with pre-bound components.
141
- */
142
- export type AppGroupColumnDef<
143
- TFeatures extends TableFeatures,
144
- TData extends RowData,
145
- TCellComponents extends Record<string, ComponentType<any>>,
146
- THeaderComponents extends Record<string, ComponentType<any>>,
147
- > = Omit<
148
- GroupColumnDef<TFeatures, TData, unknown>,
149
- 'cell' | 'header' | 'footer' | 'columns'
150
- > & {
151
- cell?: AppColumnDefTemplate<
152
- AppCellContext<TFeatures, TData, unknown, TCellComponents>
153
- >
154
- header?: AppColumnDefTemplate<
155
- AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
156
- >
157
- footer?: AppColumnDefTemplate<
158
- AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>
159
- >
160
- columns?: Array<ColumnDef<TFeatures, TData, unknown>>
161
- }
162
-
163
- // =============================================================================
164
- // Enhanced Column Helper Type
165
- // =============================================================================
166
-
167
- /**
168
- * Enhanced column helper with pre-bound components in cell/header/footer contexts.
169
- * This enables TypeScript to know about the registered components when defining columns.
170
- */
171
- export type AppColumnHelper<
172
- TFeatures extends TableFeatures,
173
- TData extends RowData,
174
- TCellComponents extends Record<string, ComponentType<any>>,
175
- THeaderComponents extends Record<string, ComponentType<any>>,
176
- > = {
177
- /**
178
- * Creates a data column definition with an accessor key or function.
179
- * The cell, header, and footer contexts include pre-bound components.
180
- */
181
- accessor: <
182
- TAccessor extends AccessorFn<TData> | DeepKeys<TData>,
183
- TValue extends TAccessor extends AccessorFn<TData, infer TReturn>
184
- ? TReturn
185
- : TAccessor extends DeepKeys<TData>
186
- ? DeepValue<TData, TAccessor>
187
- : never,
188
- >(
189
- accessor: TAccessor,
190
- column: TAccessor extends AccessorFn<TData>
191
- ? AppColumnDefBase<
192
- TFeatures,
193
- TData,
194
- TValue,
195
- TCellComponents,
196
- THeaderComponents
197
- > & { id: string }
198
- : AppColumnDefBase<
199
- TFeatures,
200
- TData,
201
- TValue,
202
- TCellComponents,
203
- THeaderComponents
204
- >,
205
- ) => TAccessor extends AccessorFn<TData>
206
- ? AccessorFnColumnDef<TFeatures, TData, TValue>
207
- : AccessorKeyColumnDef<TFeatures, TData, TValue>
208
-
209
- /**
210
- * Wraps an array of column definitions to preserve each column's individual TValue type.
211
- */
212
- columns: <TColumns extends ReadonlyArray<ColumnDef<TFeatures, TData, any>>>(
213
- columns: [...TColumns],
214
- ) => Array<ColumnDef<TFeatures, TData, any>> & [...TColumns]
215
-
216
- /**
217
- * Creates a display column definition for non-data columns.
218
- * The cell, header, and footer contexts include pre-bound components.
219
- */
220
- display: (
221
- column: AppDisplayColumnDef<
222
- TFeatures,
223
- TData,
224
- TCellComponents,
225
- THeaderComponents
226
- >,
227
- ) => DisplayColumnDef<TFeatures, TData, unknown>
228
-
229
- /**
230
- * Creates a group column definition with nested child columns.
231
- * The cell, header, and footer contexts include pre-bound components.
232
- */
233
- group: (
234
- column: AppGroupColumnDef<
235
- TFeatures,
236
- TData,
237
- TCellComponents,
238
- THeaderComponents
239
- >,
240
- ) => GroupColumnDef<TFeatures, TData, unknown>
241
- }
242
-
243
- // =============================================================================
244
- // CreateTableHook Options
245
- // =============================================================================
246
-
247
- /**
248
- * Options for creating a table hook with pre-bound components and default table options.
249
- * Extends all TableOptions except 'columns' | 'data' | 'store' | 'state' | 'initialState'.
250
- */
251
- export type CreateTableHookOptions<
252
- TFeatures extends TableFeatures,
253
- TTableComponents extends Record<string, ComponentType<any>>,
254
- TCellComponents extends Record<string, ComponentType<any>>,
255
- THeaderComponents extends Record<string, ComponentType<any>>,
256
- > = Omit<
257
- TableOptions<TFeatures, any>,
258
- 'columns' | 'data' | 'store' | 'state' | 'initialState'
259
- > & {
260
- /**
261
- * Table-level components that need access to the table instance.
262
- * These are available directly on the table object returned by createAppTable.
263
- * Use `useTableContext()` inside these components.
264
- * @example { PaginationControls, GlobalFilter, RowCount }
265
- */
266
- tableComponents?: TTableComponents
267
- /**
268
- * Cell-level components that need access to the cell instance.
269
- * These are available on the cell object passed to AppCell's children.
270
- * Use `useCellContext()` inside these components.
271
- * @example { TextCell, NumberCell, DateCell, CurrencyCell }
272
- */
273
- cellComponents?: TCellComponents
274
- /**
275
- * Header-level components that need access to the header instance.
276
- * These are available on the header object passed to AppHeader/AppFooter's children.
277
- * Use `useHeaderContext()` inside these components.
278
- * @example { SortIndicator, ColumnFilter, ResizeHandle }
279
- */
280
- headerComponents?: THeaderComponents
281
- }
282
-
283
- // =============================================================================
284
- // Extended Table Type
285
- // =============================================================================
286
-
287
- /**
288
- * Extended table API returned by createAppTable with all App wrapper components.
289
- */
290
- export type AppSvelteTable<
291
- TFeatures extends TableFeatures,
292
- TData extends RowData,
293
- TSelected,
294
- TTableComponents extends Record<string, ComponentType<any>>,
295
- TCellComponents extends Record<string, ComponentType<any>>,
296
- THeaderComponents extends Record<string, ComponentType<any>>,
297
- > = SvelteTable<TFeatures, TData, TSelected> &
298
- NoInfer<TTableComponents> & {
299
- /**
300
- * Root wrapper component that provides table context.
301
- * @example
302
- * ```svelte
303
- * <table.AppTable>
304
- * <table>...</table>
305
- * </table.AppTable>
306
- * ```
307
- */
308
- AppTable: Component<{ children: Snippet }>
309
- /**
310
- * Wraps a cell and provides cell context with pre-bound cellComponents.
311
- * @example
312
- * ```svelte
313
- * <table.AppCell cell={cell}>
314
- * {#snippet children(c)}
315
- * <td><c.TextCell /></td>
316
- * {/snippet}
317
- * </table.AppCell>
318
- * ```
319
- */
320
- AppCell: Component<{
321
- cell: Cell<TFeatures, TData, any>
322
- children: Snippet<
323
- [
324
- Cell<TFeatures, TData, any> &
325
- NoInfer<TCellComponents> & { FlexRender: typeof FlexRenderSvelte },
326
- ]
327
- >
328
- }>
329
- /**
330
- * Wraps a header and provides header context with pre-bound headerComponents.
331
- * @example
332
- * ```svelte
333
- * <table.AppHeader header={header}>
334
- * {#snippet children(h)}
335
- * <th><h.SortIndicator /></th>
336
- * {/snippet}
337
- * </table.AppHeader>
338
- * ```
339
- */
340
- AppHeader: Component<{
341
- header: Header<TFeatures, TData, any>
342
- children: Snippet<
343
- [
344
- Header<TFeatures, TData, any> &
345
- NoInfer<THeaderComponents> & {
346
- FlexRender: typeof FlexRenderSvelte
347
- },
348
- ]
349
- >
350
- }>
351
- /**
352
- * Wraps a footer and provides header context with pre-bound headerComponents.
353
- * @example
354
- * ```svelte
355
- * <table.AppFooter header={footer}>
356
- * {#snippet children(f)}
357
- * <td><f.FlexRender /></td>
358
- * {/snippet}
359
- * </table.AppFooter>
360
- * ```
361
- */
362
- AppFooter: Component<{
363
- header: Header<TFeatures, TData, any>
364
- children: Snippet<
365
- [
366
- Header<TFeatures, TData, any> &
367
- NoInfer<THeaderComponents> & {
368
- FlexRender: typeof FlexRenderSvelte
369
- },
370
- ]
371
- >
372
- }>
373
- /**
374
- * Convenience FlexRender component attached to the table instance.
375
- */
376
- FlexRender: typeof FlexRenderSvelte
377
- }
378
-
379
- export interface CreateTableHookResult<
380
- TFeatures extends TableFeatures,
381
- TTableComponents extends Record<string, ComponentType<any>>,
382
- TCellComponents extends Record<string, ComponentType<any>>,
383
- THeaderComponents extends Record<string, ComponentType<any>>,
384
- > {
385
- /** The features object that was passed to `createTableHook`. */
386
- appFeatures: TFeatures
387
- /**
388
- * A column helper pre-bound to `TFeatures` and the registered components, so
389
- * the cell/header/footer render props expose the bound components.
390
- */
391
- createAppColumnHelper: <TData extends RowData>() => AppColumnHelper<
392
- TFeatures,
393
- TData,
394
- TCellComponents,
395
- THeaderComponents
396
- >
397
- /**
398
- * Creates a table with the `App*` wrapper components and registered
399
- * `tableComponents` attached. `TData` is inferred from the `data` option.
400
- */
401
- createAppTable: <TData extends RowData, TSelected = TableState<TFeatures>>(
402
- tableOptions: Omit<TableOptions<TFeatures, TData>, 'features'>,
403
- selector?: (state: TableState<TFeatures>) => TSelected,
404
- ) => AppSvelteTable<
405
- TFeatures,
406
- TData,
407
- TSelected,
408
- TTableComponents,
409
- TCellComponents,
410
- THeaderComponents
411
- >
412
- /**
413
- * Reads the table provided by the nearest `<table.AppTable>`. This is the same
414
- * extended instance `createAppTable` returns, so the `App*` components and your
415
- * `tableComponents` are available on it.
416
- *
417
- * Pass `TSelected` to match the selector you gave `createAppTable`, so
418
- * `table.state` is typed as the selected slice. It cannot be inferred
419
- * automatically (context does not carry the provider's generics), so it
420
- * defaults to the full table state, which is correct for the common case of
421
- * `createAppTable` without a selector.
422
- */
423
- useTableContext: <
424
- TData extends RowData = RowData,
425
- TSelected = TableState<TFeatures>,
426
- >() => AppSvelteTable<
427
- TFeatures,
428
- TData,
429
- TSelected,
430
- TTableComponents,
431
- TCellComponents,
432
- THeaderComponents
433
- >
434
- /**
435
- * Reads the cell provided by the nearest `<table.AppCell>`, extended with your
436
- * `cellComponents` and a context-bound `FlexRender`.
437
- */
438
- useCellContext: <TValue extends CellData = CellData>() => Cell<
439
- TFeatures,
440
- any,
441
- TValue
442
- > &
443
- TCellComponents & { FlexRender: typeof FlexRenderSvelte }
444
- /**
445
- * Reads the header provided by the nearest `<table.AppHeader>` /
446
- * `<table.AppFooter>`, extended with your `headerComponents` and a
447
- * context-bound `FlexRender`.
448
- */
449
- useHeaderContext: <TValue extends CellData = CellData>() => Header<
450
- TFeatures,
451
- any,
452
- TValue
453
- > &
454
- THeaderComponents & { FlexRender: typeof FlexRenderSvelte }
455
- }
456
-
457
- // =============================================================================
458
- // createTableHook Factory
459
- // =============================================================================
460
-
461
- /**
462
- * Creates a custom table hook with pre-bound components for composition.
463
- *
464
- * This is the table equivalent of TanStack Form's `createFormHook`. It allows you to:
465
- * - Define features, row models, and default options once, shared across all tables
466
- * - Register reusable table, cell, and header components
467
- * - Access table/cell/header instances via context in those components
468
- * - Get a `createAppTable` hook that returns an extended table with App wrapper components
469
- * - Get a `createAppColumnHelper` function pre-bound to your features
470
- *
471
- * @example
472
- * ```ts
473
- * // hooks/table.ts
474
- * export const {
475
- * createAppTable,
476
- * createAppColumnHelper,
477
- * useTableContext,
478
- * useCellContext,
479
- * useHeaderContext,
480
- * } = createTableHook({
481
- * features: tableFeatures({
482
- * rowPaginationFeature,
483
- * rowSortingFeature,
484
- * columnFilteringFeature,
485
- * paginatedRowModel: createPaginatedRowModel(),
486
- * sortedRowModel: createSortedRowModel(),
487
- * filteredRowModel: createFilteredRowModel(),
488
- * sortFns,
489
- * filterFns,
490
- * }),
491
- * tableComponents: { PaginationControls, RowCount },
492
- * cellComponents: { TextCell, NumberCell },
493
- * headerComponents: { SortIndicator, ColumnFilter },
494
- * })
495
- * ```
496
- */
497
- export function createTableHook<
498
- TFeatures extends TableFeatures,
499
- const TTableComponents extends Record<string, ComponentType<any>>,
500
- const TCellComponents extends Record<string, ComponentType<any>>,
501
- const THeaderComponents extends Record<string, ComponentType<any>>,
502
- >({
503
- tableComponents,
504
- cellComponents,
505
- headerComponents,
506
- ...defaultTableOptions
507
- }: CreateTableHookOptions<
508
- TFeatures,
509
- TTableComponents,
510
- TCellComponents,
511
- THeaderComponents
512
- >): CreateTableHookResult<
513
- TFeatures,
514
- TTableComponents,
515
- TCellComponents,
516
- THeaderComponents
517
- > {
518
- /**
519
- * Create a column helper pre-bound to the features and components configured in this table hook.
520
- * The cell, header, and footer contexts include pre-bound components (e.g., `cell.TextCell`).
521
- */
522
- function createAppColumnHelper<TData extends RowData>(): AppColumnHelper<
523
- TFeatures,
524
- TData,
525
- TCellComponents,
526
- THeaderComponents
527
- > {
528
- return coreCreateColumnHelper<TFeatures, TData>() as AppColumnHelper<
529
- TFeatures,
530
- TData,
531
- TCellComponents,
532
- THeaderComponents
533
- >
534
- }
535
-
536
- /**
537
- * Access the table instance from within an `AppTable` wrapper.
538
- * Use this in custom `tableComponents` passed to `createTableHook`.
539
- * TFeatures is already known from the createTableHook call.
540
- */
541
- function useTableContext<
542
- TData extends RowData = RowData,
543
- TSelected = TableState<TFeatures>,
544
- >(): AppSvelteTable<
545
- TFeatures,
546
- TData,
547
- TSelected,
548
- TTableComponents,
549
- TCellComponents,
550
- THeaderComponents
551
- > {
552
- const table = getContext(tableContextKey)
553
-
554
- if (!table) {
555
- throw new Error(
556
- '`useTableContext` must be used within an `AppTable` component. ' +
557
- 'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.',
558
- )
559
- }
560
-
561
- // `<table.AppTable>` provides the extended table (the App* wrapper
562
- // components and `tableComponents` are Object.assign-ed onto the same
563
- // instance `createAppTable` returns), so this asserts the runtime shape.
564
- return table as unknown as AppSvelteTable<
565
- TFeatures,
566
- TData,
567
- TSelected,
568
- TTableComponents,
569
- TCellComponents,
570
- THeaderComponents
571
- >
572
- }
573
-
574
- /**
575
- * Access the cell instance from within an `AppCell` wrapper.
576
- * Use this in custom `cellComponents` passed to `createTableHook`.
577
- * TFeatures is already known from the createTableHook call.
578
- */
579
- function useCellContext<TValue extends CellData = CellData>(): Cell<
580
- TFeatures,
581
- any,
582
- TValue
583
- > &
584
- TCellComponents & { FlexRender: typeof FlexRenderSvelte } {
585
- const cell = getContext(cellContextKey)
586
-
587
- if (!cell) {
588
- throw new Error(
589
- '`useCellContext` must be used within an `AppCell` component. ' +
590
- 'Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.',
591
- )
592
- }
593
-
594
- // `<table.AppCell>` Object.assign-es `cellComponents` and `FlexRender` onto
595
- // the same cell instance it puts in context, so this asserts the runtime
596
- // shape.
597
- return cell as unknown as Cell<TFeatures, any, TValue> &
598
- TCellComponents & { FlexRender: typeof FlexRenderSvelte }
599
- }
600
-
601
- /**
602
- * Access the header instance from within an `AppHeader` or `AppFooter` wrapper.
603
- * Use this in custom `headerComponents` passed to `createTableHook`.
604
- * TFeatures is already known from the createTableHook call.
605
- */
606
- function useHeaderContext<TValue extends CellData = CellData>(): Header<
607
- TFeatures,
608
- any,
609
- TValue
610
- > &
611
- THeaderComponents & { FlexRender: typeof FlexRenderSvelte } {
612
- const header = getContext(headerContextKey)
613
-
614
- if (!header) {
615
- throw new Error(
616
- '`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.',
617
- )
618
- }
619
-
620
- // `<table.AppHeader>` / `<table.AppFooter>` Object.assign `headerComponents`
621
- // and `FlexRender` onto the same header instance they put in context.
622
- return header as unknown as Header<TFeatures, any, TValue> &
623
- THeaderComponents & { FlexRender: typeof FlexRenderSvelte }
624
- }
625
-
626
- /**
627
- * Enhanced createTable hook that returns a table with App wrapper components
628
- * and pre-bound tableComponents attached directly to the table object.
629
- *
630
- * Default options from createTableHook are automatically merged with
631
- * the options passed here. Options passed here take precedence.
632
- *
633
- * TFeatures is already known from the createTableHook call; TData is inferred from the data prop.
634
- */
635
- function createAppTable<
636
- TData extends RowData,
637
- TSelected = TableState<TFeatures>,
638
- >(
639
- tableOptions: Omit<TableOptions<TFeatures, TData>, 'features'>,
640
- selector?: (state: TableState<TFeatures>) => TSelected,
641
- ): AppSvelteTable<
642
- TFeatures,
643
- TData,
644
- TSelected,
645
- TTableComponents,
646
- TCellComponents,
647
- THeaderComponents
648
- > {
649
- // Merge default options with provided options (provided takes precedence)
650
- const mergedTableOptions = mergeObjects(
651
- defaultTableOptions,
652
- tableOptions,
653
- ) as TableOptions<TFeatures, TData>
654
-
655
- const table = createTable<TFeatures, TData, TSelected>(
656
- mergedTableOptions,
657
- selector,
658
- )
659
-
660
- // Build cellComponents with FlexRender included
661
- const cellComponentsWithFlexRender = {
662
- FlexRender: FlexRenderSvelte,
663
- ...(cellComponents ?? {}),
664
- }
665
-
666
- // Build headerComponents with FlexRender included
667
- const headerComponentsWithFlexRender = {
668
- FlexRender: FlexRenderSvelte,
669
- ...(headerComponents ?? {}),
670
- }
671
-
672
- // Create wrapper components using the svelte-form (internal, props) => pattern.
673
- // setContext is called in the closure — this runs during component
674
- // initialization, so Svelte's context API works correctly.
675
- // With keyed {#each} blocks, components are recreated on reorder,
676
- // so context is always fresh.
677
- const AppTable = ((internal: any, props: any) => {
678
- setContext(tableContextKey, table)
679
- return AppTableSvelte(internal, { ...props })
680
- }) as Component<{ children: Snippet }>
681
-
682
- const AppCell = ((internal: any, { children, cell }: any) => {
683
- setContext(cellContextKey, cell)
684
- return AppCellSvelte(internal, {
685
- cell,
686
- cellComponents: cellComponentsWithFlexRender,
687
- children,
688
- })
689
- }) as Component<{
690
- cell: Cell<TFeatures, TData, any>
691
- children: Snippet<[any]>
692
- }>
693
-
694
- const AppHeader = ((internal: any, { children, header }: any) => {
695
- setContext(headerContextKey, header)
696
- return AppHeaderSvelte(internal, {
697
- header,
698
- headerComponents: headerComponentsWithFlexRender,
699
- children,
700
- })
701
- }) as Component<{
702
- header: Header<TFeatures, TData, any>
703
- children: Snippet<[any]>
704
- }>
705
-
706
- // AppFooter reuses AppHeaderSvelte (footers use Header type in table-core)
707
- const AppFooter = ((internal: any, { children, header }: any) => {
708
- setContext(headerContextKey, header)
709
- return AppHeaderSvelte(internal, {
710
- header,
711
- headerComponents: headerComponentsWithFlexRender,
712
- children,
713
- })
714
- }) as Component<{
715
- header: Header<TFeatures, TData, any>
716
- children: Snippet<[any]>
717
- }>
718
-
719
- // Combine everything into the extended table API
720
- return Object.assign(table, {
721
- AppTable,
722
- AppCell,
723
- AppHeader,
724
- AppFooter,
725
- FlexRender: FlexRenderSvelte,
726
- ...(tableComponents ?? {}),
727
- }) as AppSvelteTable<
728
- TFeatures,
729
- TData,
730
- TSelected,
731
- TTableComponents,
732
- TCellComponents,
733
- THeaderComponents
734
- >
735
- }
736
-
737
- return {
738
- appFeatures: defaultTableOptions.features,
739
- createAppColumnHelper,
740
- createAppTable,
741
- useTableContext,
742
- useCellContext,
743
- useHeaderContext,
744
- }
745
- }