@tanstack/svelte-table 9.0.0-alpha.5 → 9.0.0-alpha.50

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.
Files changed (63) hide show
  1. package/README.md +127 -0
  2. package/dist/AppCell.svelte +13 -0
  3. package/dist/AppCell.svelte.d.ts +9 -0
  4. package/dist/AppHeader.svelte +13 -0
  5. package/dist/AppHeader.svelte.d.ts +9 -0
  6. package/dist/AppTable.svelte +11 -0
  7. package/dist/AppTable.svelte.d.ts +7 -0
  8. package/dist/FlexRender.svelte +103 -0
  9. package/dist/FlexRender.svelte.d.ts +51 -0
  10. package/dist/context-keys.d.ts +3 -0
  11. package/dist/context-keys.js +3 -0
  12. package/dist/createTable.svelte.d.ts +38 -0
  13. package/dist/createTable.svelte.js +79 -0
  14. package/dist/createTableHook.svelte.d.ts +235 -0
  15. package/dist/createTableHook.svelte.js +170 -0
  16. package/dist/createTableState.svelte.d.ts +17 -0
  17. package/dist/createTableState.svelte.js +27 -0
  18. package/dist/flex-render.d.ts +1 -0
  19. package/dist/flex-render.js +2 -0
  20. package/dist/index.d.ts +8 -3
  21. package/dist/index.js +6 -3
  22. package/dist/merge-objects.d.ts +24 -0
  23. package/dist/merge-objects.js +45 -0
  24. package/dist/reactivity.svelte.d.ts +9 -0
  25. package/dist/reactivity.svelte.js +55 -0
  26. package/dist/render-component.d.ts +66 -9
  27. package/dist/render-component.js +62 -4
  28. package/dist/static-functions.d.ts +1 -0
  29. package/dist/static-functions.js +1 -0
  30. package/dist/subscribe.d.ts +24 -0
  31. package/dist/subscribe.js +4 -0
  32. package/package.json +31 -11
  33. package/skills/svelte/client-to-server/SKILL.md +238 -0
  34. package/skills/svelte/compose-with-tanstack-form/SKILL.md +295 -0
  35. package/skills/svelte/compose-with-tanstack-pacer/SKILL.md +176 -0
  36. package/skills/svelte/compose-with-tanstack-query/SKILL.md +299 -0
  37. package/skills/svelte/compose-with-tanstack-store/SKILL.md +277 -0
  38. package/skills/svelte/compose-with-tanstack-virtual/SKILL.md +286 -0
  39. package/skills/svelte/getting-started/SKILL.md +340 -0
  40. package/skills/svelte/migrate-v8-to-v9/SKILL.md +256 -0
  41. package/skills/svelte/production-readiness/SKILL.md +256 -0
  42. package/skills/svelte/table-state/SKILL.md +441 -0
  43. package/src/AppCell.svelte +13 -0
  44. package/src/AppHeader.svelte +13 -0
  45. package/src/AppTable.svelte +11 -0
  46. package/src/FlexRender.svelte +103 -0
  47. package/src/context-keys.ts +3 -0
  48. package/src/createTable.svelte.ts +127 -0
  49. package/src/createTableHook.svelte.ts +639 -0
  50. package/src/createTableState.svelte.ts +30 -0
  51. package/src/flex-render.ts +3 -0
  52. package/src/index.ts +20 -3
  53. package/src/merge-objects.ts +79 -0
  54. package/src/reactivity.svelte.ts +74 -0
  55. package/src/render-component.ts +75 -9
  56. package/src/static-functions.ts +1 -0
  57. package/src/subscribe.ts +46 -0
  58. package/dist/flex-render.svelte +0 -35
  59. package/dist/flex-render.svelte.d.ts +0 -28
  60. package/dist/table.svelte.d.ts +0 -28
  61. package/dist/table.svelte.js +0 -87
  62. package/src/flex-render.svelte +0 -35
  63. package/src/table.svelte.ts +0 -117
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Merges objects together while keeping their getters alive.
3
+ * Taken from SolidJS: {https://github.com/solidjs/solid/blob/24abc825c0996fd2bc8c1de1491efe9a7e743aff/packages/solid/src/server/rendering.ts#L82-L115}
4
+ * */
5
+ export function mergeObjects<T>(source: T): T
6
+ export function mergeObjects<T, U>(source: T, source1: U): T & U
7
+ export function mergeObjects<T, U, V>(
8
+ source: T,
9
+ source1: U,
10
+ source2: V,
11
+ ): T & U & V
12
+ export function mergeObjects<T, U, V, W>(
13
+ source: T,
14
+ source1: U,
15
+ source2: V,
16
+ source3: W,
17
+ ): T & U & V & W
18
+ export function mergeObjects(...sources: any): any {
19
+ const target = {}
20
+ for (let source of sources) {
21
+ if (typeof source === 'function') source = source()
22
+ if (source) {
23
+ const descriptors = Object.getOwnPropertyDescriptors(source)
24
+ for (const key in descriptors) {
25
+ if (key in target) continue
26
+ Object.defineProperty(target, key, {
27
+ enumerable: true,
28
+ get() {
29
+ for (let i = sources.length - 1; i >= 0; i--) {
30
+ let v,
31
+ s = sources[i]
32
+ if (typeof s === 'function') s = s()
33
+ // eslint-disable-next-line prefer-const
34
+ v = (s || {})[key]
35
+ if (v !== undefined) return v
36
+ }
37
+ },
38
+ })
39
+ }
40
+ }
41
+ }
42
+ return target
43
+ }
44
+
45
+ /**
46
+ * Merges objects together by eagerly resolving all values into a flat object.
47
+ *
48
+ * Unlike `mergeObjects`, this does NOT preserve getters — values are read once
49
+ * and stored as plain data properties. This prevents the getter-chain
50
+ * accumulation that causes O(N) lookups when the result is repeatedly passed
51
+ * back as a source in subsequent merges (e.g., inside `$effect.pre` loops).
52
+ *
53
+ * Later sources take precedence; `undefined` values do not override.
54
+ *
55
+ * @see https://github.com/TanStack/table/issues/6235
56
+ */
57
+ export function flatMerge<T>(source: T): T
58
+ export function flatMerge<T, U>(source: T, source1: U): T & U
59
+ export function flatMerge<T, U, V>(source: T, source1: U, source2: V): T & U & V
60
+ export function flatMerge<T, U, V, W>(
61
+ source: T,
62
+ source1: U,
63
+ source2: V,
64
+ source3: W,
65
+ ): T & U & V & W
66
+ export function flatMerge(...sources: any): any {
67
+ const result: Record<PropertyKey, unknown> = {}
68
+ for (let source of sources) {
69
+ if (typeof source === 'function') source = source()
70
+ if (!source) continue
71
+ for (const key of Reflect.ownKeys(source)) {
72
+ const value = (source as Record<PropertyKey, unknown>)[key]
73
+ if (value !== undefined) {
74
+ result[key as string] = value
75
+ }
76
+ }
77
+ }
78
+ return result
79
+ }
@@ -0,0 +1,74 @@
1
+ import { flushSync, untrack } from 'svelte'
2
+ import type {
3
+ TableAtomOptions,
4
+ TableReactivityBindings,
5
+ } from '@tanstack/table-core/reactivity'
6
+ import type { Atom, Observer, ReadonlyAtom } from '@tanstack/svelte-store'
7
+
8
+ function observerToCallback<T>(
9
+ observerOrNext: Observer<T> | ((value: T) => void),
10
+ ): (value: T) => void {
11
+ return typeof observerOrNext === 'function'
12
+ ? observerOrNext
13
+ : (value) => observerOrNext.next?.(value)
14
+ }
15
+
16
+ function subscribeToRune<T>(
17
+ getValue: () => T,
18
+ observerOrNext: Observer<T> | ((value: T) => void),
19
+ ) {
20
+ const callback = observerToCallback(observerOrNext)
21
+ const unsubscribe = $effect.root(() => {
22
+ $effect(() => {
23
+ const value = getValue()
24
+ untrack(() => callback(value))
25
+ })
26
+ })
27
+
28
+ return { unsubscribe }
29
+ }
30
+
31
+ /**
32
+ * Creates the table-core reactivity bindings used by the Svelte adapter.
33
+ *
34
+ * Readonly table atoms are backed by `$derived.by`, writable atoms by `$state`,
35
+ * and subscriptions bridge through rune effects so table APIs participate in
36
+ * Svelte dependency tracking.
37
+ */
38
+ export function svelteReactivity(): TableReactivityBindings {
39
+ return {
40
+ createOptionsStore: true,
41
+ schedule: (fn) => queueMicrotask(() => fn()),
42
+ createReadonlyAtom: <T>(fn: () => T, _options?: TableAtomOptions<T>) => {
43
+ const value = $derived.by(fn)
44
+
45
+ return {
46
+ get: () => value,
47
+ subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {
48
+ return subscribeToRune(() => value, observerOrNext)
49
+ }) as ReadonlyAtom<T>['subscribe'],
50
+ }
51
+ },
52
+ createWritableAtom: <T>(
53
+ initialValue: T,
54
+ _options?: TableAtomOptions<T>,
55
+ ): Atom<T> => {
56
+ let value = $state(initialValue)
57
+
58
+ return {
59
+ set: (updater: T | ((prevVal: T) => T)) => {
60
+ value =
61
+ typeof updater === 'function'
62
+ ? (updater as (prevVal: T) => T)(value)
63
+ : updater
64
+ },
65
+ get: () => value,
66
+ subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {
67
+ return subscribeToRune(() => value, observerOrNext)
68
+ }) as Atom<T>['subscribe'],
69
+ }
70
+ },
71
+ untrack: untrack,
72
+ batch: (fn) => flushSync(fn),
73
+ }
74
+ }
@@ -1,23 +1,56 @@
1
- import type { Component, ComponentProps } from 'svelte'
1
+ import type { Component, ComponentProps, Snippet } from 'svelte'
2
2
 
