@tanstack/svelte-table 9.0.0-alpha.4 → 9.0.0-alpha.42
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/context-keys.d.ts +3 -0
- package/dist/context-keys.js +3 -0
- package/dist/createTable.svelte.d.ts +13 -0
- package/dist/createTable.svelte.js +54 -0
- package/dist/createTableHook.svelte.d.ts +235 -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/flex-render.d.ts +1 -0
- package/dist/flex-render.js +2 -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/reactivity.svelte.d.ts +2 -0
- package/dist/reactivity.svelte.js +45 -0
- package/dist/render-component.d.ts +64 -9
- package/dist/render-component.js +60 -4
- package/dist/static-functions.d.ts +1 -0
- package/dist/static-functions.js +1 -0
- package/dist/subscribe.d.ts +9 -0
- package/dist/subscribe.js +9 -0
- package/package.json +27 -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/context-keys.ts +3 -0
- package/src/createTable.svelte.ts +103 -0
- package/src/createTableHook.svelte.ts +636 -0
- package/src/createTableState.svelte.ts +15 -0
- package/src/flex-render.ts +3 -0
- package/src/index.ts +20 -3
- package/src/merge-objects.ts +43 -0
- package/src/reactivity.svelte.ts +64 -0
- package/src/render-component.ts +74 -10
- package/src/static-functions.ts +1 -0
- package/src/subscribe.ts +23 -0
- package/dist/flex-render.svelte +0 -35
- package/dist/flex-render.svelte.d.ts +0 -28
- package/dist/table.svelte.d.ts +0 -28
- package/dist/table.svelte.js +0 -87
- package/src/flex-render.svelte +0 -35
- package/src/table.svelte.ts +0 -117
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
import { createColumnHelper as coreCreateColumnHelper } from '@tanstack/table-core';
|
|
3
|
+
import { createTable } from './createTable.svelte';
|
|
4
|
+
import { mergeObjects } from './merge-objects';
|
|
5
|
+
import { cellContextKey, headerContextKey, tableContextKey, } from './context-keys.js';
|
|
6
|
+
import AppTableSvelte from './AppTable.svelte';
|
|
7
|
+
import AppCellSvelte from './AppCell.svelte';
|
|
8
|
+
import AppHeaderSvelte from './AppHeader.svelte';
|
|
9
|
+
import FlexRenderSvelte from './FlexRender.svelte';
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// createTableHook Factory
|
|
12
|
+
// =============================================================================
|
|
13
|
+
/**
|
|
14
|
+
* Creates a custom table hook with pre-bound components for composition.
|
|
15
|
+
*
|
|
16
|
+
* This is the table equivalent of TanStack Form's `createFormHook`. It allows you to:
|
|
17
|
+
* - Define features, row models, and default options once, shared across all tables
|
|
18
|
+
* - Register reusable table, cell, and header components
|
|
19
|
+
* - Access table/cell/header instances via context in those components
|
|
20
|
+
* - Get a `createAppTable` hook that returns an extended table with App wrapper components
|
|
21
|
+
* - Get a `createAppColumnHelper` function pre-bound to your features
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // hooks/table.ts
|
|
26
|
+
* export const {
|
|
27
|
+
* createAppTable,
|
|
28
|
+
* createAppColumnHelper,
|
|
29
|
+
* useTableContext,
|
|
30
|
+
* useCellContext,
|
|
31
|
+
* useHeaderContext,
|
|
32
|
+
* } = createTableHook({
|
|
33
|
+
* _features: tableFeatures({
|
|
34
|
+
* rowPaginationFeature,
|
|
35
|
+
* rowSortingFeature,
|
|
36
|
+
* columnFilteringFeature,
|
|
37
|
+
* }),
|
|
38
|
+
* _rowModels: {
|
|
39
|
+
* paginatedRowModel: createPaginatedRowModel(),
|
|
40
|
+
* sortedRowModel: createSortedRowModel(sortFns),
|
|
41
|
+
* filteredRowModel: createFilteredRowModel(filterFns),
|
|
42
|
+
* },
|
|
43
|
+
* tableComponents: { PaginationControls, RowCount },
|
|
44
|
+
* cellComponents: { TextCell, NumberCell },
|
|
45
|
+
* headerComponents: { SortIndicator, ColumnFilter },
|
|
46
|
+
* })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function createTableHook({ tableComponents, cellComponents, headerComponents, ...defaultTableOptions }) {
|
|
50
|
+
/**
|
|
51
|
+
* Create a column helper pre-bound to the features and components configured in this table hook.
|
|
52
|
+
* The cell, header, and footer contexts include pre-bound components (e.g., `cell.TextCell`).
|
|
53
|
+
*/
|
|
54
|
+
function createAppColumnHelper() {
|
|
55
|
+
return coreCreateColumnHelper();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Access the table instance from within an `AppTable` wrapper.
|
|
59
|
+
* Use this in custom `tableComponents` passed to `createTableHook`.
|
|
60
|
+
* TFeatures is already known from the createTableHook call.
|
|
61
|
+
*/
|
|
62
|
+
function useTableContext() {
|
|
63
|
+
const table = getContext(tableContextKey);
|
|
64
|
+
if (!table) {
|
|
65
|
+
throw new Error('`useTableContext` must be used within an `AppTable` component. ' +
|
|
66
|
+
'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.');
|
|
67
|
+
}
|
|
68
|
+
return table;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Access the cell instance from within an `AppCell` wrapper.
|
|
72
|
+
* Use this in custom `cellComponents` passed to `createTableHook`.
|
|
73
|
+
* TFeatures is already known from the createTableHook call.
|
|
74
|
+
*/
|
|
75
|
+
function useCellContext() {
|
|
76
|
+
const cell = getContext(cellContextKey);
|
|
77
|
+
if (!cell) {
|
|
78
|
+
throw new Error('`useCellContext` must be used within an `AppCell` component. ' +
|
|
79
|
+
'Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.');
|
|
80
|
+
}
|
|
81
|
+
return cell;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Access the header instance from within an `AppHeader` or `AppFooter` wrapper.
|
|
85
|
+
* Use this in custom `headerComponents` passed to `createTableHook`.
|
|
86
|
+
* TFeatures is already known from the createTableHook call.
|
|
87
|
+
*/
|
|
88
|
+
function useHeaderContext() {
|
|
89
|
+
const header = getContext(headerContextKey);
|
|
90
|
+
if (!header) {
|
|
91
|
+
throw new Error('`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.');
|
|
92
|
+
}
|
|
93
|
+
return header;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Enhanced createTable hook that returns a table with App wrapper components
|
|
97
|
+
* and pre-bound tableComponents attached directly to the table object.
|
|
98
|
+
*
|
|
99
|
+
* Default options from createTableHook are automatically merged with
|
|
100
|
+
* the options passed here. Options passed here take precedence.
|
|
101
|
+
*
|
|
102
|
+
* TFeatures is already known from the createTableHook call; TData is inferred from the data prop.
|
|
103
|
+
*/
|
|
104
|
+
function createAppTable(tableOptions, selector) {
|
|
105
|
+
// Merge default options with provided options (provided takes precedence)
|
|
106
|
+
const mergedTableOptions = mergeObjects(defaultTableOptions, tableOptions);
|
|
107
|
+
const table = createTable(mergedTableOptions, selector);
|
|
108
|
+
// Build cellComponents with FlexRender included
|
|
109
|
+
const cellComponentsWithFlexRender = {
|
|
110
|
+
FlexRender: FlexRenderSvelte,
|
|
111
|
+
...(cellComponents ?? {}),
|
|
112
|
+
};
|
|
113
|
+
// Build headerComponents with FlexRender included
|
|
114
|
+
const headerComponentsWithFlexRender = {
|
|
115
|
+
FlexRender: FlexRenderSvelte,
|
|
116
|
+
...(headerComponents ?? {}),
|
|
117
|
+
};
|
|
118
|
+
// Create wrapper components using the svelte-form (internal, props) => pattern.
|
|
119
|
+
// setContext is called in the closure — this runs during component
|
|
120
|
+
// initialization, so Svelte's context API works correctly.
|
|
121
|
+
// With keyed {#each} blocks, components are recreated on reorder,
|
|
122
|
+
// so context is always fresh.
|
|
123
|
+
const AppTable = ((internal, props) => {
|
|
124
|
+
setContext(tableContextKey, table);
|
|
125
|
+
return AppTableSvelte(internal, { ...props });
|
|
126
|
+
});
|
|
127
|
+
const AppCell = ((internal, { children, cell, ...rest }) => {
|
|
128
|
+
setContext(cellContextKey, cell);
|
|
129
|
+
return AppCellSvelte(internal, {
|
|
130
|
+
cell,
|
|
131
|
+
cellComponents: cellComponentsWithFlexRender,
|
|
132
|
+
children,
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
const AppHeader = ((internal, { children, header, ...rest }) => {
|
|
136
|
+
setContext(headerContextKey, header);
|
|
137
|
+
return AppHeaderSvelte(internal, {
|
|
138
|
+
header,
|
|
139
|
+
headerComponents: headerComponentsWithFlexRender,
|
|
140
|
+
children,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
// AppFooter reuses AppHeaderSvelte (footers use Header type in table-core)
|
|
144
|
+
const AppFooter = ((internal, { children, header, ...rest }) => {
|
|
145
|
+
setContext(headerContextKey, header);
|
|
146
|
+
return AppHeaderSvelte(internal, {
|
|
147
|
+
header,
|
|
148
|
+
headerComponents: headerComponentsWithFlexRender,
|
|
149
|
+
children,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
// Combine everything into the extended table API
|
|
153
|
+
return Object.assign(table, {
|
|
154
|
+
AppTable,
|
|
155
|
+
AppCell,
|
|
156
|
+
AppHeader,
|
|
157
|
+
AppFooter,
|
|
158
|
+
FlexRender: FlexRenderSvelte,
|
|
159
|
+
...(tableComponents ?? {}),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
appFeatures: defaultTableOptions._features,
|
|
164
|
+
createAppColumnHelper,
|
|
165
|
+
createAppTable,
|
|
166
|
+
useTableContext,
|
|
167
|
+
useCellContext,
|
|
168
|
+
useHeaderContext,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as FlexRender } from './FlexRender.svelte';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export * from '@tanstack/table-core';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
2
|
+
export { createTable } from './createTable.svelte';
|
|
3
|
+
export type { SvelteTable } from './createTable.svelte';
|
|
4
|
+
export { createTableHook } from './createTableHook.svelte';
|
|
5
|
+
export type { AppCellContext, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderContext, AppSvelteTable, ComponentType, CreateTableHookOptions, } from './createTableHook.svelte';
|
|
6
|
+
export { createTableState } from './createTableState.svelte';
|
|
7
|
+
export { default as FlexRender } from './FlexRender.svelte';
|
|
8
|
+
export { subscribeTable } from './subscribe';
|
|
9
|
+
export { renderComponent, renderSnippet } from './render-component';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export * from '@tanstack/table-core';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
2
|
+
export { createTable } from './createTable.svelte';
|
|
3
|
+
export { createTableHook } from './createTableHook.svelte';
|
|
4
|
+
export { createTableState } from './createTableState.svelte';
|
|
5
|
+
export { default as FlexRender } from './FlexRender.svelte';
|
|
6
|
+
export { subscribeTable } from './subscribe';
|
|
7
|
+
export { renderComponent, renderSnippet } from './render-component';
|
|
@@ -0,0 +1,8 @@
|
|
|
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 declare function mergeObjects<T>(source: T): T;
|
|
6
|
+
export declare function mergeObjects<T, U>(source: T, source1: U): T & U;
|
|
7
|
+
export declare function mergeObjects<T, U, V>(source: T, source1: U, source2: V): T & U & V;
|
|
8
|
+
export declare function mergeObjects<T, U, V, W>(source: T, source1: U, source2: V, source3: W): T & U & V & W;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function mergeObjects(...sources) {
|
|
2
|
+
const target = {};
|
|
3
|
+
for (let source of sources) {
|
|
4
|
+
if (typeof source === 'function')
|
|
5
|
+
source = source();
|
|
6
|
+
if (source) {
|
|
7
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
8
|
+
for (const key in descriptors) {
|
|
9
|
+
if (key in target)
|
|
10
|
+
continue;
|
|
11
|
+
Object.defineProperty(target, key, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get() {
|
|
14
|
+
for (let i = sources.length - 1; i >= 0; i--) {
|
|
15
|
+
let v, s = sources[i];
|
|
16
|
+
if (typeof s === 'function')
|
|
17
|
+
s = s();
|
|
18
|
+
// eslint-disable-next-line prefer-const
|
|
19
|
+
v = (s || {})[key];
|
|
20
|
+
if (v !== undefined)
|
|
21
|
+
return v;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return target;
|
|
29
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { flushSync, untrack } from 'svelte';
|
|
2
|
+
function observerToCallback(observerOrNext) {
|
|
3
|
+
return typeof observerOrNext === 'function'
|
|
4
|
+
? observerOrNext
|
|
5
|
+
: (value) => observerOrNext.next?.(value);
|
|
6
|
+
}
|
|
7
|
+
function subscribeToRune(getValue, observerOrNext) {
|
|
8
|
+
const callback = observerToCallback(observerOrNext);
|
|
9
|
+
const unsubscribe = $effect.root(() => {
|
|
10
|
+
$effect(() => {
|
|
11
|
+
callback(getValue());
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
return { unsubscribe };
|
|
15
|
+
}
|
|
16
|
+
export function svelteReactivity() {
|
|
17
|
+
return {
|
|
18
|
+
createReadonlyAtom: (fn, _options) => {
|
|
19
|
+
const value = $derived.by(fn);
|
|
20
|
+
return {
|
|
21
|
+
get: () => value,
|
|
22
|
+
subscribe: ((observerOrNext) => {
|
|
23
|
+
return subscribeToRune(() => value, observerOrNext);
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
createWritableAtom: (initialValue, _options) => {
|
|
28
|
+
let value = $state(initialValue);
|
|
29
|
+
return {
|
|
30
|
+
set: (updater) => {
|
|
31
|
+
value =
|
|
32
|
+
typeof updater === 'function'
|
|
33
|
+
? updater(value)
|
|
34
|
+
: updater;
|
|
35
|
+
},
|
|
36
|
+
get: () => value,
|
|
37
|
+
subscribe: ((observerOrNext) => {
|
|
38
|
+
return subscribeToRune(() => value, observerOrNext);
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
untrack: untrack,
|
|
43
|
+
batch: (fn) => flushSync(fn),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -1,20 +1,50 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Component, ComponentProps, Snippet } from 'svelte';
|
|
2
2
|
/**
|
|
3
|
-
* A helper class to make it easy to identify Svelte components in
|
|
3
|
+
* A helper class to make it easy to identify Svelte components in
|
|
4
|
+
* `columnDef.cell` and `columnDef.header` properties.
|
|
5
|
+
*
|
|
6
|
+
* > NOTE: This class should only be used internally by the adapter. If you're
|
|
7
|
+
* reading this and you don't know what this is for, you probably don't need it.
|
|
8
|
+
*
|
|
4
9
|
* @example
|
|
5
10
|
* ```svelte
|
|
6
|
-
* {
|
|
7
|
-
*
|
|
11
|
+
* {@const result = content(context as any)}
|
|
12
|
+
* {#if result instanceof RenderComponentConfig}
|
|
13
|
+
* {@const { component: Component, props } = result}
|
|
14
|
+
* <Component {...props} />
|
|
8
15
|
* {/if}
|
|
9
16
|
* ```
|
|
10
17
|
* */
|
|
11
|
-
export declare class RenderComponentConfig<TComponent extends
|
|
12
|
-
component:
|
|
13
|
-
props
|
|
14
|
-
constructor(component:
|
|
18
|
+
export declare class RenderComponentConfig<TComponent extends Component> {
|
|
19
|
+
component: TComponent;
|
|
20
|
+
props?: (ComponentProps<TComponent> | Record<string, never>) | undefined;
|
|
21
|
+
constructor(component: TComponent, props?: (ComponentProps<TComponent> | Record<string, never>) | undefined);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A helper class to make it easy to identify Svelte Snippets in `columnDef.cell` and `columnDef.header` properties.
|
|
25
|
+
*
|
|
26
|
+
* > NOTE: This class should only be used internally by the adapter. If you're
|
|
27
|
+
* reading this and you don't know what this is for, you probably don't need it.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```svelte
|
|
31
|
+
* {@const result = content(context as any)}
|
|
32
|
+
* {#if result instanceof RenderSnippetConfig}
|
|
33
|
+
* {@const { snippet, params } = result}
|
|
34
|
+
* {@render snippet(params)}
|
|
35
|
+
* {/if}
|
|
36
|
+
* ```
|
|
37
|
+
* */
|
|
38
|
+
export declare class RenderSnippetConfig<TProps> {
|
|
39
|
+
snippet: Snippet<[TProps]>;
|
|
40
|
+
params?: TProps | undefined;
|
|
41
|
+
constructor(snippet: Snippet<[TProps]>, params?: TProps | undefined);
|
|
15
42
|
}
|
|
16
43
|
/**
|
|
17
44
|
* A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
|
|
45
|
+
*
|
|
46
|
+
* This is only to be used with Svelte Components - use `renderSnippet` for Svelte Snippets.
|
|
47
|
+
*
|
|
18
48
|
* @param component A Svelte component
|
|
19
49
|
* @param props The props to pass to `component`
|
|
20
50
|
* @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
|
|
@@ -32,4 +62,29 @@ export declare class RenderComponentConfig<TComponent extends SvelteComponent> {
|
|
|
32
62
|
* ```
|
|
33
63
|
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
34
64
|
*/
|
|
35
|
-
export declare const renderComponent: <TComponent extends
|
|
65
|
+
export declare const renderComponent: <TComponent extends Component<any>, TProps extends ComponentProps<TComponent>>(component: TComponent, props?: TProps) => RenderComponentConfig<TComponent>;
|
|
66
|
+
/**
|
|
67
|
+
* A helper function to help create cells from Svelte Snippets through ColumnDef's `cell` and `header` properties.
|
|
68
|
+
*
|
|
69
|
+
* *The snippet must only take one parameter.*
|
|
70
|
+
*
|
|
71
|
+
* This is only to be used with Snippets - use `renderComponent` for Svelte Components.
|
|
72
|
+
*
|
|
73
|
+
* @param snippet
|
|
74
|
+
* @param params
|
|
75
|
+
* @returns
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* // +page.svelte
|
|
79
|
+
* const defaultColumns = [
|
|
80
|
+
* columnHelper.accessor('name', {
|
|
81
|
+
* cell: cell => renderSnippet(nameSnippet, { name: cell.row.name }),
|
|
82
|
+
* }),
|
|
83
|
+
* columnHelper.accessor('state', {
|
|
84
|
+
* cell: cell => renderSnippet(stateSnippet, { state: cell.row.state }),
|
|
85
|
+
* }),
|
|
86
|
+
* ]
|
|
87
|
+
* ```
|
|
88
|
+
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
89
|
+
*/
|
|
90
|
+
export declare const renderSnippet: <TProps>(snippet: Snippet<[TProps]>, params?: TProps) => RenderSnippetConfig<TProps>;
|
package/dist/render-component.js
CHANGED
|
@@ -1,20 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A helper class to make it easy to identify Svelte components in
|
|
2
|
+
* A helper class to make it easy to identify Svelte components in
|
|
3
|
+
* `columnDef.cell` and `columnDef.header` properties.
|
|
4
|
+
*
|
|
5
|
+
* > NOTE: This class should only be used internally by the adapter. If you're
|
|
6
|
+
* reading this and you don't know what this is for, you probably don't need it.
|
|
7
|
+
*
|
|
3
8
|
* @example
|
|
4
9
|
* ```svelte
|
|
5
|
-
* {
|
|
6
|
-
*
|
|
10
|
+
* {@const result = content(context as any)}
|
|
11
|
+
* {#if result instanceof RenderComponentConfig}
|
|
12
|
+
* {@const { component: Component, props } = result}
|
|
13
|
+
* <Component {...props} />
|
|
7
14
|
* {/if}
|
|
8
15
|
* ```
|
|
9
16
|
* */
|
|
10
17
|
export class RenderComponentConfig {
|
|
11
|
-
constructor(component, props
|
|
18
|
+
constructor(component, props) {
|
|
12
19
|
this.component = component;
|
|
13
20
|
this.props = props;
|
|
14
21
|
}
|
|
15
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* A helper class to make it easy to identify Svelte Snippets in `columnDef.cell` and `columnDef.header` properties.
|
|
25
|
+
*
|
|
26
|
+
* > NOTE: This class should only be used internally by the adapter. If you're
|
|
27
|
+
* reading this and you don't know what this is for, you probably don't need it.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```svelte
|
|
31
|
+
* {@const result = content(context as any)}
|
|
32
|
+
* {#if result instanceof RenderSnippetConfig}
|
|
33
|
+
* {@const { snippet, params } = result}
|
|
34
|
+
* {@render snippet(params)}
|
|
35
|
+
* {/if}
|
|
36
|
+
* ```
|
|
37
|
+
* */
|
|
38
|
+
export class RenderSnippetConfig {
|
|
39
|
+
constructor(snippet, params) {
|
|
40
|
+
this.snippet = snippet;
|
|
41
|
+
this.params = params;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
16
44
|
/**
|
|
17
45
|
* A helper function to help create cells from Svelte components through ColumnDef's `cell` and `header` properties.
|
|
46
|
+
*
|
|
47
|
+
* This is only to be used with Svelte Components - use `renderSnippet` for Svelte Snippets.
|
|
48
|
+
*
|
|
18
49
|
* @param component A Svelte component
|
|
19
50
|
* @param props The props to pass to `component`
|
|
20
51
|
* @returns A `RenderComponentConfig` object that helps svelte-table know how to render the header/cell component.
|
|
@@ -33,3 +64,28 @@ export class RenderComponentConfig {
|
|
|
33
64
|
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
34
65
|
*/
|
|
35
66
|
export const renderComponent = (component, props) => new RenderComponentConfig(component, props);
|
|
67
|
+
/**
|
|
68
|
+
* A helper function to help create cells from Svelte Snippets through ColumnDef's `cell` and `header` properties.
|
|
69
|
+
*
|
|
70
|
+
* *The snippet must only take one parameter.*
|
|
71
|
+
*
|
|
72
|
+
* This is only to be used with Snippets - use `renderComponent` for Svelte Components.
|
|
73
|
+
*
|
|
74
|
+
* @param snippet
|
|
75
|
+
* @param params
|
|
76
|
+
* @returns
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // +page.svelte
|
|
80
|
+
* const defaultColumns = [
|
|
81
|
+
* columnHelper.accessor('name', {
|
|
82
|
+
* cell: cell => renderSnippet(nameSnippet, { name: cell.row.name }),
|
|
83
|
+
* }),
|
|
84
|
+
* columnHelper.accessor('state', {
|
|
85
|
+
* cell: cell => renderSnippet(stateSnippet, { state: cell.row.state }),
|
|
86
|
+
* }),
|
|
87
|
+
* ]
|
|
88
|
+
* ```
|
|
89
|
+
* @see {@link https://tanstack.com/table/latest/docs/guide/column-defs}
|
|
90
|
+
*/
|
|
91
|
+
export const renderSnippet = (snippet, params) => new RenderSnippetConfig(snippet, params);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/table-core/static-functions';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/table-core/static-functions';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { RowData, Table, TableFeatures, TableState } from '@tanstack/table-core';
|
|
2
|
+
/**
|
|
3
|
+
* Fine-grained subscription to `table.store` using `useSelector` with shallow
|
|
4
|
+
* comparison. Call from `<script>` so the selector is contextually typed
|
|
5
|
+
* (same inference model as React’s `Subscribe` / `table.Subscribe`).
|
|
6
|
+
*/
|
|
7
|
+
export declare function subscribeTable<TFeatures extends TableFeatures, TData extends RowData, TSelected>(table: Table<TFeatures, TData>, selector: (state: TableState<TFeatures>) => TSelected): {
|
|
8
|
+
readonly current: TSelected;
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { shallow, useSelector } from '@tanstack/svelte-store';
|
|
2
|
+
/**
|
|
3
|
+
* Fine-grained subscription to `table.store` using `useSelector` with shallow
|
|
4
|
+
* comparison. Call from `<script>` so the selector is contextually typed
|
|
5
|
+
* (same inference model as React’s `Subscribe` / `table.Subscribe`).
|
|
6
|
+
*/
|
|
7
|
+
export function subscribeTable(table, selector) {
|
|
8
|
+
return useSelector(table.store, selector, { compare: shallow });
|
|
9
|
+
}
|
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.42",
|
|
4
4
|
"description": "Headless UI for building powerful tables & datagrids for Svelte.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,26 +30,44 @@
|
|
|
30
30
|
"svelte": "./dist/index.js",
|
|
31
31
|
"import": "./dist/index.js"
|
|
32
32
|
},
|
|
33
|
+
"./static-functions": {
|
|
34
|
+
"types": "./dist/static-functions.d.ts",
|
|
35
|
+
"svelte": "./dist/static-functions.js",
|
|
36
|
+
"import": "./dist/static-functions.js"
|
|
37
|
+
},
|
|
38
|
+
"./flex-render": {
|
|
39
|
+
"types": "./dist/flex-render.d.ts",
|
|
40
|
+
"svelte": "./dist/flex-render.js",
|
|
41
|
+
"import": "./dist/flex-render.js"
|
|
42
|
+
},
|
|
33
43
|
"./package.json": "./package.json"
|
|
34
44
|
},
|
|
35
45
|
"engines": {
|
|
36
|
-
"node": ">=
|
|
46
|
+
"node": ">=16"
|
|
37
47
|
},
|
|
38
48
|
"files": [
|
|
39
49
|
"dist",
|
|
40
50
|
"src"
|
|
41
51
|
],
|
|
42
52
|
"dependencies": {
|
|
43
|
-
"@tanstack/
|
|
53
|
+
"@tanstack/svelte-store": "^0.12.0",
|
|
54
|
+
"@tanstack/table-core": "9.0.0-alpha.42"
|
|
44
55
|
},
|
|
45
56
|
"devDependencies": {
|
|
46
|
-
"@sveltejs/package": "^2.
|
|
47
|
-
"@sveltejs/vite-plugin-svelte": "^
|
|
48
|
-
"svelte": "^
|
|
49
|
-
"svelte
|
|
57
|
+
"@sveltejs/package": "^2.5.7",
|
|
58
|
+
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
59
|
+
"eslint-plugin-svelte": "^3.17.1",
|
|
60
|
+
"svelte": "^5.55.5",
|
|
61
|
+
"svelte-check": "^4.4.7"
|
|
50
62
|
},
|
|
51
63
|
"peerDependencies": {
|
|
52
|
-
"svelte": "^5.0.0
|
|
64
|
+
"svelte": "^5.0.0"
|
|
53
65
|
},
|
|
54
|
-
"scripts": {
|
|
66
|
+
"scripts": {
|
|
67
|
+
"clean": "rimraf ./build && rimraf ./dist",
|
|
68
|
+
"test:eslint": "eslint ./src",
|
|
69
|
+
"test:types": "svelte-check --tsconfig ./tsconfig.json",
|
|
70
|
+
"test:build": "publint --strict",
|
|
71
|
+
"build": "svelte-package --input ./src --output ./dist"
|
|
72
|
+
}
|
|
55
73
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte'
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
cell: any
|
|
6
|
+
cellComponents: any
|
|
7
|
+
children: Snippet<[any]>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let { children, cell, cellComponents }: Props = $props()
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
{@render children?.(Object.assign(cell, cellComponents))}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte'
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
header: any
|
|
6
|
+
headerComponents: any
|
|
7
|
+
children: Snippet<[any]>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let { children, header, headerComponents }: Props = $props()
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
{@render children?.(Object.assign(header, headerComponents))}
|