@tanstack/svelte-table 9.0.0-alpha.0 → 9.0.0-alpha.4
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/dist/flex-render.svelte +35 -0
- package/dist/flex-render.svelte.d.ts +28 -0
- package/dist/index.d.ts +3 -7
- package/dist/index.js +3 -93
- package/dist/render-component.d.ts +35 -4
- package/dist/render-component.js +34 -47
- package/dist/table.svelte.d.ts +28 -0
- package/dist/table.svelte.js +87 -0
- package/package.json +6 -5
- package/src/flex-render.svelte +35 -0
- package/src/index.ts +3 -124
- package/src/render-component.ts +38 -86
- package/src/table.svelte.ts +117 -0
- package/dist/placeholder.d.ts +0 -4
- package/dist/placeholder.js +0 -8
- package/dist/placeholder.svelte +0 -5
- package/dist/placeholder.svelte.d.ts +0 -23
- package/src/placeholder.svelte +0 -5
- package/src/placeholder.ts +0 -15
|
@@ -0,0 +1,35 @@
|
|
|
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}
|
|
@@ -0,0 +1,28 @@
|
|
|
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/index.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import { RowData, TableOptions } from '@tanstack/table-core';
|
|
2
|
-
import type { ComponentType } from 'svelte';
|
|
3
|
-
import { Readable } from 'svelte/store';
|
|
4
|
-
export { renderComponent } from './render-component';
|
|
5
1
|
export * from '@tanstack/table-core';
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
export
|
|
2
|
+
export { default as FlexRender } from './flex-render.svelte';
|
|
3
|
+
export { renderComponent } from './render-component';
|
|
4
|
+
export { createSvelteTable } from './table.svelte';
|
package/dist/index.js
CHANGED
|
@@ -1,94 +1,4 @@
|
|
|
1
|
-
import { createTable, } from '@tanstack/table-core';
|
|
2
|
-
import Placeholder from './placeholder';
|
|
3
|
-
import { SvelteComponent } from 'svelte/internal';
|
|
4
|
-
import { readable, writable, derived, get } from 'svelte/store';
|
|
5
|
-
import { renderComponent } from './render-component';
|
|
6
|
-
export { renderComponent } from './render-component';
|
|
7
1
|
export * from '@tanstack/table-core';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
typeof component.render === 'function');
|
|
12
|
-
}
|
|
13
|
-
function isSvelteClientComponent(component) {
|
|
14
|
-
let isHMR = '__SVELTE_HMR' in window;
|
|
15
|
-
return (component.prototype instanceof SvelteComponent ||
|
|
16
|
-
(isHMR &&
|
|
17
|
-
component.name?.startsWith('Proxy<') &&
|
|
18
|
-
component.name?.endsWith('>')));
|
|
19
|
-
}
|
|
20
|
-
function isSvelteComponent(component) {
|
|
21
|
-
if (typeof document === 'undefined') {
|
|
22
|
-
return isSvelteServerComponent(component);
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
return isSvelteClientComponent(component);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function wrapInPlaceholder(content) {
|
|
29
|
-
return renderComponent(Placeholder, { content });
|
|
30
|
-
}
|
|
31
|
-
export function flexRender(component, props) {
|
|
32
|
-
if (!component)
|
|
33
|
-
return null;
|
|
34
|
-
if (isSvelteComponent(component)) {
|
|
35
|
-
return renderComponent(component, props);
|
|
36
|
-
}
|
|
37
|
-
if (typeof component === 'function') {
|
|
38
|
-
const result = component(props);
|
|
39
|
-
if (result === null || result === undefined)
|
|
40
|
-
return null;
|
|
41
|
-
if (isSvelteComponent(result)) {
|
|
42
|
-
return renderComponent(result, props);
|
|
43
|
-
}
|
|
44
|
-
return wrapInPlaceholder(result);
|
|
45
|
-
}
|
|
46
|
-
return wrapInPlaceholder(component);
|
|
47
|
-
}
|
|
48
|
-
export function createSvelteTable(options) {
|
|
49
|
-
let optionsStore;
|
|
50
|
-
if ('subscribe' in options) {
|
|
51
|
-
optionsStore = options;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
optionsStore = readable(options);
|
|
55
|
-
}
|
|
56
|
-
let resolvedOptions = {
|
|
57
|
-
state: {}, // Dummy state
|
|
58
|
-
onStateChange: () => { }, // noop
|
|
59
|
-
renderFallbackValue: null,
|
|
60
|
-
...get(optionsStore),
|
|
61
|
-
};
|
|
62
|
-
let table = createTable(resolvedOptions);
|
|
63
|
-
let stateStore = writable(/** @type {number} */ table.initialState);
|
|
64
|
-
// combine stores
|
|
65
|
-
let stateOptionsStore = derived([stateStore, optionsStore], s => s);
|
|
66
|
-
const tableReadable = readable(table, function start(set) {
|
|
67
|
-
const unsubscribe = stateOptionsStore.subscribe(([state, options]) => {
|
|
68
|
-
table.setOptions(prev => {
|
|
69
|
-
return {
|
|
70
|
-
...prev,
|
|
71
|
-
...options,
|
|
72
|
-
state: { ...state, ...options.state },
|
|
73
|
-
// Similarly, we'll maintain both our internal state and any user-provided
|
|
74
|
-
// state.
|
|
75
|
-
onStateChange: updater => {
|
|
76
|
-
if (updater instanceof Function) {
|
|
77
|
-
stateStore.update(updater);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
stateStore.set(updater);
|
|
81
|
-
}
|
|
82
|
-
resolvedOptions.onStateChange?.(updater);
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
});
|
|
86
|
-
// it didn't seem to rerender without setting the table
|
|
87
|
-
set(table);
|
|
88
|
-
});
|
|
89
|
-
return function stop() {
|
|
90
|
-
unsubscribe();
|
|
91
|
-
};
|
|
92
|
-
});
|
|
93
|
-
return tableReadable;
|
|
94
|
-
}
|
|
2
|
+
export { default as FlexRender } from './flex-render.svelte';
|
|
3
|
+
export { renderComponent } from './render-component';
|
|
4
|
+
export { createSvelteTable } from './table.svelte';
|
|
@@ -1,4 +1,35 @@
|
|
|
1
|
-
import type { ComponentType, ComponentProps } from 'svelte';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type { SvelteComponent, ComponentType, ComponentProps } from 'svelte';
|
|
2
|
+
/**
|
|
3
|
+
* A helper class to make it easy to identify Svelte components in `columnDef.cell` and `columnDef.header` properties.
|
|
4
|
+
* @example
|
|
5
|
+
* ```svelte
|
|
6
|
+
* {#if cell.column.columnDef.cell(cell.getContext()) instanceof RenderComponentConfig}
|
|
7
|
+
* <svelte:component this={columnDef.cell.component} {...columnDef.cell.props} />
|
|
8
|
+
* {/if}
|
|
9
|
+
* ```
|
|
10
|
+
* */
|
|
11
|
+
export declare class RenderComponentConfig<TComponent extends SvelteComponent> {
|
|
12
|
+
component: ComponentType<TComponent>;
|
|
13
|
+
props: ComponentProps<TComponent> | Record<string, never>;
|
|
14
|
+
constructor(component: ComponentType<TComponent>, props?: ComponentProps<TComponent> | Record<string, never>);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
|
|
18
|
+
* @param component A Svelte component
|
|
19
|
+
* @param props The props to pass to `component`
|
|
20
|
+
* @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // +page.svelte
|
|
24
|
+
* const defaultColumns = [
|
|
25
|
+
* columnHelper.accessor('name', {
|
|
26
|
+
* header: header => renderComponent(SortHeader, { label: 'Name', header }),
|
|
27
|
+
* }),
|
|
28
|
+
* columnHelper.accessor('state', {
|
|
29
|
+
* header: header => renderComponent(SortHeader, { label: 'State', header }),
|
|
30
|
+
* }),
|
|
31
|
+
* ]
|
|
32
|
+
* ```
|
|
33
|
+
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
34
|
+
*/
|
|
35
|
+
export declare const renderComponent: <TComponent extends SvelteComponent<any, any, any>>(component: ComponentType<TComponent>, props: ComponentProps<TComponent>) => RenderComponentConfig<TComponent>;
|
package/dist/render-component.js
CHANGED
|
@@ -1,48 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
mount_component(c, target, anchor);
|
|
16
|
-
current = true;
|
|
17
|
-
},
|
|
18
|
-
p: noop,
|
|
19
|
-
i(local) {
|
|
20
|
-
if (current)
|
|
21
|
-
return;
|
|
22
|
-
transition_in(c.$$.fragment, local);
|
|
23
|
-
current = true;
|
|
24
|
-
},
|
|
25
|
-
o(local) {
|
|
26
|
-
transition_out(c.$$.fragment, local);
|
|
27
|
-
current = false;
|
|
28
|
-
},
|
|
29
|
-
d(detaching) {
|
|
30
|
-
destroy_component(c, detaching);
|
|
31
|
-
},
|
|
32
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* A helper class to make it easy to identify Svelte components in `columnDef.cell` and `columnDef.header` properties.
|
|
3
|
+
* @example
|
|
4
|
+
* ```svelte
|
|
5
|
+
* {#if cell.column.columnDef.cell(cell.getContext()) instanceof RenderComponentConfig}
|
|
6
|
+
* <svelte:component this={columnDef.cell.component} {...columnDef.cell.props} />
|
|
7
|
+
* {/if}
|
|
8
|
+
* ```
|
|
9
|
+
* */
|
|
10
|
+
export class RenderComponentConfig {
|
|
11
|
+
constructor(component, props = {}) {
|
|
12
|
+
this.component = component;
|
|
13
|
+
this.props = props;
|
|
14
|
+
}
|
|
33
15
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
16
|
+
/**
|
|
17
|
+
* A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
|
|
18
|
+
* @param component A Svelte component
|
|
19
|
+
* @param props The props to pass to `component`
|
|
20
|
+
* @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // +page.svelte
|
|
24
|
+
* const defaultColumns = [
|
|
25
|
+
* columnHelper.accessor('name', {
|
|
26
|
+
* header: header => renderComponent(SortHeader, { label: 'Name', header }),
|
|
27
|
+
* }),
|
|
28
|
+
* columnHelper.accessor('state', {
|
|
29
|
+
* header: header => renderComponent(SortHeader, { label: 'State', header }),
|
|
30
|
+
* }),
|
|
31
|
+
* ]
|
|
32
|
+
* ```
|
|
33
|
+
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
34
|
+
*/
|
|
35
|
+
export const renderComponent = (component, props) => new RenderComponentConfig(component, props);
|
|
@@ -0,0 +1,28 @@
|
|
|
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>;
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-table",
|
|
3
|
-
"version": "9.0.0-alpha.
|
|
3
|
+
"version": "9.0.0-alpha.4",
|
|
4
4
|
"description": "Headless UI for building powerful tables & datagrids for Svelte.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,15 +40,16 @@
|
|
|
40
40
|
"src"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@tanstack/table-core": "9.0.0-alpha.
|
|
43
|
+
"@tanstack/table-core": "9.0.0-alpha.4"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@sveltejs/package": "^2.3.1",
|
|
47
|
-
"@sveltejs/vite-plugin-svelte": "^
|
|
48
|
-
"svelte": "^
|
|
47
|
+
"@sveltejs/vite-plugin-svelte": "^4.0.0-next",
|
|
48
|
+
"svelte": "^5.0.0-next",
|
|
49
|
+
"svelte-check": "^3.7.0"
|
|
49
50
|
},
|
|
50
51
|
"peerDependencies": {
|
|
51
|
-
"svelte": "^
|
|
52
|
+
"svelte": "^5.0.0-next"
|
|
52
53
|
},
|
|
53
54
|
"scripts": {}
|
|
54
55
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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/index.ts
CHANGED
|
@@ -1,125 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
RowData,
|
|
3
|
-
createTable,
|
|
4
|
-
TableOptions,
|
|
5
|
-
TableOptionsResolved,
|
|
6
|
-
} from '@tanstack/table-core'
|
|
7
|
-
import Placeholder from './placeholder'
|
|
8
|
-
import type { ComponentType } from 'svelte'
|
|
9
|
-
import { SvelteComponent } from 'svelte/internal'
|
|
10
|
-
import { readable, writable, derived, Readable, get } from 'svelte/store'
|
|
11
|
-
import { renderComponent } from './render-component'
|
|
12
|
-
|
|
13
|
-
export { renderComponent } from './render-component'
|
|
14
|
-
|
|
15
1
|
export * from '@tanstack/table-core'
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
typeof component === 'object' &&
|
|
20
|
-
typeof component.$$render === 'function' &&
|
|
21
|
-
typeof component.render === 'function'
|
|
22
|
-
)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function isSvelteClientComponent(component: any) {
|
|
26
|
-
let isHMR = '__SVELTE_HMR' in window
|
|
27
|
-
|
|
28
|
-
return (
|
|
29
|
-
component.prototype instanceof SvelteComponent ||
|
|
30
|
-
(isHMR &&
|
|
31
|
-
component.name?.startsWith('Proxy<') &&
|
|
32
|
-
component.name?.endsWith('>'))
|
|
33
|
-
)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function isSvelteComponent(component: any) {
|
|
37
|
-
if (typeof document === 'undefined') {
|
|
38
|
-
return isSvelteServerComponent(component)
|
|
39
|
-
} else {
|
|
40
|
-
return isSvelteClientComponent(component)
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function wrapInPlaceholder(content: any) {
|
|
45
|
-
return renderComponent(Placeholder, { content })
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function flexRender(component: any, props: any): ComponentType | null {
|
|
49
|
-
if (!component) return null
|
|
50
|
-
|
|
51
|
-
if (isSvelteComponent(component)) {
|
|
52
|
-
return renderComponent(component, props)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (typeof component === 'function') {
|
|
56
|
-
const result = component(props)
|
|
57
|
-
if (result === null || result === undefined) return null
|
|
58
|
-
|
|
59
|
-
if (isSvelteComponent(result)) {
|
|
60
|
-
return renderComponent(result, props)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return wrapInPlaceholder(result)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return wrapInPlaceholder(component)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
type ReadableOrVal<T> = T | Readable<T>
|
|
70
|
-
|
|
71
|
-
export function createSvelteTable<TData extends RowData>(
|
|
72
|
-
options: ReadableOrVal<TableOptions<TData>>
|
|
73
|
-
) {
|
|
74
|
-
let optionsStore: Readable<TableOptions<TData>>
|
|
75
|
-
|
|
76
|
-
if ('subscribe' in options) {
|
|
77
|
-
optionsStore = options
|
|
78
|
-
} else {
|
|
79
|
-
optionsStore = readable(options)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
let resolvedOptions: TableOptionsResolved<TData> = {
|
|
83
|
-
state: {}, // Dummy state
|
|
84
|
-
onStateChange: () => {}, // noop
|
|
85
|
-
renderFallbackValue: null,
|
|
86
|
-
...get(optionsStore),
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let table = createTable(resolvedOptions)
|
|
90
|
-
|
|
91
|
-
let stateStore = writable(/** @type {number} */ table.initialState)
|
|
92
|
-
// combine stores
|
|
93
|
-
let stateOptionsStore = derived([stateStore, optionsStore], s => s)
|
|
94
|
-
const tableReadable = readable(table, function start(set) {
|
|
95
|
-
const unsubscribe = stateOptionsStore.subscribe(([state, options]) => {
|
|
96
|
-
table.setOptions(prev => {
|
|
97
|
-
return {
|
|
98
|
-
...prev,
|
|
99
|
-
...options,
|
|
100
|
-
state: { ...state, ...options.state },
|
|
101
|
-
// Similarly, we'll maintain both our internal state and any user-provided
|
|
102
|
-
// state.
|
|
103
|
-
onStateChange: updater => {
|
|
104
|
-
if (updater instanceof Function) {
|
|
105
|
-
stateStore.update(updater)
|
|
106
|
-
} else {
|
|
107
|
-
stateStore.set(updater)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
resolvedOptions.onStateChange?.(updater)
|
|
111
|
-
},
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
// it didn't seem to rerender without setting the table
|
|
116
|
-
set(table)
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
return function stop() {
|
|
120
|
-
unsubscribe()
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
return tableReadable
|
|
125
|
-
}
|
|
2
|
+
export { default as FlexRender } from './flex-render.svelte'
|
|
3
|
+
export { renderComponent } from './render-component'
|
|
4
|
+
export { createSvelteTable } from './table.svelte'
|
package/src/render-component.ts
CHANGED
|
@@ -1,89 +1,41 @@
|
|
|
1
|
-
import type { ComponentType, ComponentProps } from 'svelte'
|
|
2
|
-
import {
|
|
3
|
-
SvelteComponent,
|
|
4
|
-
claim_component,
|
|
5
|
-
create_component,
|
|
6
|
-
destroy_component,
|
|
7
|
-
init,
|
|
8
|
-
mount_component,
|
|
9
|
-
noop,
|
|
10
|
-
safe_not_equal,
|
|
11
|
-
transition_in,
|
|
12
|
-
transition_out,
|
|
13
|
-
create_ssr_component,
|
|
14
|
-
validate_component,
|
|
15
|
-
} from 'svelte/internal'
|
|
1
|
+
import type { SvelteComponent, ComponentType, ComponentProps } from 'svelte'
|
|
16
2
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
mount_component(c, target, anchor)
|
|
32
|
-
current = true
|
|
33
|
-
},
|
|
34
|
-
p: noop,
|
|
35
|
-
i(local: any) {
|
|
36
|
-
if (current) return
|
|
37
|
-
transition_in(c.$$.fragment, local)
|
|
38
|
-
current = true
|
|
39
|
-
},
|
|
40
|
-
o(local: any) {
|
|
41
|
-
transition_out(c.$$.fragment, local)
|
|
42
|
-
current = false
|
|
43
|
-
},
|
|
44
|
-
d(detaching: any) {
|
|
45
|
-
destroy_component(c, detaching)
|
|
46
|
-
},
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function renderClient<T>(
|
|
51
|
-
Comp: T,
|
|
52
|
-
props: T extends ComponentType<infer C> ? ComponentProps<C> : any
|
|
53
|
-
) {
|
|
54
|
-
return class WrapperComp extends SvelteComponent {
|
|
55
|
-
constructor(options: any) {
|
|
56
|
-
super()
|
|
57
|
-
init(
|
|
58
|
-
this,
|
|
59
|
-
options,
|
|
60
|
-
null,
|
|
61
|
-
(ctx: any) => create_fragment(ctx, Comp, props),
|
|
62
|
-
safe_not_equal,
|
|
63
|
-
{},
|
|
64
|
-
undefined
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
} as ComponentType
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function renderServer<T>(
|
|
71
|
-
Comp: T,
|
|
72
|
-
props: T extends ComponentType<infer C> ? ComponentProps<C> : any
|
|
73
|
-
) {
|
|
74
|
-
const WrapperComp = create_ssr_component(
|
|
75
|
-
($$result: any, $$props: any, $$bindings: any, slots: any) => {
|
|
76
|
-
return `${validate_component(Comp, 'TableComponent').$$render(
|
|
77
|
-
$$result,
|
|
78
|
-
props,
|
|
79
|
-
{},
|
|
80
|
-
{}
|
|
81
|
-
)}`
|
|
82
|
-
}
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
return WrapperComp as unknown as ComponentType
|
|
3
|
+
/**
|
|
4
|
+
* A helper class to make it easy to identify Svelte components in `columnDef.cell` and `columnDef.header` properties.
|
|
5
|
+
* @example
|
|
6
|
+
* ```svelte
|
|
7
|
+
* {#if cell.column.columnDef.cell(cell.getContext()) instanceof RenderComponentConfig}
|
|
8
|
+
* <svelte:component this={columnDef.cell.component} {...columnDef.cell.props} />
|
|
9
|
+
* {/if}
|
|
10
|
+
* ```
|
|
11
|
+
* */
|
|
12
|
+
export class RenderComponentConfig<TComponent extends SvelteComponent> {
|
|
13
|
+
constructor(
|
|
14
|
+
public component: ComponentType<TComponent>,
|
|
15
|
+
public props: ComponentProps<TComponent> | Record<string, never> = {}
|
|
16
|
+
) {}
|
|
86
17
|
}
|
|
87
18
|
|
|
88
|
-
|
|
89
|
-
|
|
19
|
+
/**
|
|
20
|
+
* A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
|
|
21
|
+
* @param component A Svelte component
|
|
22
|
+
* @param props The props to pass to `component`
|
|
23
|
+
* @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* // +page.svelte
|
|
27
|
+
* const defaultColumns = [
|
|
28
|
+
* columnHelper.accessor('name', {
|
|
29
|
+
* header: header => renderComponent(SortHeader, { label: 'Name', header }),
|
|
30
|
+
* }),
|
|
31
|
+
* columnHelper.accessor('state', {
|
|
32
|
+
* header: header => renderComponent(SortHeader, { label: 'State', header }),
|
|
33
|
+
* }),
|
|
34
|
+
* ]
|
|
35
|
+
* ```
|
|
36
|
+
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
37
|
+
*/
|
|
38
|
+
export const renderComponent = <TComponent extends SvelteComponent>(
|
|
39
|
+
component: ComponentType<TComponent>,
|
|
40
|
+
props: ComponentProps<TComponent>
|
|
41
|
+
) => new RenderComponentConfig(component, props)
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
}
|
package/dist/placeholder.d.ts
DELETED
package/dist/placeholder.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { create_ssr_component, escape } from 'svelte/internal';
|
|
2
|
-
import PlaceholderClient from './placeholder.svelte';
|
|
3
|
-
const PlaceholderServer = create_ssr_component(($$result, $$props, $$bindings, slots) => {
|
|
4
|
-
return `${escape($$props.content)}`;
|
|
5
|
-
});
|
|
6
|
-
export default typeof document === 'undefined'
|
|
7
|
-
? PlaceholderServer
|
|
8
|
-
: PlaceholderClient;
|
package/dist/placeholder.svelte
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/** @typedef {typeof __propDef.props} PlaceholderProps */
|
|
2
|
-
/** @typedef {typeof __propDef.events} PlaceholderEvents */
|
|
3
|
-
/** @typedef {typeof __propDef.slots} PlaceholderSlots */
|
|
4
|
-
export default class Placeholder extends SvelteComponentTyped<{
|
|
5
|
-
content: any;
|
|
6
|
-
}, {
|
|
7
|
-
[evt: string]: CustomEvent<any>;
|
|
8
|
-
}, {}> {
|
|
9
|
-
}
|
|
10
|
-
export type PlaceholderProps = typeof __propDef.props;
|
|
11
|
-
export type PlaceholderEvents = typeof __propDef.events;
|
|
12
|
-
export type PlaceholderSlots = typeof __propDef.slots;
|
|
13
|
-
import { SvelteComponentTyped } from "svelte";
|
|
14
|
-
declare const __propDef: {
|
|
15
|
-
props: {
|
|
16
|
-
content: any;
|
|
17
|
-
};
|
|
18
|
-
events: {
|
|
19
|
-
[evt: string]: CustomEvent<any>;
|
|
20
|
-
};
|
|
21
|
-
slots: {};
|
|
22
|
-
};
|
|
23
|
-
export {};
|
package/src/placeholder.svelte
DELETED
package/src/placeholder.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { SvelteComponentDev } from 'svelte/internal'
|
|
2
|
-
import { create_ssr_component, escape } from 'svelte/internal'
|
|
3
|
-
import PlaceholderClient from './placeholder.svelte'
|
|
4
|
-
|
|
5
|
-
type X = typeof PlaceholderClient
|
|
6
|
-
|
|
7
|
-
const PlaceholderServer = create_ssr_component(
|
|
8
|
-
($$result: any, $$props: any, $$bindings: any, slots: any) => {
|
|
9
|
-
return `${escape($$props.content)}`
|
|
10
|
-
}
|
|
11
|
-
) as any as typeof SvelteComponentDev
|
|
12
|
-
|
|
13
|
-
export default typeof document === 'undefined'
|
|
14
|
-
? PlaceholderServer
|
|
15
|
-
: PlaceholderClient
|