3
3
  /**
4
- * A helper class to make it easy to identify Svelte components in `columnDef.cell` and `columnDef.header` properties.
4
+ * A helper class to make it easy to identify Svelte components in
5
+ * `columnDef.cell` and `columnDef.header` properties.
6
+ *
7
+ * > NOTE: This class should only be used internally by the adapter. If you're
8
+ * reading this and you don't know what this is for, you probably don't need it.
9
+ *
5
10
  * @example
6
11
  * ```svelte
7
- * {#if cell.column.columnDef.cell(cell.getContext()) instanceof RenderComponentConfig}
8
- * <svelte:component this={columnDef.cell.component} {...columnDef.cell.props} />
12
+ * {@const result = content(context as any)}
13
+ * {#if result instanceof RenderComponentConfig}
14
+ * {@const { component: Component, props } = result}
15
+ * <Component {...props} />
9
16
  * {/if}
10
17
  * ```
11
18
  * */
12
- export class RenderComponentConfig<TComponent extends Component<any>> {
19
+ export class RenderComponentConfig<TComponent extends Component> {
13
20
  constructor(
14
21
  public component: TComponent,
15
- public props: ComponentProps<TComponent>
22
+ public props?: ComponentProps<TComponent> | Record<string, never>,
16
23
  ) {}
17
24
  }
18
25
 
19
26
  /**
20
- * A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
27
+ * A helper class to make it easy to identify Svelte Snippets in `columnDef.cell` and `columnDef.header` properties.
28
+ *
29
+ * > NOTE: This class should only be used internally by the adapter. If you're
30
+ * reading this and you don't know what this is for, you probably don't need it.
31
+ *
32
+ * @example
33
+ * ```svelte
34
+ * {@const result = content(context as any)}
35
+ * {#if result instanceof RenderSnippetConfig}
36
+ * {@const { snippet, params } = result}
37
+ * {@render snippet(params)}
38
+ * {/if}
39
+ * ```
40
+ * */
41
+ export class RenderSnippetConfig<TProps> {
42
+ constructor(
43
+ public snippet: Snippet<[TProps]>,
44
+ public params?: TProps,
45
+ ) {}
46
+ }
47
+
48
+ /**
49
+ * Wraps a Svelte component so it can be returned from a column definition
50
+ * renderer such as `cell`, `header`, or `footer`.
51
+ *
52
+ * This is only to be used with Svelte Components - use `renderSnippet` for Svelte Snippets.
53
+ *
21
54
  * @param component A Svelte component
22
55
  * @param props The props to pass to `component`
23
56
  * @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
@@ -35,7 +68,40 @@ export class RenderComponentConfig<TComponent extends Component<any>> {
35
68
  * ```
36
69
  * @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
37
70
  */
38
- export const renderComponent = <TComponent extends Component<any>>(
71
+ export const renderComponent = <
72
+ TComponent extends Component<any>,
73
+ TProps extends ComponentProps<TComponent>,
74
+ >(
39
75
  component: TComponent,
40
- props: ComponentProps<TComponent>
76
+ props?: TProps,
41
77
  ) => new RenderComponentConfig(component, props)
78
+
79
+ /**
80
+ * Wraps a Svelte snippet so it can be returned from a column definition
81
+ * renderer such as `cell`, `header`, or `footer`.
82
+ *
83
+ * *The snippet must only take one parameter.*
84
+ *
85
+ * This is only to be used with Snippets - use `renderComponent` for Svelte Components.
86
+ *
87
+ * @param snippet The snippet to render.
88
+ * @param params The single parameter object passed to the snippet.
89
+ * @returns A `RenderSnippetConfig` consumed by the Svelte `FlexRender` component.
90
+ * @example
91
+ * ```ts
92
+ * // +page.svelte
93
+ * const defaultColumns = [
94
+ * columnHelper.accessor('name', {
95
+ * cell: cell => renderSnippet(nameSnippet, { name: cell.row.name }),
96
+ * }),
97
+ * columnHelper.accessor('state', {
98
+ * cell: cell => renderSnippet(stateSnippet, { state: cell.row.state }),
99
+ * }),
100
+ * ]
101
+ * ```
102
+ * @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
103
+ */
104
+ export const renderSnippet = <TProps>(
105
+ snippet: Snippet<[TProps]>,
106
+ params?: TProps,
107
+ ) => new RenderSnippetConfig(snippet, params)
@@ -0,0 +1 @@
1
+ export * from '@tanstack/table-core/static-functions'
@@ -0,0 +1,46 @@
1
+ import { shallow, useSelector } from '@tanstack/svelte-store'
2
+ import type {
3
+ Atom,
4
+ ReadonlyAtom,
5
+ ReadonlyStore,
6
+ Store,
7
+ } from '@tanstack/svelte-store'
8
+
9
+ export type SubscribeSource<TValue> =
10
+ | Atom<TValue>
11
+ | ReadonlyAtom<TValue>
12
+ | Store<TValue>
13
+ | ReadonlyStore<TValue>
14
+
15
+ /**
16
+ * Creates a fine-grained Svelte subscription to a TanStack Store source.
17
+ *
18
+ * Pass a table atom or store and optionally project it with a selector. The
19
+ * returned selector store exposes `.current`, making it useful for reading
20
+ * focused table state outside the broad `createTable` selector.
21
+ *
22
+ * @example
23
+ * ```svelte
24
+ * <script lang="ts">
25
+ * const selected = subscribeTable(
26
+ * table.atoms.rowSelection,
27
+ * (rowSelection) => rowSelection[row.id],
28
+ * )
29
+ * </script>
30
+ *
31
+ * <input type="checkbox" checked={!!selected.current} />
32
+ * ```
33
+ */
34
+ export function subscribeTable<TSourceValue>(
35
+ source: SubscribeSource<TSourceValue>,
36
+ ): ReturnType<typeof useSelector<TSourceValue>>
37
+ export function subscribeTable<TSourceValue, TSelected>(
38
+ source: SubscribeSource<TSourceValue>,
39
+ selector: (state: TSourceValue) => TSelected,
40
+ ): ReturnType<typeof useSelector<TSourceValue, TSelected>>
41
+ export function subscribeTable<TSourceValue, TSelected>(
42
+ source: SubscribeSource<TSourceValue>,
43
+ selector?: (state: TSourceValue) => TSelected,
44
+ ) {
45
+ return useSelector(source, selector, { compare: shallow })
46
+ }
@@ -1,35 +0,0 @@
1
- <script
2
- lang="ts"
3
- generics="TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>"
4
- >
5
- import type {
6
- CellContext,
7
- ColumnDefTemplate,
8
- HeaderContext,
9
- } from '@tanstack/table-core'
10
- import { RenderComponentConfig } from './render-component'
11
-
12
- type Props = {
13
- /** The cell or header field of the current cell's column definition. */
14
- content?: TContext extends HeaderContext<TData, TValue>
15
- ? ColumnDefTemplate<HeaderContext<TData, TValue>>
16
- : TContext extends CellContext<TData, TValue>
17
- ? ColumnDefTemplate<CellContext<TData, TValue>>
18
- : never
19
- /** The result of the `getContext()` function of the header or cell */
20
- context: TContext
21
- }
22
-
23
- let { content, context }: Props = $props()
24
- </script>
25
-
26
- {#if typeof content === 'string'}
27
- {content}
28
- {:else if content instanceof Function}
29
- {@const result = content(context as any)}
30
- {#if result instanceof RenderComponentConfig}
31
- <svelte:component this={result.component} {...result.props} />
32
- {:else}
33
- {result}
34
- {/if}
35
- {/if}
@@ -1,28 +0,0 @@
1
- /// <reference types="svelte" />
2
- import type { CellContext, ColumnDefTemplate, HeaderContext } from '@tanstack/table-core';
3
- declare class __sveltets_Render<TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>> {
4
- props(): {
5
- /** The cell or header field of the current cell's column definition. */
6
- content?: (TContext extends HeaderContext<TData, TValue> ? ColumnDefTemplate<HeaderContext<TData, TValue>> : TContext extends CellContext<TData, TValue> ? ColumnDefTemplate<CellContext<TData, TValue>> : never) | undefined;
7
- /** The result of the `getContext()` function of the header or cell */
8
- context: TContext;
9
- };
10
- events(): {} & {
11
- [evt: string]: CustomEvent<any>;
12
- };
13
- slots(): {};
14
- bindings(): "";
15
- exports(): {};
16
- }
17
- interface $$IsomorphicComponent {
18
- new <TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<TData, TValue, TContext>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<TData, TValue, TContext>['props']>, ReturnType<__sveltets_Render<TData, TValue, TContext>['events']>, ReturnType<__sveltets_Render<TData, TValue, TContext>['slots']>> & {
19
- $$bindings?: ReturnType<__sveltets_Render<TData, TValue, TContext>['bindings']>;
20
- } & ReturnType<__sveltets_Render<TData, TValue, TContext>['exports']>;
21
- <TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>>(internal: unknown, props: ReturnType<__sveltets_Render<TData, TValue, TContext>['props']> & {
22
- $$events?: ReturnType<__sveltets_Render<TData, TValue, TContext>['events']>;
23
- }): ReturnType<__sveltets_Render<TData, TValue, TContext>['exports']>;
24
- z_$$bindings?: ReturnType<__sveltets_Render<any, any, any>['bindings']>;
25
- }
26
- declare const FlexRender: $$IsomorphicComponent;
27
- type FlexRender<TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>> = InstanceType<typeof FlexRender<TData, TValue, TContext>>;
28
- export default FlexRender;
@@ -1,28 +0,0 @@
1
- import { type RowData, type TableOptions } from '@tanstack/table-core';
2
- /**
3
- * Creates a reactive TanStack table object for Svelte.
4
- * @param options Table options to create the table with.
5
- * @returns A reactive table object.
6
- * @example
7
- * ```svelte
8
- * <script>
9
- * const table = createSvelteTable({ ... })
10
- * </script>
11
- *
12
- * <table>
13
- * <thead>
14
- * {#each table.getHeaderGroups() as headerGroup}
15
- * <tr>
16
- * {#each headerGroup.headers as header}
17
- * <th colspan={header.colSpan}>
18
- * <FlexRender content={header.column.columnDef.header} context={header.getContext()} />
19
- * </th>
20
- * {/each}
21
- * </tr>
22
- * {/each}
23
- * </thead>
24
- * <!-- ... -->
25
- * </table>
26
- * ```
27
- */
28
- export declare function createSvelteTable<TData extends RowData>(options: TableOptions<TData>): import("@tanstack/table-core").Table<TData>;
@@ -1,87 +0,0 @@
1
- import { createTable, } from '@tanstack/table-core';
2
- /**
3
- * Creates a reactive TanStack table object for Svelte.
4
- * @param options Table options to create the table with.
5
- * @returns A reactive table object.
6
- * @example
7
- * ```svelte
8
- * <script>
9
- * const table = createSvelteTable({ ... })
10
- * </script>
11
- *
12
- * <table>
13
- * <thead>
14
- * {#each table.getHeaderGroups() as headerGroup}
15
- * <tr>
16
- * {#each headerGroup.headers as header}
17
- * <th colspan={header.colSpan}>
18
- * <FlexRender content={header.column.columnDef.header} context={header.getContext()} />
19
- * </th>
20
- * {/each}
21
- * </tr>
22
- * {/each}
23
- * </thead>
24
- * <!-- ... -->
25
- * </table>
26
- * ```
27
- */
28
- export function createSvelteTable(options) {
29
- const resolvedOptions = mergeObjects({
30
- state: {},
31
- onStateChange() { },
32
- renderFallbackValue: null,
33
- mergeOptions: (defaultOptions, options) => {
34
- return mergeObjects(defaultOptions, options);
35
- },
36
- }, options);
37
- const table = createTable(resolvedOptions);
38
- let state = $state(table.initialState);
39
- function updateOptions() {
40
- table.setOptions(prev => {
41
- return mergeObjects(prev, options, {
42
- state: mergeObjects(state, options.state || {}),
43
- onStateChange: (updater) => {
44
- if (updater instanceof Function)
45
- state = updater(state);
46
- else
47
- state = mergeObjects(state, updater);
48
- options.onStateChange?.(updater);
49
- },
50
- });
51
- });
52
- }
53
- updateOptions();
54
- $effect.pre(() => {
55
- updateOptions();
56
- });
57
- return table;
58
- }
59
- function mergeObjects(...sources) {
60
- const target = {};
61
- for (let i = 0; i < sources.length; i++) {
62
- let source = sources[i];
63
- if (typeof source === 'function')
64
- source = source();
65
- if (source) {
66
- const descriptors = Object.getOwnPropertyDescriptors(source);
67
- for (const key in descriptors) {
68
- if (key in target)
69
- continue;
70
- Object.defineProperty(target, key, {
71
- enumerable: true,
72
- get() {
73
- for (let i = sources.length - 1; i >= 0; i--) {
74
- let v, s = sources[i];
75
- if (typeof s === 'function')
76
- s = s();
77
- v = (s || {})[key];
78
- if (v !== undefined)
79
- return v;
80
- }
81
- },
82
- });
83
- }
84
- }
85
- }
86
- return target;
87
- }
@@ -1,35 +0,0 @@
1
- <script
2
- lang="ts"
3
- generics="TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>"
4
- >
5
- import type {
6
- CellContext,
7
- ColumnDefTemplate,
8
- HeaderContext,
9
- } from '@tanstack/table-core'
10
- import { RenderComponentConfig } from './render-component'
11
-
12
- type Props = {
13
- /** The cell or header field of the current cell's column definition. */
14
- content?: TContext extends HeaderContext<TData, TValue>
15
- ? ColumnDefTemplate<HeaderContext<TData, TValue>>
16
- : TContext extends CellContext<TData, TValue>
17
- ? ColumnDefTemplate<CellContext<TData, TValue>>
18
- : never
19
- /** The result of the `getContext()` function of the header or cell */
20
- context: TContext
21
- }
22
-
23
- let { content, context }: Props = $props()
24
- </script>
25
-
26
- {#if typeof content === 'string'}
27
- {content}
28
- {:else if content instanceof Function}
29
- {@const result = content(context as any)}
30
- {#if result instanceof RenderComponentConfig}
31
- <svelte:component this={result.component} {...result.props} />
32
- {:else}
33
- {result}
34
- {/if}
35
- {/if}
@@ -1,117 +0,0 @@
1
- import {
2
- createTable,
3
- type RowData,
4
- type TableOptions,
5
- type TableOptionsResolved,
6
- type TableState,
7
- } from '@tanstack/table-core'
8
-
9
- /**
10
- * Creates a reactive TanStack table object for Svelte.
11
- * @param options Table options to create the table with.
12
- * @returns A reactive table object.
13
- * @example
14
- * ```svelte
15
- * <script>
16
- * const table = createSvelteTable({ ... })
17
- * </script>
18
- *
19
- * <table>
20
- * <thead>
21
- * {#each table.getHeaderGroups() as headerGroup}
22
- * <tr>
23
- * {#each headerGroup.headers as header}
24
- * <th colspan={header.colSpan}>
25
- * <FlexRender content={header.column.columnDef.header} context={header.getContext()} />
26
- * </th>
27
- * {/each}
28
- * </tr>
29
- * {/each}
30
- * </thead>
31
- * <!-- ... -->
32
- * </table>
33
- * ```
34
- */
35
- export function createSvelteTable<TData extends RowData>(
36
- options: TableOptions<TData>
37
- ) {
38
- const resolvedOptions: TableOptionsResolved<TData> = mergeObjects(
39
- {
40
- state: {},
41
- onStateChange() {},
42
- renderFallbackValue: null,
43
- mergeOptions: (
44
- defaultOptions: TableOptions<TData>,
45
- options: Partial<TableOptions<TData>>
46
- ) => {
47
- return mergeObjects(defaultOptions, options)
48
- },
49
- },
50
- options
51
- )
52
-
53
- const table = createTable(resolvedOptions)
54
- let state = $state<Partial<TableState>>(table.initialState)
55
-
56
- function updateOptions() {
57
- table.setOptions(prev => {
58
- return mergeObjects(prev, options, {
59
- state: mergeObjects(state, options.state || {}),
60
- onStateChange: (updater: any) => {
61
- if (updater instanceof Function) state = updater(state)
62
- else state = mergeObjects(state, updater)
63
-
64
- options.onStateChange?.(updater)
65
- },
66
- })
67
- })
68
- }
69
-
70
- updateOptions()
71
-
72
- $effect.pre(() => {
73
- updateOptions()
74
- })
75
-
76
- return table
77
- }
78
-
79
- /**
80
- * Merges objects together while keeping their getters alive.
81
- * Taken from SolidJS: {@link https://github.com/solidjs/solid/blob/24abc825c0996fd2bc8c1de1491efe9a7e743aff/packages/solid/src/server/rendering.ts#L82-L115}
82
- * */
83
- function mergeObjects<T>(source: T): T
84
- function mergeObjects<T, U>(source: T, source1: U): T & U
85
- function mergeObjects<T, U, V>(source: T, source1: U, source2: V): T & U & V
86
- function mergeObjects<T, U, V, W>(
87
- source: T,
88
- source1: U,
89
- source2: V,
90
- source3: W
91
- ): T & U & V & W
92
- function mergeObjects(...sources: any): any {
93
- const target = {}
94
- for (let i = 0; i < sources.length; i++) {
95
- let source = sources[i]
96
- if (typeof source === 'function') source = source()
97
- if (source) {
98
- const descriptors = Object.getOwnPropertyDescriptors(source)
99
- for (const key in descriptors) {
100
- if (key in target) continue
101
- Object.defineProperty(target, key, {
102
- enumerable: true,
103
- get() {
104
- for (let i = sources.length - 1; i >= 0; i--) {
105
- let v,
106
- s = sources[i]
107
- if (typeof s === 'function') s = s()
108
- v = (s || {})[key]
109
- if (v !== undefined) return v
110
- }
111
- },
112
- })
113
- }
114
- }
115
- }
116
- return target
117
- }