1
0
Fork 0
mirror of synced 2024-09-28 07:11:40 +12:00
budibase/packages/client/src/components/Component.svelte

54 lines
1.7 KiB
Svelte
Raw Normal View History

<script>
2020-11-19 22:24:58 +13:00
import { getContext } from "svelte"
import * as ComponentLibrary from "@budibase/standard-components"
import Router from "./Router.svelte"
2020-11-19 22:31:49 +13:00
import enrichDataBinding from "../utils/enrichDataBinding"
2020-11-19 22:24:58 +13:00
const dataProviderStore = getContext("data")
export let definition = {}
2020-11-19 22:24:58 +13:00
$: contextRow = dataProviderStore ? $dataProviderStore.row : undefined
$: componentProps = extractValidProps(definition, contextRow)
$: children = definition._children
$: componentName = extractComponentName(definition._component)
$: constructor = getComponentConstructor(componentName)
$: id = `${componentName}-${definition._id}`
$: styles = { ...definition._styles, id }
// Extracts the actual component name from the library name
const extractComponentName = name => {
const split = name?.split("/")
return split?.[split.length - 1]
}
// Extracts valid props to pass to the real svelte component
2020-11-19 22:24:58 +13:00
const extractValidProps = (component, row) => {
let props = {}
2020-11-19 22:31:49 +13:00
const enrich = value => enrichDataBinding(value, { data: row })
Object.entries(component)
.filter(([name]) => !name.startsWith("_"))
.forEach(([key, value]) => {
2020-11-19 22:31:49 +13:00
props[key] = row === undefined ? value : enrich(value)
})
return props
}
// Gets the component constructor for the specified component
const getComponentConstructor = name => {
return name === "screenslot" ? Router : ComponentLibrary[componentName]
}
$: console.log("Rendering: " + componentName)
</script>
{#if constructor}
<svelte:component this={constructor} {...componentProps} {styles}>
{#if children && children.length}
{#each children as child}
<svelte:self definition={child} />
{/each}
{/if}
</svelte:component>
{/if}