-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat: svelte 5 adapter #5403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: svelte 5 adapter #5403
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
c7a92a3
updated svelte-table adapter to Svelte 5
a9acc09
updated rollup-plugin-svelte
0d42c16
updated prettier plugin for svelte
25a8057
updated the table implementation and applied formatting
63db98d
updated flex-render documentation
9428a3d
Update packages/svelte-table/src/flex-render.svelte
walker-tx 517a862
re-add tweak rollup config and uglify TContext in flex-render
a44aa1a
Merge branch 'feat-svelte-5-adapter' of github.com:wlockiv/tanstack-t…
28b3441
updated svelte-basic example
c7ba006
revert svelte-table version
e4a6ae0
correction - using wrong content object for footer
4e3338d
removing unused imports from svelte's basic example
d9a2262
updated tanstack-table-example-svelte-column-groups to svelte 5
7ca9368
updated svelte-table column order example
19e3d8c
column-pinning example
58630ca
svelte column visibility example
8381954
svelte filtering example
5cdd953
simplified column-visibility svelte example a tiny bit
af910ae
svelte sorting example
9b2cbe3
remove comment from svelte sorting example
027d1f1
fix svelte flex-render ts types and docs
b0d6713
implement svelte-package
7167446
updated svelte package.json and corrected type error in flex render
80fc5de
prettier
be8752e
removed rollup-plugin-svelte dependencies
edcc180
inline the flex render component
77c41e6
Merge branch 'main' into feat-svelte-5-adapter
KevinVandy 914665e
fixed test errors
a0664fb
Merge branch 'main' of github.com:wlockiv/tanstack-table into feat-sv…
8b3b064
removed duplicate devdependencies in the svelte package
4d2ccbc
Merge branch 'feat-svelte-5-adapter' of github.com:wlockiv/tanstack-t…
9aaccbb
Merge branch 'feat-svelte-5-adapter' of github.com:wlockiv/tanstack-t…
750c3ed
Merge branch 'alpha' into feat-svelte-5-adapter
lachlancollins e0d3b32
Merge branch 'alpha' into feat-svelte-5-adapter
KevinVandy 8198cd5
upgrade svelte versions in examples
KevinVandy 91f6f83
disable knip because its annoying
KevinVandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<!-- | ||
@component | ||
A Svelte component that renders a cell or header, according to what was specified in the column definition. | ||
|
||
|
||
```svelte | ||
<script> | ||
import { FlexRender, createSvelteTable, renderComponent } from '@tanstack/svelte-table'; | ||
import ColorCell from './ColorCell.svelte'; | ||
|
||
const columns = [ | ||
{ | ||
// The header will be `name`, and the cell will be the accessed value. | ||
accessor: 'name', | ||
}, | ||
{ | ||
// The header will be `Age`, and the cell will be the accessed value plus the string ` years old`. | ||
accessor: 'age', | ||
header: 'Age', | ||
cell(props) => props.getValue() + ' years old' | ||
}, | ||
{ | ||
// The header will be `Favorite Color`, and the cell will be a dynamically rendered Svelte component. | ||
accessor: 'favoriteColor', | ||
header: 'Favorite Color', | ||
cell: (props) => renderComponent(ColorCell, { color: props.getValue() }) | ||
} | ||
]; | ||
|
||
const table = createSvelteTable({ columns, ...restOptions }) | ||
</script> | ||
|
||
<table> | ||
<thead> | ||
{#each table.getHeaderGroups() as headerGroup} | ||
<tr> | ||
{#each headerGroup.headers as header} | ||
<th colspan={header.colSpan}> | ||
<FlexRender content={header.column.columnDef.header} context={header.getContext()} /> | ||
</th> | ||
{/each} | ||
</tr> | ||
{/each} | ||
</thead> | ||
<tbody> | ||
{#each table.getRowModel().rows as row} | ||
<tr> | ||
{#each row.getVisibleCells() as cell} | ||
<td> | ||
<FlexRender content={cell.column.columnDef.cell} context={cell.getContext()} /> | ||
</td> | ||
{/each} | ||
</tr> | ||
{/each} | ||
</tbody> | ||
</table> | ||
``` | ||
--> | ||
|
||
<script | ||
lang="ts" | ||
generics="TData, TValue, TContext extends object = HeaderOrCellContext<TData, TValue>" | ||
> | ||
import type { ColumnDefTemplate } from '@tanstack/table-core' | ||
import { RenderComponentConfig } from './render-component' | ||
import type { HeaderOrCellContext } from './types' | ||
|
||
type Props = { | ||
/** The cell or header field of the current cell's column definition. */ | ||
content: ColumnDefTemplate<TContext> | undefined | ||
/** The result of the `getContext()` function of the header or cell */ | ||
context: TContext | ||
} | ||
|
||
let { content, context } = $props<Props>() | ||
</script> | ||
|
||
{#snippet componentCell()} | ||
{#if typeof content === 'string'} | ||
{content} | ||
{:else if content instanceof Function} | ||
{@const result = content(context)} | ||
{#if result instanceof RenderComponentConfig} | ||
<svelte:component this={result.component} {...result.props} /> | ||
{:else} | ||
{result} | ||
{/if} | ||
{/if} | ||
{/snippet} | ||
|
||
{@render componentCell()} | ||
walker-tx marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,4 @@ | ||
import { | ||
RowData, | ||
createTable, | ||
TableOptions, | ||
TableOptionsResolved, | ||
} from '@tanstack/table-core' | ||
import Placeholder from './placeholder' | ||
import type { ComponentType } from 'svelte' | ||
import { SvelteComponent } from 'svelte/internal' | ||
import { readable, writable, derived, Readable, get } from 'svelte/store' | ||
import { renderComponent } from './render-component' | ||
|
||
export { renderComponent } from './render-component' | ||
|
||
export * from '@tanstack/table-core' | ||
|
||
function isSvelteServerComponent(component: any) { | ||
return ( | ||
typeof component === 'object' && | ||
typeof component.$$render === 'function' && | ||
typeof component.render === 'function' | ||
) | ||
} | ||
|
||
function isSvelteClientComponent(component: any) { | ||
let isHMR = '__SVELTE_HMR' in window | ||
|
||
return ( | ||
component.prototype instanceof SvelteComponent || | ||
(isHMR && | ||
component.name?.startsWith('Proxy<') && | ||
component.name?.endsWith('>')) | ||
) | ||
} | ||
|
||
function isSvelteComponent(component: any) { | ||
if (typeof document === 'undefined') { | ||
return isSvelteServerComponent(component) | ||
} else { | ||
return isSvelteClientComponent(component) | ||
} | ||
} | ||
|
||
function wrapInPlaceholder(content: any) { | ||
return renderComponent(Placeholder, { content }) | ||
} | ||
|
||
export function flexRender(component: any, props: any): ComponentType | null { | ||
if (!component) return null | ||
|
||
if (isSvelteComponent(component)) { | ||
return renderComponent(component, props) | ||
} | ||
|
||
if (typeof component === 'function') { | ||
const result = component(props) | ||
if (result === null || result === undefined) return null | ||
|
||
if (isSvelteComponent(result)) { | ||
return renderComponent(result, props) | ||
} | ||
|
||
return wrapInPlaceholder(result) | ||
} | ||
|
||
return wrapInPlaceholder(component) | ||
} | ||
|
||
type ReadableOrVal<T> = T | Readable<T> | ||
|
||
export function createSvelteTable<TData extends RowData>( | ||
options: ReadableOrVal<TableOptions<TData>> | ||
) { | ||
let optionsStore: Readable<TableOptions<TData>> | ||
|
||
if ('subscribe' in options) { | ||
optionsStore = options | ||
} else { | ||
optionsStore = readable(options) | ||
} | ||
|
||
let resolvedOptions: TableOptionsResolved<TData> = { | ||
state: {}, // Dummy state | ||
onStateChange: () => {}, // noop | ||
renderFallbackValue: null, | ||
...get(optionsStore), | ||
} | ||
|
||
let table = createTable(resolvedOptions) | ||
|
||
let stateStore = writable(/** @type {number} */ table.initialState) | ||
// combine stores | ||
let stateOptionsStore = derived([stateStore, optionsStore], s => s) | ||
const tableReadable = readable(table, function start(set) { | ||
const unsubscribe = stateOptionsStore.subscribe(([state, options]) => { | ||
table.setOptions(prev => { | ||
return { | ||
...prev, | ||
...options, | ||
state: { ...state, ...options.state }, | ||
// Similarly, we'll maintain both our internal state and any user-provided | ||
// state. | ||
onStateChange: updater => { | ||
if (updater instanceof Function) { | ||
stateStore.update(updater) | ||
} else { | ||
stateStore.set(updater) | ||
} | ||
|
||
resolvedOptions.onStateChange?.(updater) | ||
}, | ||
} | ||
}) | ||
|
||
// it didn't seem to rerender without setting the table | ||
set(table) | ||
}) | ||
|
||
return function stop() { | ||
unsubscribe() | ||
} | ||
}) | ||
|
||
return tableReadable | ||
} | ||
export { default as FlexRender } from './flex-render.svelte' | ||
export { renderComponent } from './render-component' | ||
export { createSvelteTable } from './table.svelte' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.