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