DRY
This commit is contained in:
parent
0e139c19da
commit
6ca25d400a
|
@ -0,0 +1,56 @@
|
||||||
|
<script>
|
||||||
|
import BlockComponent from "components/BlockComponent.svelte"
|
||||||
|
|
||||||
|
export let field
|
||||||
|
export let schema
|
||||||
|
export let order
|
||||||
|
|
||||||
|
const FieldTypeToComponentMap = {
|
||||||
|
string: "stringfield",
|
||||||
|
number: "numberfield",
|
||||||
|
bigint: "bigintfield",
|
||||||
|
options: "optionsfield",
|
||||||
|
array: "multifieldselect",
|
||||||
|
boolean: "booleanfield",
|
||||||
|
longform: "longformfield",
|
||||||
|
datetime: "datetimefield",
|
||||||
|
attachment: "attachmentfield",
|
||||||
|
link: "relationshipfield",
|
||||||
|
json: "jsonfield",
|
||||||
|
barcodeqr: "codescanner",
|
||||||
|
bb_reference: "bbreferencefield",
|
||||||
|
}
|
||||||
|
|
||||||
|
const getComponentForField = field => {
|
||||||
|
const fieldSchemaName = field.field || field.name
|
||||||
|
if (!fieldSchemaName || !schema?.[fieldSchemaName]) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const type = schema[fieldSchemaName].type
|
||||||
|
return FieldTypeToComponentMap[type]
|
||||||
|
}
|
||||||
|
|
||||||
|
const getPropsForField = field => {
|
||||||
|
let fieldProps = field._component
|
||||||
|
? {
|
||||||
|
...field,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
field: field.name,
|
||||||
|
label: field.name,
|
||||||
|
placeholder: field.name,
|
||||||
|
_instanceName: field.name,
|
||||||
|
}
|
||||||
|
return fieldProps
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if getComponentForField(field) && field.active}
|
||||||
|
<BlockComponent
|
||||||
|
type={getComponentForField(field)}
|
||||||
|
props={getPropsForField(field)}
|
||||||
|
{order}
|
||||||
|
interactive
|
||||||
|
name={field?.field}
|
||||||
|
/>
|
||||||
|
{/if}
|
|
@ -6,6 +6,7 @@
|
||||||
import { Utils } from "@budibase/frontend-core"
|
import { Utils } from "@budibase/frontend-core"
|
||||||
import FormBlockWrapper from "./form/FormBlockWrapper.svelte"
|
import FormBlockWrapper from "./form/FormBlockWrapper.svelte"
|
||||||
import { writable } from "svelte/store"
|
import { writable } from "svelte/store"
|
||||||
|
import FormBlockComponent from "./FormBlockComponent.svelte"
|
||||||
|
|
||||||
export let actionType
|
export let actionType
|
||||||
export let rowId
|
export let rowId
|
||||||
|
@ -23,22 +24,6 @@
|
||||||
const currentStep = writable(1)
|
const currentStep = writable(1)
|
||||||
setContext("current-step", currentStep)
|
setContext("current-step", currentStep)
|
||||||
|
|
||||||
const FieldTypeToComponentMap = {
|
|
||||||
string: "stringfield",
|
|
||||||
number: "numberfield",
|
|
||||||
bigint: "bigintfield",
|
|
||||||
options: "optionsfield",
|
|
||||||
array: "multifieldselect",
|
|
||||||
boolean: "booleanfield",
|
|
||||||
longform: "longformfield",
|
|
||||||
datetime: "datetimefield",
|
|
||||||
attachment: "attachmentfield",
|
|
||||||
link: "relationshipfield",
|
|
||||||
json: "jsonfield",
|
|
||||||
barcodeqr: "codescanner",
|
|
||||||
bb_reference: "bbreferencefield",
|
|
||||||
}
|
|
||||||
|
|
||||||
let schema
|
let schema
|
||||||
|
|
||||||
$: fetchSchema(dataSource)
|
$: fetchSchema(dataSource)
|
||||||
|
@ -68,27 +53,6 @@
|
||||||
currentStep.set(newStep + 1)
|
currentStep.set(newStep + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const getPropsForField = field => {
|
|
||||||
if (field._component) {
|
|
||||||
return field
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
field: field.name,
|
|
||||||
label: field.name,
|
|
||||||
placeholder: field.name,
|
|
||||||
_instanceName: field.name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const getComponentForField = field => {
|
|
||||||
const fieldSchemaName = field.field || field.name
|
|
||||||
if (!fieldSchemaName || !schema?.[fieldSchemaName]) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
const type = schema[fieldSchemaName].type
|
|
||||||
return FieldTypeToComponentMap[type]
|
|
||||||
}
|
|
||||||
|
|
||||||
const fetchSchema = async () => {
|
const fetchSchema = async () => {
|
||||||
schema = (await fetchDatasourceSchema(dataSource)) || {}
|
schema = (await fetchDatasourceSchema(dataSource)) || {}
|
||||||
}
|
}
|
||||||
|
@ -195,15 +159,7 @@
|
||||||
class:mobile={$context.device.mobile}
|
class:mobile={$context.device.mobile}
|
||||||
>
|
>
|
||||||
{#each step.fields as field, fieldIdx (`${field.field || field.name}_${fieldIdx}`)}
|
{#each step.fields as field, fieldIdx (`${field.field || field.name}_${fieldIdx}`)}
|
||||||
{#if getComponentForField(field)}
|
<FormBlockComponent {field} {schema} order={fieldIdx} />
|
||||||
<BlockComponent
|
|
||||||
type={getComponentForField(field)}
|
|
||||||
props={getPropsForField(field)}
|
|
||||||
order={fieldIdx}
|
|
||||||
interactive
|
|
||||||
name={field.field}
|
|
||||||
/>
|
|
||||||
{/if}
|
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
</BlockComponent>
|
</BlockComponent>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import BlockComponent from "components/BlockComponent.svelte"
|
import BlockComponent from "components/BlockComponent.svelte"
|
||||||
import Placeholder from "components/app/Placeholder.svelte"
|
import Placeholder from "components/app/Placeholder.svelte"
|
||||||
import { getContext } from "svelte"
|
import { getContext } from "svelte"
|
||||||
|
import FormBlockComponent from "../FormBlockComponent.svelte"
|
||||||
|
|
||||||
export let dataSource
|
export let dataSource
|
||||||
export let actionType
|
export let actionType
|
||||||
|
@ -14,49 +15,11 @@
|
||||||
export let buttonPosition = "bottom"
|
export let buttonPosition = "bottom"
|
||||||
export let schema
|
export let schema
|
||||||
|
|
||||||
const FieldTypeToComponentMap = {
|
|
||||||
string: "stringfield",
|
|
||||||
number: "numberfield",
|
|
||||||
bigint: "bigintfield",
|
|
||||||
options: "optionsfield",
|
|
||||||
array: "multifieldselect",
|
|
||||||
boolean: "booleanfield",
|
|
||||||
longform: "longformfield",
|
|
||||||
datetime: "datetimefield",
|
|
||||||
attachment: "attachmentfield",
|
|
||||||
link: "relationshipfield",
|
|
||||||
json: "jsonfield",
|
|
||||||
barcodeqr: "codescanner",
|
|
||||||
bb_reference: "bbreferencefield",
|
|
||||||
}
|
|
||||||
const context = getContext("context")
|
const context = getContext("context")
|
||||||
|
|
||||||
let formId
|
let formId
|
||||||
|
|
||||||
$: renderHeader = buttons || title
|
$: renderHeader = buttons || title
|
||||||
|
|
||||||
const getComponentForField = field => {
|
|
||||||
const fieldSchemaName = field.field || field.name
|
|
||||||
if (!fieldSchemaName || !schema?.[fieldSchemaName]) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
const type = schema[fieldSchemaName].type
|
|
||||||
return FieldTypeToComponentMap[type]
|
|
||||||
}
|
|
||||||
|
|
||||||
const getPropsForField = field => {
|
|
||||||
let fieldProps = field._component
|
|
||||||
? {
|
|
||||||
...field,
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
field: field.name,
|
|
||||||
label: field.name,
|
|
||||||
placeholder: field.name,
|
|
||||||
_instanceName: field.name,
|
|
||||||
}
|
|
||||||
return fieldProps
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if fields?.length}
|
{#if fields?.length}
|
||||||
|
@ -132,15 +95,7 @@
|
||||||
<BlockComponent type="container">
|
<BlockComponent type="container">
|
||||||
<div class="form-block fields" class:mobile={$context.device.mobile}>
|
<div class="form-block fields" class:mobile={$context.device.mobile}>
|
||||||
{#each fields as field, idx}
|
{#each fields as field, idx}
|
||||||
{#if getComponentForField(field) && field.active}
|
<FormBlockComponent {field} {schema} order={idx} />
|
||||||
<BlockComponent
|
|
||||||
type={getComponentForField(field)}
|
|
||||||
props={getPropsForField(field)}
|
|
||||||
order={idx}
|
|
||||||
interactive
|
|
||||||
name={field?.field}
|
|
||||||
/>
|
|
||||||
{/if}
|
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
</BlockComponent>
|
</BlockComponent>
|
||||||
|
|
Loading…
Reference in New Issue