Allow dynamic re-registration of form field components
This commit is contained in:
parent
9b988b60b0
commit
5f5ca5fdf7
|
@ -23,7 +23,7 @@
|
||||||
// Register field with form
|
// Register field with form
|
||||||
const formApi = formContext?.formApi
|
const formApi = formContext?.formApi
|
||||||
const labelPos = fieldGroupContext?.labelPosition || "above"
|
const labelPos = fieldGroupContext?.labelPosition || "above"
|
||||||
const formField = formApi?.registerField(
|
$: formField = formApi?.registerField(
|
||||||
field,
|
field,
|
||||||
type,
|
type,
|
||||||
defaultValue,
|
defaultValue,
|
||||||
|
@ -37,29 +37,16 @@
|
||||||
$: $component.editing && labelNode?.focus()
|
$: $component.editing && labelNode?.focus()
|
||||||
|
|
||||||
// Update form properties in parent component on every store change
|
// Update form properties in parent component on every store change
|
||||||
const unsubscribe = formField?.subscribe(value => {
|
$: unsubscribe = formField?.subscribe(value => {
|
||||||
fieldState = value?.fieldState
|
fieldState = value?.fieldState
|
||||||
fieldApi = value?.fieldApi
|
fieldApi = value?.fieldApi
|
||||||
fieldSchema = value?.fieldSchema
|
fieldSchema = value?.fieldSchema
|
||||||
})
|
})
|
||||||
onDestroy(() => unsubscribe?.())
|
onDestroy(() => unsubscribe?.())
|
||||||
|
|
||||||
// Keep field state up to date with props which might change due to
|
|
||||||
// conditional UI
|
|
||||||
$: updateValidation(validation)
|
|
||||||
$: updateDisabled(disabled)
|
|
||||||
|
|
||||||
// Determine label class from position
|
// Determine label class from position
|
||||||
$: labelClass = labelPos === "above" ? "" : `spectrum-FieldLabel--${labelPos}`
|
$: labelClass = labelPos === "above" ? "" : `spectrum-FieldLabel--${labelPos}`
|
||||||
|
|
||||||
const updateValidation = validation => {
|
|
||||||
fieldApi?.updateValidation(validation)
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateDisabled = disabled => {
|
|
||||||
fieldApi?.setDisabled(disabled)
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateLabel = e => {
|
const updateLabel = e => {
|
||||||
builderStore.actions.updateProp("label", e.target.textContent)
|
builderStore.actions.updateProp("label", e.target.textContent)
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,15 +96,14 @@
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we've already registered this field then wipe any errors and
|
// If we've already registered this field then keep some existing state
|
||||||
// return the existing field
|
let initialValue = initialValues[field] ?? defaultValue
|
||||||
|
let fieldId = `id-${generateID()}`
|
||||||
const existingField = getField(field)
|
const existingField = getField(field)
|
||||||
if (existingField) {
|
if (existingField) {
|
||||||
existingField.update(state => {
|
const { fieldState } = get(existingField)
|
||||||
state.fieldState.error = null
|
initialValue = fieldState.value ?? initialValue
|
||||||
return state
|
fieldId = fieldState.fieldId
|
||||||
})
|
|
||||||
return existingField
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto columns are always disabled
|
// Auto columns are always disabled
|
||||||
|
@ -125,8 +124,8 @@
|
||||||
type,
|
type,
|
||||||
step: step || 1,
|
step: step || 1,
|
||||||
fieldState: {
|
fieldState: {
|
||||||
fieldId: `id-${generateID()}`,
|
fieldId,
|
||||||
value: initialValues[field] ?? defaultValue,
|
value: initialValue,
|
||||||
error: null,
|
error: null,
|
||||||
disabled: disabled || fieldDisabled || isAutoColumn,
|
disabled: disabled || fieldDisabled || isAutoColumn,
|
||||||
defaultValue,
|
defaultValue,
|
||||||
|
|
Loading…
Reference in New Issue