Skip to content
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

feat: svelte 5 adapter #5403

Merged
merged 36 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c7a92a3
updated svelte-table adapter to Svelte 5
Mar 11, 2024
a9acc09
updated rollup-plugin-svelte
Mar 12, 2024
0d42c16
updated prettier plugin for svelte
Mar 12, 2024
25a8057
updated the table implementation and applied formatting
Mar 12, 2024
63db98d
updated flex-render documentation
Mar 12, 2024
9428a3d
Update packages/svelte-table/src/flex-render.svelte
walker-tx Mar 13, 2024
517a862
re-add tweak rollup config and uglify TContext in flex-render
Mar 13, 2024
a44aa1a
Merge branch 'feat-svelte-5-adapter' of github.com:wlockiv/tanstack-t…
Mar 13, 2024
28b3441
updated svelte-basic example
Mar 13, 2024
c7ba006
revert svelte-table version
Mar 13, 2024
e4a6ae0
correction - using wrong content object for footer
Mar 13, 2024
4e3338d
removing unused imports from svelte's basic example
Mar 13, 2024
d9a2262
updated tanstack-table-example-svelte-column-groups to svelte 5
Mar 13, 2024
7ca9368
updated svelte-table column order example
Mar 14, 2024
19e3d8c
column-pinning example
Mar 14, 2024
58630ca
svelte column visibility example
Mar 14, 2024
8381954
svelte filtering example
Mar 14, 2024
5cdd953
simplified column-visibility svelte example a tiny bit
Mar 14, 2024
af910ae
svelte sorting example
Mar 14, 2024
9b2cbe3
remove comment from svelte sorting example
Mar 14, 2024
027d1f1
fix svelte flex-render ts types and docs
Mar 14, 2024
b0d6713
implement svelte-package
Mar 14, 2024
7167446
updated svelte package.json and corrected type error in flex render
Mar 14, 2024
80fc5de
prettier
Mar 14, 2024
be8752e
removed rollup-plugin-svelte dependencies
Mar 14, 2024
edcc180
inline the flex render component
Mar 14, 2024
77c41e6
Merge branch 'main' into feat-svelte-5-adapter
KevinVandy Mar 21, 2024
914665e
fixed test errors
Mar 29, 2024
a0664fb
Merge branch 'main' of github.com:wlockiv/tanstack-table into feat-sv…
Mar 29, 2024
8b3b064
removed duplicate devdependencies in the svelte package
Mar 29, 2024
4d2ccbc
Merge branch 'feat-svelte-5-adapter' of github.com:wlockiv/tanstack-t…
Mar 29, 2024
9aaccbb
Merge branch 'feat-svelte-5-adapter' of github.com:wlockiv/tanstack-t…
Apr 29, 2024
750c3ed
Merge branch 'alpha' into feat-svelte-5-adapter
lachlancollins May 21, 2024
e0d3b32
Merge branch 'alpha' into feat-svelte-5-adapter
KevinVandy Jun 12, 2024
8198cd5
upgrade svelte versions in examples
KevinVandy Jun 12, 2024
91f6f83
disable knip because its annoying
KevinVandy Jun 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"rollup-plugin-visualizer": "^5.12.0",
"sherif": "^0.7.0",
"size-limit": "^11.0.2",
"svelte": "^4.2.8",
"svelte": "^5.0.0-next",
"typescript": "5.3.3",
"vitest": "^1.2.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-table/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"@tanstack/table-core": "workspace:*"
},
"peerDependencies": {
"svelte": "^4.0.0 || ^3.49.0"
"svelte": "^5.0.0-next"
}
}
8 changes: 1 addition & 7 deletions packages/svelte-table/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ export default defineConfig(
jsName: 'SvelteTable',
outputFile: 'index',
entryFile: 'src/index.ts',
external: [
'svelte',
'svelte/internal',
'svelte/store',
'@tanstack/table-core',
],
external: ['svelte', 'svelte/store', '@tanstack/table-core'],
globals: {
svelte: 'Svelte',
'svelte/internal': 'SvelteInternal',
'svelte/store': 'SvelteStore',
},
})
Expand Down
93 changes: 93 additions & 0 deletions packages/svelte-table/src/flex-render.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!--
@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
129 changes: 4 additions & 125 deletions packages/svelte-table/src/index.ts
100755 → 100644
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 * from '@tanstack/table-core';
export { default as FlexRender } from './flex-render.svelte';
export { renderComponent } from './render-component';
export { createSvelteTable } from './table.svelte';
5 changes: 0 additions & 5 deletions packages/svelte-table/src/placeholder.svelte

This file was deleted.

15 changes: 0 additions & 15 deletions packages/svelte-table/src/placeholder.ts

This file was deleted.

Loading