@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,236 @@
|
|
|
1
|
+
import FlexRenderSvelte from './FlexRender.svelte';
|
|
2
|
+
import type { SvelteTable } from './createTable.svelte';
|
|
3
|
+
import type { Component, Snippet } from 'svelte';
|
|
4
|
+
import type { AccessorFn, AccessorFnColumnDef, AccessorKeyColumnDef, Cell, CellContext, CellData, Column, ColumnDef, DeepKeys, DeepValue, DisplayColumnDef, GroupColumnDef, Header, IdentifiedColumnDef, NoInfer, Row, RowData, Table, TableFeatures, TableOptions, TableState } from '@tanstack/table-core';
|
|
5
|
+
type ComponentType<T extends Record<string, any>> = Component<T>;
|
|
6
|
+
/**
|
|
7
|
+
* Enhanced CellContext with pre-bound cell components.
|
|
8
|
+
* The `cell` property includes the registered cellComponents.
|
|
9
|
+
*/
|
|
10
|
+
export type AppCellContext<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData, TCellComponents extends Record<string, ComponentType<any>>> = {
|
|
11
|
+
cell: Cell<TFeatures, TData, TValue> & TCellComponents & {
|
|
12
|
+
FlexRender: typeof FlexRenderSvelte;
|
|
13
|
+
};
|
|
14
|
+
column: Column<TFeatures, TData, TValue>;
|
|
15
|
+
getValue: CellContext<TFeatures, TData, TValue>['getValue'];
|
|
16
|
+
renderValue: CellContext<TFeatures, TData, TValue>['renderValue'];
|
|
17
|
+
row: Row<TFeatures, TData>;
|
|
18
|
+
table: Table<TFeatures, TData>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Enhanced HeaderContext with pre-bound header components.
|
|
22
|
+
* The `header` property includes the registered headerComponents.
|
|
23
|
+
*/
|
|
24
|
+
export type AppHeaderContext<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData, THeaderComponents extends Record<string, ComponentType<any>>> = {
|
|
25
|
+
column: Column<TFeatures, TData, TValue>;
|
|
26
|
+
header: Header<TFeatures, TData, TValue> & THeaderComponents & {
|
|
27
|
+
FlexRender: typeof FlexRenderSvelte;
|
|
28
|
+
};
|
|
29
|
+
table: Table<TFeatures, TData>;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Template type for column definitions that can be a string or a function.
|
|
33
|
+
*/
|
|
34
|
+
type AppColumnDefTemplate<TProps extends object> = string | ((props: TProps) => any);
|
|
35
|
+
/**
|
|
36
|
+
* Enhanced column definition base with pre-bound components in cell/header/footer contexts.
|
|
37
|
+
*/
|
|
38
|
+
type AppColumnDefBase<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> = Omit<IdentifiedColumnDef<TFeatures, TData, TValue>, 'cell' | 'header' | 'footer'> & {
|
|
39
|
+
cell?: AppColumnDefTemplate<AppCellContext<TFeatures, TData, TValue, TCellComponents>>;
|
|
40
|
+
header?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>>;
|
|
41
|
+
footer?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Enhanced display column definition with pre-bound components.
|
|
45
|
+
*/
|
|
46
|
+
type AppDisplayColumnDef<TFeatures extends TableFeatures, TData extends RowData, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> = Omit<DisplayColumnDef<TFeatures, TData, unknown>, 'cell' | 'header' | 'footer'> & {
|
|
47
|
+
cell?: AppColumnDefTemplate<AppCellContext<TFeatures, TData, unknown, TCellComponents>>;
|
|
48
|
+
header?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
|
|
49
|
+
footer?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Enhanced group column definition with pre-bound components.
|
|
53
|
+
*/
|
|
54
|
+
type AppGroupColumnDef<TFeatures extends TableFeatures, TData extends RowData, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> = Omit<GroupColumnDef<TFeatures, TData, unknown>, 'cell' | 'header' | 'footer' | 'columns'> & {
|
|
55
|
+
cell?: AppColumnDefTemplate<AppCellContext<TFeatures, TData, unknown, TCellComponents>>;
|
|
56
|
+
header?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
|
|
57
|
+
footer?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
|
|
58
|
+
columns?: Array<ColumnDef<TFeatures, TData, unknown>>;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Enhanced column helper with pre-bound components in cell/header/footer contexts.
|
|
62
|
+
* This enables TypeScript to know about the registered components when defining columns.
|
|
63
|
+
*/
|
|
64
|
+
export type AppColumnHelper<TFeatures extends TableFeatures, TData extends RowData, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> = {
|
|
65
|
+
/**
|
|
66
|
+
* Creates a data column definition with an accessor key or function.
|
|
67
|
+
* The cell, header, and footer contexts include pre-bound components.
|
|
68
|
+
*/
|
|
69
|
+
accessor: <TAccessor extends AccessorFn<TData> | DeepKeys<TData>, TValue extends TAccessor extends AccessorFn<TData, infer TReturn> ? TReturn : TAccessor extends DeepKeys<TData> ? DeepValue<TData, TAccessor> : never>(accessor: TAccessor, column: TAccessor extends AccessorFn<TData> ? AppColumnDefBase<TFeatures, TData, TValue, TCellComponents, THeaderComponents> & {
|
|
70
|
+
id: string;
|
|
71
|
+
} : AppColumnDefBase<TFeatures, TData, TValue, TCellComponents, THeaderComponents>) => TAccessor extends AccessorFn<TData> ? AccessorFnColumnDef<TFeatures, TData, TValue> : AccessorKeyColumnDef<TFeatures, TData, TValue>;
|
|
72
|
+
/**
|
|
73
|
+
* Wraps an array of column definitions to preserve each column's individual TValue type.
|
|
74
|
+
*/
|
|
75
|
+
columns: <TColumns extends ReadonlyArray<ColumnDef<TFeatures, TData, any>>>(columns: [...TColumns]) => Array<ColumnDef<TFeatures, TData, any>> & [...TColumns];
|
|
76
|
+
/**
|
|
77
|
+
* Creates a display column definition for non-data columns.
|
|
78
|
+
* The cell, header, and footer contexts include pre-bound components.
|
|
79
|
+
*/
|
|
80
|
+
display: (column: AppDisplayColumnDef<TFeatures, TData, TCellComponents, THeaderComponents>) => DisplayColumnDef<TFeatures, TData, unknown>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a group column definition with nested child columns.
|
|
83
|
+
* The cell, header, and footer contexts include pre-bound components.
|
|
84
|
+
*/
|
|
85
|
+
group: (column: AppGroupColumnDef<TFeatures, TData, TCellComponents, THeaderComponents>) => GroupColumnDef<TFeatures, TData, unknown>;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Options for creating a table hook with pre-bound components and default table options.
|
|
89
|
+
* Extends all TableOptions except 'columns' | 'data' | 'store' | 'state' | 'initialState'.
|
|
90
|
+
*/
|
|
91
|
+
export type CreateTableHookOptions<TFeatures extends TableFeatures, TTableComponents extends Record<string, ComponentType<any>>, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> = Omit<TableOptions<TFeatures, any>, 'columns' | 'data' | 'store' | 'state' | 'initialState'> & {
|
|
92
|
+
/**
|
|
93
|
+
* Table-level components that need access to the table instance.
|
|
94
|
+
* These are available directly on the table object returned by createAppTable.
|
|
95
|
+
* Use `useTableContext()` inside these components.
|
|
96
|
+
* @example { PaginationControls, GlobalFilter, RowCount }
|
|
97
|
+
*/
|
|
98
|
+
tableComponents?: TTableComponents;
|
|
99
|
+
/**
|
|
100
|
+
* Cell-level components that need access to the cell instance.
|
|
101
|
+
* These are available on the cell object passed to AppCell's children.
|
|
102
|
+
* Use `useCellContext()` inside these components.
|
|
103
|
+
* @example { TextCell, NumberCell, DateCell, CurrencyCell }
|
|
104
|
+
*/
|
|
105
|
+
cellComponents?: TCellComponents;
|
|
106
|
+
/**
|
|
107
|
+
* Header-level components that need access to the header instance.
|
|
108
|
+
* These are available on the header object passed to AppHeader/AppFooter's children.
|
|
109
|
+
* Use `useHeaderContext()` inside these components.
|
|
110
|
+
* @example { SortIndicator, ColumnFilter, ResizeHandle }
|
|
111
|
+
*/
|
|
112
|
+
headerComponents?: THeaderComponents;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Extended table API returned by createAppTable with all App wrapper components.
|
|
116
|
+
*/
|
|
117
|
+
export type AppSvelteTable<TFeatures extends TableFeatures, TData extends RowData, TSelected, TTableComponents extends Record<string, ComponentType<any>>, TCellComponents extends Record<string, ComponentType<any>>, THeaderComponents extends Record<string, ComponentType<any>>> = SvelteTable<TFeatures, TData, TSelected> & NoInfer<TTableComponents> & {
|
|
118
|
+
/**
|
|
119
|
+
* Root wrapper component that provides table context.
|
|
120
|
+
* @example
|
|
121
|
+
* ```svelte
|
|
122
|
+
* <table.AppTable>
|
|
123
|
+
* <table>...</table>
|
|
124
|
+
* </table.AppTable>
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
AppTable: Component<{
|
|
128
|
+
children: Snippet;
|
|
129
|
+
}>;
|
|
130
|
+
/**
|
|
131
|
+
* Wraps a cell and provides cell context with pre-bound cellComponents.
|
|
132
|
+
* @example
|
|
133
|
+
* ```svelte
|
|
134
|
+
* <table.AppCell cell={cell}>
|
|
135
|
+
* {#snippet children(c)}
|
|
136
|
+
* <td><c.TextCell /></td>
|
|
137
|
+
* {/snippet}
|
|
138
|
+
* </table.AppCell>
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
AppCell: Component<{
|
|
142
|
+
cell: Cell<TFeatures, TData, any>;
|
|
143
|
+
children: Snippet<[
|
|
144
|
+
Cell<TFeatures, TData, any> & NoInfer<TCellComponents> & {
|
|
145
|
+
FlexRender: typeof FlexRenderSvelte;
|
|
146
|
+
}
|
|
147
|
+
]>;
|
|
148
|
+
}>;
|
|
149
|
+
/**
|
|
150
|
+
* Wraps a header and provides header context with pre-bound headerComponents.
|
|
151
|
+
* @example
|
|
152
|
+
* ```svelte
|
|
153
|
+
* <table.AppHeader header={header}>
|
|
154
|
+
* {#snippet children(h)}
|
|
155
|
+
* <th><h.SortIndicator /></th>
|
|
156
|
+
* {/snippet}
|
|
157
|
+
* </table.AppHeader>
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
AppHeader: Component<{
|
|
161
|
+
header: Header<TFeatures, TData, any>;
|
|
162
|
+
children: Snippet<[
|
|
163
|
+
Header<TFeatures, TData, any> & NoInfer<THeaderComponents> & {
|
|
164
|
+
FlexRender: typeof FlexRenderSvelte;
|
|
165
|
+
}
|
|
166
|
+
]>;
|
|
167
|
+
}>;
|
|
168
|
+
/**
|
|
169
|
+
* Wraps a footer and provides header context with pre-bound headerComponents.
|
|
170
|
+
* @example
|
|
171
|
+
* ```svelte
|
|
172
|
+
* <table.AppFooter header={footer}>
|
|
173
|
+
* {#snippet children(f)}
|
|
174
|
+
* <td><f.FlexRender /></td>
|
|
175
|
+
* {/snippet}
|
|
176
|
+
* </table.AppFooter>
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
AppFooter: Component<{
|
|
180
|
+
header: Header<TFeatures, TData, any>;
|
|
181
|
+
children: Snippet<[
|
|
182
|
+
Header<TFeatures, TData, any> & NoInfer<THeaderComponents> & {
|
|
183
|
+
FlexRender: typeof FlexRenderSvelte;
|
|
184
|
+
}
|
|
185
|
+
]>;
|
|
186
|
+
}>;
|
|
187
|
+
/**
|
|
188
|
+
* Convenience FlexRender component attached to the table instance.
|
|
189
|
+
*/
|
|
190
|
+
FlexRender: typeof FlexRenderSvelte;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* Creates a custom table hook with pre-bound components for composition.
|
|
194
|
+
*
|
|
195
|
+
* This is the table equivalent of TanStack Form's `createFormHook`. It allows you to:
|
|
196
|
+
* - Define features, row models, and default options once, shared across all tables
|
|
197
|
+
* - Register reusable table, cell, and header components
|
|
198
|
+
* - Access table/cell/header instances via context in those components
|
|
199
|
+
* - Get a `createAppTable` hook that returns an extended table with App wrapper components
|
|
200
|
+
* - Get a `createAppColumnHelper` function pre-bound to your features
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* // hooks/table.ts
|
|
205
|
+
* export const {
|
|
206
|
+
* createAppTable,
|
|
207
|
+
* createAppColumnHelper,
|
|
208
|
+
* useTableContext,
|
|
209
|
+
* useCellContext,
|
|
210
|
+
* useHeaderContext,
|
|
211
|
+
* } = createTableHook({
|
|
212
|
+
* _features: tableFeatures({
|
|
213
|
+
* rowPaginationFeature,
|
|
214
|
+
* rowSortingFeature,
|
|
215
|
+
* columnFilteringFeature,
|
|
216
|
+
* }),
|
|
217
|
+
* _rowModels: {
|
|
218
|
+
* paginatedRowModel: createPaginatedRowModel(),
|
|
219
|
+
* sortedRowModel: createSortedRowModel(sortFns),
|
|
220
|
+
* filteredRowModel: createFilteredRowModel(filterFns),
|
|
221
|
+
* },
|
|
222
|
+
* tableComponents: { PaginationControls, RowCount },
|
|
223
|
+
* cellComponents: { TextCell, NumberCell },
|
|
224
|
+
* headerComponents: { SortIndicator, ColumnFilter },
|
|
225
|
+
* })
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
export declare function createTableHook<TFeatures extends TableFeatures, const TTableComponents extends Record<string, ComponentType<any>>, const TCellComponents extends Record<string, ComponentType<any>>, const THeaderComponents extends Record<string, ComponentType<any>>>({ tableComponents, cellComponents, headerComponents, ...defaultTableOptions }: CreateTableHookOptions<TFeatures, TTableComponents, TCellComponents, THeaderComponents>): {
|
|
229
|
+
appFeatures: TFeatures;
|
|
230
|
+
createAppColumnHelper: <TData extends RowData>() => AppColumnHelper<TFeatures, TData, TCellComponents, THeaderComponents>;
|
|
231
|
+
createAppTable: <TData extends RowData, TSelected = {}>(tableOptions: Omit<TableOptions<TFeatures, TData>, "_features" | "_rowModels">, selector?: (state: TableState<TFeatures>) => TSelected) => AppSvelteTable<TFeatures, TData, TSelected, TTableComponents, TCellComponents, THeaderComponents>;
|
|
232
|
+
useTableContext: <TData extends RowData = RowData>() => SvelteTable<TFeatures, TData>;
|
|
233
|
+
useCellContext: <TValue extends CellData = unknown>() => Cell<TFeatures, any, TValue>;
|
|
234
|
+
useHeaderContext: <TValue extends CellData = unknown>() => Header<TFeatures, any, TValue>;
|
|
235
|
+
};
|
|
236
|
+
export {};
|
|
@@ -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
|
+
}
|
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, AppColumnHelper, AppHeaderContext, AppSvelteTable, CreateTableHookOptions, } from './createTableHook.svelte';
|
|
6
|
+
export { createTableState } from './createTableState.svelte';
|
|
7
|
+
export { default as FlexRender } from './FlexRender.svelte';
|
|
8
|
+
export { default as Subscribe } from './Subscribe.svelte';
|
|
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 { default as Subscribe } from './Subscribe.svelte';
|
|
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
|
+
}
|
|
@@ -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);
|