@tanstack/svelte-table 9.0.0-alpha.2 → 9.0.0-alpha.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -0
- package/dist/AppCell.svelte +13 -0
- package/dist/AppCell.svelte.d.ts +9 -0
- package/dist/AppHeader.svelte +13 -0
- package/dist/AppHeader.svelte.d.ts +9 -0
- package/dist/AppTable.svelte +11 -0
- package/dist/AppTable.svelte.d.ts +7 -0
- package/dist/FlexRender.svelte +103 -0
- package/dist/FlexRender.svelte.d.ts +51 -0
- package/dist/Subscribe.svelte +45 -0
- package/dist/Subscribe.svelte.d.ts +44 -0
- package/dist/context-keys.d.ts +3 -0
- package/dist/context-keys.js +3 -0
- package/dist/createTable.svelte.d.ts +32 -0
- package/dist/createTable.svelte.js +82 -0
- package/dist/createTableHook.svelte.d.ts +236 -0
- package/dist/createTableHook.svelte.js +170 -0
- package/dist/createTableState.svelte.d.ts +2 -0
- package/dist/createTableState.svelte.js +12 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.js +6 -3
- package/dist/merge-objects.d.ts +8 -0
- package/dist/merge-objects.js +29 -0
- package/dist/render-component.d.ts +64 -9
- package/dist/render-component.js +60 -4
- package/package.json +17 -9
- package/src/AppCell.svelte +13 -0
- package/src/AppHeader.svelte +13 -0
- package/src/AppTable.svelte +11 -0
- package/src/FlexRender.svelte +103 -0
- package/src/Subscribe.svelte +45 -0
- package/src/context-keys.ts +3 -0
- package/src/createTable.svelte.ts +165 -0
- package/src/createTableHook.svelte.ts +636 -0
- package/src/createTableState.svelte.ts +15 -0
- package/src/index.ts +15 -3
- package/src/merge-objects.ts +43 -0
- package/src/render-component.ts +74 -10
- package/dist/flex-render.svelte +0 -17
- package/dist/flex-render.svelte.d.ts +0 -28
- package/dist/table.svelte.d.ts +0 -36
- package/dist/table.svelte.js +0 -87
- package/src/flex-render.svelte +0 -35
- package/src/table.svelte.ts +0 -121
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
}
|
package/src/render-component.ts
CHANGED
|
@@ -1,23 +1,55 @@
|
|
|
1
|
-
import type {
|
|
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
|
|
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
|
-
* {
|
|
8
|
-
*
|
|
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
|
|
19
|
+
export class RenderComponentConfig<TComponent extends Component> {
|
|
13
20
|
constructor(
|
|
14
|
-
public component:
|
|
15
|
-
public props
|
|
21
|
+
public component: TComponent,
|
|
22
|
+
public props?: ComponentProps<TComponent> | Record<string, never>,
|
|
23
|
+
) {}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
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,
|
|
16
45
|
) {}
|
|
17
46
|
}
|
|
18
47
|
|
|
19
48
|
/**
|
|
20
49
|
* A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
|
|
50
|
+
*
|
|
51
|
+
* This is only to be used with Svelte Components - use `renderSnippet` for Svelte Snippets.
|
|
52
|
+
*
|
|
21
53
|
* @param component A Svelte component
|
|
22
54
|
* @param props The props to pass to `component`
|
|
23
55
|
* @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
|
|
@@ -35,7 +67,39 @@ export class RenderComponentConfig<TComponent extends SvelteComponent> {
|
|
|
35
67
|
* ```
|
|
36
68
|
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
37
69
|
*/
|
|
38
|
-
export const renderComponent = <
|
|
39
|
-
|
|
40
|
-
|
|
70
|
+
export const renderComponent = <
|
|
71
|
+
TComponent extends Component<any>,
|
|
72
|
+
TProps extends ComponentProps<TComponent>,
|
|
73
|
+
>(
|
|
74
|
+
component: TComponent,
|
|
75
|
+
props?: TProps,
|
|
41
76
|
) => new RenderComponentConfig(component, props)
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A helper function to help create cells from Svelte Snippets through ColumnDef's `cell` and `header` properties.
|
|
80
|
+
*
|
|
81
|
+
* *The snippet must only take one parameter.*
|
|
82
|
+
*
|
|
83
|
+
* This is only to be used with Snippets - use `renderComponent` for Svelte Components.
|
|
84
|
+
*
|
|
85
|
+
* @param snippet
|
|
86
|
+
* @param params
|
|
87
|
+
* @returns
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* // +page.svelte
|
|
91
|
+
* const defaultColumns = [
|
|
92
|
+
* columnHelper.accessor('name', {
|
|
93
|
+
* cell: cell => renderSnippet(nameSnippet, { name: cell.row.name }),
|
|
94
|
+
* }),
|
|
95
|
+
* columnHelper.accessor('state', {
|
|
96
|
+
* cell: cell => renderSnippet(stateSnippet, { state: cell.row.state }),
|
|
97
|
+
* }),
|
|
98
|
+
* ]
|
|
99
|
+
* ```
|
|
100
|
+
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
101
|
+
*/
|
|
102
|
+
export const renderSnippet = <TProps>(
|
|
103
|
+
snippet: Snippet<[TProps]>,
|
|
104
|
+
params?: TProps,
|
|
105
|
+
) => new RenderSnippetConfig(snippet, params)
|
package/dist/flex-render.svelte
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<script
|
|
2
|
-
lang="ts"
|
|
3
|
-
generics="TData, TValue, TContext extends HeaderContext<TData, TValue> | CellContext<TData, TValue>"
|
|
4
|
-
>import { RenderComponentConfig } from "./render-component";
|
|
5
|
-
let { content, context } = $props();
|
|
6
|
-
</script>
|
|
7
|
-
|
|
8
|
-
{#if typeof content === 'string'}
|
|
9
|
-
{content}
|
|
10
|
-
{:else if content instanceof Function}
|
|
11
|
-
{@const result = content(context as any)}
|
|
12
|
-
{#if result instanceof RenderComponentConfig}
|
|
13
|
-
<svelte:component this={result.component} {...result.props} />
|
|
14
|
-
{:else}
|
|
15
|
-
{result}
|
|
16
|
-
{/if}
|
|
17
|
-
{/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;
|
package/dist/table.svelte.d.ts
DELETED
|
@@ -1,36 +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>;
|
|
29
|
-
/**
|
|
30
|
-
* Merges objects together while keeping their getters alive.
|
|
31
|
-
* Taken from SolidJS: {@link https://github.com/solidjs/solid/blob/24abc825c0996fd2bc8c1de1491efe9a7e743aff/packages/solid/src/server/rendering.ts#L82-L115}
|
|
32
|
-
* */
|
|
33
|
-
export declare function mergeObjects<T>(source: T): T;
|
|
34
|
-
export declare function mergeObjects<T, U>(source: T, source1: U): T & U;
|
|
35
|
-
export declare function mergeObjects<T, U, V>(source: T, source1: U, source2: V): T & U & V;
|
|
36
|
-
export declare function mergeObjects<T, U, V, W>(source: T, source1: U, source2: V, source3: W): T & U & V & W;
|
package/dist/table.svelte.js
DELETED
|
@@ -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
|
-
export 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
|
-
}
|
package/src/flex-render.svelte
DELETED
|
@@ -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}
|
package/src/table.svelte.ts
DELETED
|
@@ -1,121 +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
|
-
export function mergeObjects<T>(source: T): T
|
|
84
|
-
export function mergeObjects<T, U>(source: T, source1: U): T & U
|
|
85
|
-
export function mergeObjects<T, U, V>(
|
|
86
|
-
source: T,
|
|
87
|
-
source1: U,
|
|
88
|
-
source2: V
|
|
89
|
-
): T & U & V
|
|
90
|
-
export function mergeObjects<T, U, V, W>(
|
|
91
|
-
source: T,
|
|
92
|
-
source1: U,
|
|
93
|
-
source2: V,
|
|
94
|
-
source3: W
|
|
95
|
-
): T & U & V & W
|
|
96
|
-
export function mergeObjects(...sources: any): any {
|
|
97
|
-
const target = {}
|
|
98
|
-
for (let i = 0; i < sources.length; i++) {
|
|
99
|
-
let source = sources[i]
|
|
100
|
-
if (typeof source === 'function') source = source()
|
|
101
|
-
if (source) {
|
|
102
|
-
const descriptors = Object.getOwnPropertyDescriptors(source)
|
|
103
|
-
for (const key in descriptors) {
|
|
104
|
-
if (key in target) continue
|
|
105
|
-
Object.defineProperty(target, key, {
|
|
106
|
-
enumerable: true,
|
|
107
|
-
get() {
|
|
108
|
-
for (let i = sources.length - 1; i >= 0; i--) {
|
|
109
|
-
let v,
|
|
110
|
-
s = sources[i]
|
|
111
|
-
if (typeof s === 'function') s = s()
|
|
112
|
-
v = (s || {})[key]
|
|
113
|
-
if (v !== undefined) return v
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return target
|
|
121
|
-
}
|