Update field disabled state when disabled prop changes
This commit is contained in:
parent
cc89350ccb
commit
8cf672252a
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
// Register field with form
|
// Register field with form
|
||||||
const formApi = formContext?.formApi
|
const formApi = formContext?.formApi
|
||||||
const labelPosition = fieldGroupContext?.labelPosition || "above"
|
const labelPos = fieldGroupContext?.labelPosition || "above"
|
||||||
const formField = formApi?.registerField(
|
const formField = formApi?.registerField(
|
||||||
field,
|
field,
|
||||||
type,
|
type,
|
||||||
|
@ -38,17 +38,23 @@
|
||||||
fieldApi = value?.fieldApi
|
fieldApi = value?.fieldApi
|
||||||
fieldSchema = value?.fieldSchema
|
fieldSchema = value?.fieldSchema
|
||||||
})
|
})
|
||||||
onDestroy(() => unsubscribe && unsubscribe())
|
onDestroy(() => unsubscribe?.())
|
||||||
|
|
||||||
// Keep validation rules up to date
|
// Keep field state up to date with props which might change due to
|
||||||
|
// conditional UI
|
||||||
$: updateValidation(validation)
|
$: updateValidation(validation)
|
||||||
|
$: updateDisabled(disabled)
|
||||||
|
|
||||||
|
// Determine label class from position
|
||||||
|
$: labelClass = labelPos === "above" ? "" : `spectrum-FieldLabel--${labelPos}`
|
||||||
|
|
||||||
const updateValidation = validation => {
|
const updateValidation = validation => {
|
||||||
fieldApi?.updateValidation(validation)
|
fieldApi?.updateValidation(validation)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract label position from field group context
|
const updateDisabled = disabled => {
|
||||||
$: labelPositionClass =
|
fieldApi?.setDisabled(disabled)
|
||||||
labelPosition === "above" ? "" : `spectrum-FieldLabel--${labelPosition}`
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<FieldGroupFallback>
|
<FieldGroupFallback>
|
||||||
|
@ -56,7 +62,7 @@
|
||||||
<label
|
<label
|
||||||
class:hidden={!label}
|
class:hidden={!label}
|
||||||
for={fieldState?.fieldId}
|
for={fieldState?.fieldId}
|
||||||
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelPositionClass}`}
|
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelClass}`}
|
||||||
>
|
>
|
||||||
{label || ""}
|
{label || ""}
|
||||||
</label>
|
</label>
|
||||||
|
|
|
@ -248,10 +248,25 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Updates the disabled state of a certain field
|
||||||
|
const setDisabled = fieldDisabled => {
|
||||||
|
const fieldInfo = getField(field)
|
||||||
|
|
||||||
|
// Auto columns are always disabled
|
||||||
|
const isAutoColumn = !!schema?.[field]?.autocolumn
|
||||||
|
|
||||||
|
// Update disabled state
|
||||||
|
fieldInfo.update(state => {
|
||||||
|
state.fieldState.disabled = disabled || fieldDisabled || isAutoColumn
|
||||||
|
return state
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setValue,
|
setValue,
|
||||||
clearValue,
|
clearValue,
|
||||||
updateValidation,
|
updateValidation,
|
||||||
|
setDisabled,
|
||||||
validate: () => {
|
validate: () => {
|
||||||
// Validate the field by force setting the same value again
|
// Validate the field by force setting the same value again
|
||||||
const { fieldState } = get(getField(field))
|
const { fieldState } = get(getField(field))
|
||||||
|
|
Loading…
Reference in New Issue