budibase/packages/bbui/src/Table/CellRenderer.svelte

70 lines
1.9 KiB
Svelte
Raw Normal View History

<script>
import StringRenderer from "./StringRenderer.svelte"
import BooleanRenderer from "./BooleanRenderer.svelte"
import DateTimeRenderer from "./DateTimeRenderer.svelte"
import RelationshipRenderer from "./RelationshipRenderer.svelte"
import AttachmentRenderer from "./AttachmentRenderer.svelte"
import ArrayRenderer from "./ArrayRenderer.svelte"
2021-10-18 16:08:57 +02:00
import InternalRenderer from "./InternalRenderer.svelte"
import { processStringSync } from "@budibase/string-templates"
2021-10-13 10:29:01 +02:00
export let row
export let schema
export let value
export let customRenderers = []
let renderer
const typeMap = {
boolean: BooleanRenderer,
datetime: DateTimeRenderer,
link: RelationshipRenderer,
attachment: AttachmentRenderer,
string: StringRenderer,
options: StringRenderer,
number: StringRenderer,
longform: StringRenderer,
array: ArrayRenderer,
2021-10-18 16:08:57 +02:00
internal: InternalRenderer,
}
$: type = getType(schema)
2021-05-04 12:32:22 +02:00
$: customRenderer = customRenderers?.find(x => x.column === schema?.name)
2021-10-13 12:39:36 +02:00
$: renderer = customRenderer?.component ?? typeMap[type] ?? StringRenderer
$: width = schema?.width || "150px"
$: cellValue = getCellValue(value, schema.template)
const getType = schema => {
// Use a string renderer for dates if we use a custom template
if (schema?.type === "datetime" && schema?.template) {
return "string"
}
return schema?.type || "string"
}
const getCellValue = (value, template) => {
if (!template) {
return value
}
return processStringSync(template, { value })
}
</script>
{#if renderer && (customRenderer || (cellValue != null && cellValue !== ""))}
<div style="--max-cell-width: {schema.width ? 'none' : '200px'};">
<svelte:component
this={renderer}
{row}
{schema}
value={cellValue}
on:clickrelationship
>
<slot />
</svelte:component>
</div>
{/if}
<style>
div {
display: contents;
}
</style>