1
0
Fork 0
mirror of synced 2024-10-02 10:08:09 +13:00

Fix multi step settings rendering and improve multi step form block client rendering when inside builder

This commit is contained in:
Andrew Kingston 2023-12-14 14:33:43 +00:00
parent 40ac6c7aec
commit a244b34e53
4 changed files with 36 additions and 47 deletions

View file

@ -23,18 +23,32 @@
setContext("multi-step-form-block", multiStepStore) setContext("multi-step-form-block", multiStepStore)
$: defaultProps = Utils.buildMultiStepFormBlockDefaultProps({
_id: componentInstance._id,
stepCount: stepCount,
step: $multiStepStore.currentStep,
})
$: stepCount = value?.length || 0 $: stepCount = value?.length || 0
$: updateStore(stepCount) $: updateStore(stepCount)
$: dataSource = getDatasourceForProvider($currentAsset, componentInstance) $: dataSource = getDatasourceForProvider($currentAsset, componentInstance)
$: emitCurrentStep($currentStep) $: emitCurrentStep($currentStep)
$: stepLabel = getStepLabel($multiStepStore) $: stepLabel = getStepLabel($multiStepStore)
$: stepDef = getDefinition(stepLabel) $: stepDef = getDefinition(stepLabel)
$: stepInstance = buildPseudoInstance(value, $currentStep, defaultProps) $: stepSettings = value?.[$currentStep] || {}
$: defaults = Utils.buildMultiStepFormBlockDefaultProps({
_id: componentInstance._id,
stepCount: $multiStepStore.stepCount,
currentStep: $multiStepStore.currentStep,
actionType: componentInstance.actionType,
dataSource: componentInstance.dataSource,
})
$: stepInstance = {
_id: Helpers.uuid(),
_component: componentType,
_instanceName: `Step ${currentStep + 1}`,
title: stepSettings.title ?? defaults.title,
buttons: stepSettings.buttons || defaults.buttons,
fields: stepSettings.fields,
desc: stepSettings.desc,
// Needed for field configuration
dataSource,
}
const getDefinition = stepLabel => { const getDefinition = stepLabel => {
let def = cloneDeep(store.actions.components.getDefinition(componentType)) let def = cloneDeep(store.actions.components.getDefinition(componentType))
@ -130,22 +144,6 @@
updateStep(field, val) updateStep(field, val)
} }
} }
const buildPseudoInstance = (value, currentStep, defaultProps) => {
const { buttons, fields, title, desc } = value?.[currentStep] || {}
return {
_id: Helpers.uuid(),
_component: componentType,
_instanceName: `Step ${currentStep + 1}`,
title: title ?? defaultProps.title,
buttons: buttons || defaultProps.buttons,
fields,
desc,
// Needed for field configuration
dataSource,
}
}
</script> </script>
<div class="nested-section"> <div class="nested-section">

View file

@ -1,20 +1,24 @@
<script> <script>
import BlockComponent from "components/BlockComponent.svelte" import BlockComponent from "components/BlockComponent.svelte"
import { getContext } from "svelte" import { getContext, setContext } from "svelte"
import { builderStore } from "stores" import { builderStore } from "stores"
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"
export let actionType export let actionType
export let rowId export let rowId
export let noRowsMessage export let noRowsMessage
export let steps export let steps
export let dataSource export let dataSource
export let initialFormStep = 1
const { fetchDatasourceSchema } = getContext("sdk") const { fetchDatasourceSchema } = getContext("sdk")
const component = getContext("component") const component = getContext("component")
// Set current step context to force child form to use it
const currentStep = writable(1)
setContext("current-step", currentStep)
const FieldTypeToComponentMap = { const FieldTypeToComponentMap = {
string: "stringfield", string: "stringfield",
number: "numberfield", number: "numberfield",
@ -35,30 +39,29 @@
$: fetchSchema(dataSource) $: fetchSchema(dataSource)
$: enrichedSteps = enrichSteps(steps, schema, $component.id) $: enrichedSteps = enrichSteps(steps, schema, $component.id)
$: currentStep = getCurrentStep(enrichedSteps, $builderStore, $component) $: updateCurrentStep(enrichedSteps, $builderStore, $component)
const getCurrentStep = (steps, builderStore, component) => { const updateCurrentStep = (steps, builderStore, component) => {
const { componentId, step } = builderStore.metadata || {} const { componentId, step } = builderStore.metadata || {}
// If we aren't in the builder or aren't selected then don't return a form // If we aren't in the builder or aren't selected then don't update the step
// step at all. This will let the form component take over state for which // context at all, allowing the normal form to take control.
// step is active.
if ( if (
!component.selected || !component.selected ||
!builderStore.inBuilder || !builderStore.inBuilder ||
componentId !== component.id componentId !== component.id
) { ) {
return null return
} }
// Ensure we have a valid step selected // Ensure we have a valid step selected
let currentStep = Math.min(step || 0, steps.length - 1) let newStep = Math.min(step || 0, steps.length - 1)
// Sanity check // Sanity check
currentStep = Math.max(currentStep, 0) newStep = Math.max(newStep, 0)
// Add 1 because the form component expects 1 indexed rather than 0 indexed // Add 1 because the form component expects 1 indexed rather than 0 indexed
return currentStep + 1 currentStep.set(newStep + 1)
} }
const getPropsForField = field => { const getPropsForField = field => {
@ -124,8 +127,6 @@
context="form" context="form"
props={{ props={{
dataSource, dataSource,
initialFormStep,
step: currentStep,
actionType: actionType === "Create" ? "Create" : "Update", actionType: actionType === "Create" ? "Create" : "Update",
readonly: actionType === "View", readonly: actionType === "View",
}} }}

View file

@ -11,7 +11,6 @@
export let readonly = false export let readonly = false
export let actionType = "Create" export let actionType = "Create"
export let initialFormStep = 1 export let initialFormStep = 1
export let step
// Not exposed as a builder setting. Used internally to disable validation // Not exposed as a builder setting. Used internally to disable validation
// for fields rendered in things like search blocks. // for fields rendered in things like search blocks.
@ -35,16 +34,7 @@
let loaded = false let loaded = false
let schema let schema
let table let table
let currentStep = writable(getInitialFormStep()) let currentStep = getContext("current-step") || writable(getInitialFormStep())
$: if (
currentStep &&
Number.isInteger($currentStep) &&
Number.isInteger(step) &&
step !== currentStep
) {
currentStep.set(step)
}
$: fetchSchema(dataSource) $: fetchSchema(dataSource)
$: schemaKey = generateSchemaKey(schema) $: schemaKey = generateSchemaKey(schema)

View file

@ -241,7 +241,7 @@ export const buildFormBlockButtonConfig = props => {
return defaultButtons return defaultButtons
} }
export const buildMultiStepFormBlockDefaultProps = props => { export const buildMultiStepFormBlockDefaultProps = (props, log = false) => {
const { _id, stepCount, currentStep, actionType, dataSource } = props || {} const { _id, stepCount, currentStep, actionType, dataSource } = props || {}
// Sanity check // Sanity check