Skip to content
Merged
Changes from all commits
Commits
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
46 changes: 39 additions & 7 deletions packages/svelte-table/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,56 @@ import {
TableOptions,
} from '@tanstack/table-core'
import Placeholder from './placeholder.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'

export function render(Comp: any, props: any) {
if (!Comp) return null
function isSvelteServerComponent(component: any) {
return (
typeof component === 'object' &&
typeof component.$$render === 'function' &&
typeof component.render === 'function'
)
}

function isSvelteClientComponent(component: any) {
return component.prototype instanceof SvelteComponent
}

if (Comp instanceof Function) {
let res = Comp(props)
return res
function isSvelteComponent(component: any) {
if (typeof document === 'undefined') {
return isSvelteServerComponent(component)
} else {
return renderComponent(Placeholder, { content: Comp })
return isSvelteClientComponent(component)
}
}

function wrapInPlaceholder(content: any) {
return renderComponent(Placeholder, { content })
}

export function render(component: any, props: any) {
if (!component) return null

if (isSvelteComponent(component)) {
return renderComponent(component, props)
}

if (typeof component === 'function') {
const result = component(props)

if (isSvelteComponent(result)) {
return result
}

return wrapInPlaceholder(result)
}

// return Comp
return wrapInPlaceholder(component)
}

export const createTable = createTableFactory({ render })
Expand Down