@tanstack/svelte-table 9.0.0-beta.20 → 9.0.0-beta.22

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.
@@ -189,6 +189,47 @@ export type AppSvelteTable<TFeatures extends TableFeatures, TData extends RowDat
189
189
  */
190
190
  FlexRender: typeof FlexRenderSvelte;
191
191
  };
192
+ export interface CreateTableHookResult<TFeatures extends TableFeatures, TTableComponents extends Record<string, ComponentType<any>>, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> {
193
+ /** The features object that was passed to `createTableHook`. */
194
+ appFeatures: TFeatures;
195
+ /**
196
+ * A column helper pre-bound to `TFeatures` and the registered components, so
197
+ * the cell/header/footer render props expose the bound components.
198
+ */
199
+ createAppColumnHelper: <TData extends RowData>() => AppColumnHelper<TFeatures, TData, TCellComponents, THeaderComponents>;
200
+ /**
201
+ * Creates a table with the `App*` wrapper components and registered
202
+ * `tableComponents` attached. `TData` is inferred from the `data` option.
203
+ */
204
+ createAppTable: <TData extends RowData, TSelected = TableState<TFeatures>>(tableOptions: Omit<TableOptions<TFeatures, TData>, 'features'>, selector?: (state: TableState<TFeatures>) => TSelected) => AppSvelteTable<TFeatures, TData, TSelected, TTableComponents, TCellComponents, THeaderComponents>;
205
+ /**
206
+ * Reads the table provided by the nearest `<table.AppTable>`. This is the same
207
+ * extended instance `createAppTable` returns, so the `App*` components and your
208
+ * `tableComponents` are available on it.
209
+ *
210
+ * Pass `TSelected` to match the selector you gave `createAppTable`, so
211
+ * `table.state` is typed as the selected slice. It cannot be inferred
212
+ * automatically (context does not carry the provider's generics), so it
213
+ * defaults to the full table state, which is correct for the common case of
214
+ * `createAppTable` without a selector.
215
+ */
216
+ useTableContext: <TData extends RowData = RowData, TSelected = TableState<TFeatures>>() => AppSvelteTable<TFeatures, TData, TSelected, TTableComponents, TCellComponents, THeaderComponents>;
217
+ /**
218
+ * Reads the cell provided by the nearest `<table.AppCell>`, extended with your
219
+ * `cellComponents` and a context-bound `FlexRender`.
220
+ */
221
+ useCellContext: <TValue extends CellData = CellData>() => Cell<TFeatures, any, TValue> & TCellComponents & {
222
+ FlexRender: typeof FlexRenderSvelte;
223
+ };
224
+ /**
225
+ * Reads the header provided by the nearest `<table.AppHeader>` /
226
+ * `<table.AppFooter>`, extended with your `headerComponents` and a
227
+ * context-bound `FlexRender`.
228
+ */
229
+ useHeaderContext: <TValue extends CellData = CellData>() => Header<TFeatures, any, TValue> & THeaderComponents & {
230
+ FlexRender: typeof FlexRenderSvelte;
231
+ };
232
+ }
192
233
  /**
193
234
  * Creates a custom table hook with pre-bound components for composition.
194
235
  *
@@ -225,11 +266,4 @@ export type AppSvelteTable<TFeatures extends TableFeatures, TData extends RowDat
225
266
  * })
226
267
  * ```
227
268
  */
228
- export declare function createTableHook<TFeatures extends TableFeatures, const TTableComponents extends Record<string, ComponentType<any>>, const TCellComponents extends Record<string, ComponentType<any>>, const THeaderComponents extends Record<string, ComponentType<any>>>({ tableComponents, cellComponents, headerComponents, ...defaultTableOptions }: CreateTableHookOptions<TFeatures, TTableComponents, TCellComponents, THeaderComponents>): {
229
- appFeatures: TFeatures;
230
- createAppColumnHelper: <TData extends RowData>() => AppColumnHelper<TFeatures, TData, TCellComponents, THeaderComponents>;
231
- createAppTable: <TData extends RowData, TSelected = TableState<TFeatures>>(tableOptions: Omit<TableOptions<TFeatures, TData>, "features">, selector?: (state: TableState<TFeatures>) => TSelected) => AppSvelteTable<TFeatures, TData, TSelected, TTableComponents, TCellComponents, THeaderComponents>;
232
- useTableContext: <TData extends RowData = RowData>() => SvelteTable<TFeatures, TData>;
233
- useCellContext: <TValue extends CellData = unknown>() => Cell<TFeatures, any, TValue>;
234
- useHeaderContext: <TValue extends CellData = unknown>() => Header<TFeatures, any, TValue>;
235
- };
269
+ export declare function createTableHook<TFeatures extends TableFeatures, const TTableComponents extends Record<string, ComponentType<any>>, const TCellComponents extends Record<string, ComponentType<any>>, const THeaderComponents extends Record<string, ComponentType<any>>>({ tableComponents, cellComponents, headerComponents, ...defaultTableOptions }: CreateTableHookOptions<TFeatures, TTableComponents, TCellComponents, THeaderComponents>): CreateTableHookResult<TFeatures, TTableComponents, TCellComponents, THeaderComponents>;
@@ -65,6 +65,9 @@ export function createTableHook({ tableComponents, cellComponents, headerCompone
65
65
  throw new Error('`useTableContext` must be used within an `AppTable` component. ' +
66
66
  'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.');
67
67
  }
68
+ // `<table.AppTable>` provides the extended table (the App* wrapper
69
+ // components and `tableComponents` are Object.assign-ed onto the same
70
+ // instance `createAppTable` returns), so this asserts the runtime shape.
68
71
  return table;
69
72
  }
70
73
  /**
@@ -78,6 +81,9 @@ export function createTableHook({ tableComponents, cellComponents, headerCompone
78
81
  throw new Error('`useCellContext` must be used within an `AppCell` component. ' +
79
82
  'Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.');
80
83
  }
84
+ // `<table.AppCell>` Object.assign-es `cellComponents` and `FlexRender` onto
85
+ // the same cell instance it puts in context, so this asserts the runtime
86
+ // shape.
81
87
  return cell;
82
88
  }
83
89
  /**
@@ -90,6 +96,8 @@ export function createTableHook({ tableComponents, cellComponents, headerCompone
90
96
  if (!header) {
91
97
  throw new Error('`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.');
92
98
  }
99
+ // `<table.AppHeader>` / `<table.AppFooter>` Object.assign `headerComponents`
100
+ // and `FlexRender` onto the same header instance they put in context.
93
101
  return header;
94
102
  }
95
103
  /**
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export * from '@tanstack/table-core';
2
2
  export { createTable } from './createTable.svelte';
3
3
  export type { SvelteTable } from './createTable.svelte';
4
4
  export { createTableHook } from './createTableHook.svelte';
5
- export type { AppCellContext, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderContext, AppSvelteTable, ComponentType, CreateTableHookOptions, } from './createTableHook.svelte';
5
+ export type { AppCellContext, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderContext, AppSvelteTable, ComponentType, CreateTableHookOptions, CreateTableHookResult, } from './createTableHook.svelte';
6
6
  export { createTableState } from './createTableState.svelte';
7
7
  export { default as FlexRender } from './FlexRender.svelte';
8
8
  export { subscribeTable, type SubscribeSource } from './subscribe';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-table",
3
- "version": "9.0.0-beta.20",
3
+ "version": "9.0.0-beta.22",
4
4
  "description": "Headless UI for building powerful tables & datagrids for Svelte.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -53,7 +53,7 @@
53
53
  ],
54
54
  "dependencies": {
55
55
  "@tanstack/svelte-store": "^0.12.0",
56
- "@tanstack/table-core": "9.0.0-beta.20"
56
+ "@tanstack/table-core": "9.0.0-beta.21"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@sveltejs/package": "^2.5.8",
@@ -376,6 +376,84 @@ export type AppSvelteTable<
376
376
  FlexRender: typeof FlexRenderSvelte
377
377
  }
378
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
+
379
457
  // =============================================================================
380
458
  // createTableHook Factory
381
459
  // =============================================================================
@@ -431,7 +509,12 @@ export function createTableHook<
431
509
  TTableComponents,
432
510
  TCellComponents,
433
511
  THeaderComponents
434
- >) {
512
+ >): CreateTableHookResult<
513
+ TFeatures,
514
+ TTableComponents,
515
+ TCellComponents,
516
+ THeaderComponents
517
+ > {
435
518
  /**
436
519
  * Create a column helper pre-bound to the features and components configured in this table hook.
437
520
  * The cell, header, and footer contexts include pre-bound components (e.g., `cell.TextCell`).
@@ -455,9 +538,16 @@ export function createTableHook<
455
538
  * Use this in custom `tableComponents` passed to `createTableHook`.
456
539
  * TFeatures is already known from the createTableHook call.
457
540
  */
458
- function useTableContext<TData extends RowData = RowData>(): SvelteTable<
541
+ function useTableContext<
542
+ TData extends RowData = RowData,
543
+ TSelected = TableState<TFeatures>,
544
+ >(): AppSvelteTable<
459
545
  TFeatures,
460
- TData
546
+ TData,
547
+ TSelected,
548
+ TTableComponents,
549
+ TCellComponents,
550
+ THeaderComponents
461
551
  > {
462
552
  const table = getContext(tableContextKey)
463
553
 
@@ -468,7 +558,17 @@ export function createTableHook<
468
558
  )
469
559
  }
470
560
 
471
- return table as SvelteTable<TFeatures, TData>
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
+ >
472
572
  }
473
573
 
474
574
  /**
@@ -480,7 +580,8 @@ export function createTableHook<
480
580
  TFeatures,
481
581
  any,
482
582
  TValue
483
- > {
583
+ > &
584
+ TCellComponents & { FlexRender: typeof FlexRenderSvelte } {
484
585
  const cell = getContext(cellContextKey)
485
586
 
486
587
  if (!cell) {
@@ -490,7 +591,11 @@ export function createTableHook<
490
591
  )
491
592
  }
492
593
 
493
- return cell as Cell<TFeatures, any, TValue>
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 }
494
599
  }
495
600
 
496
601
  /**
@@ -502,7 +607,8 @@ export function createTableHook<
502
607
  TFeatures,
503
608
  any,
504
609
  TValue
505
- > {
610
+ > &
611
+ THeaderComponents & { FlexRender: typeof FlexRenderSvelte } {
506
612
  const header = getContext(headerContextKey)
507
613
 
508
614
  if (!header) {
@@ -511,7 +617,10 @@ export function createTableHook<
511
617
  )
512
618
  }
513
619
 
514
- return header as Header<TFeatures, any, TValue>
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 }
515
624
  }
516
625
 
517
626
  /**
@@ -626,7 +735,7 @@ export function createTableHook<
626
735
  }
627
736
 
628
737
  return {
629
- appFeatures: defaultTableOptions.features as TFeatures,
738
+ appFeatures: defaultTableOptions.features,
630
739
  createAppColumnHelper,
631
740
  createAppTable,
632
741
  useTableContext,
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export type {
14
14
  AppSvelteTable,
15
15
  ComponentType,
16
16
  CreateTableHookOptions,
17
+ CreateTableHookResult,
17
18
  } from './createTableHook.svelte'
18
19
  export { createTableState } from './createTableState.svelte'
19
20
  export { default as FlexRender } from './FlexRender.svelte'