Pushed default parsing up into the InnerForm component to ensure that invalid default values are removed entirely. Fixes for custom validation for array types
This commit is contained in:
parent
b31f7f4b87
commit
0e4a7ace08
|
@ -15,25 +15,12 @@
|
||||||
export let sort = false
|
export let sort = false
|
||||||
export let autoWidth = false
|
export let autoWidth = false
|
||||||
|
|
||||||
$: streamed = Array.isArray(value)
|
|
||||||
? value.reduce((acc, entry) => {
|
|
||||||
if (typeof ele === "string" && entry.trim() === "") {
|
|
||||||
return acc
|
|
||||||
}
|
|
||||||
let processedOption = String(entry)
|
|
||||||
if (options.indexOf(processedOption) > -1) {
|
|
||||||
acc.push(processedOption)
|
|
||||||
}
|
|
||||||
return acc
|
|
||||||
}, [])
|
|
||||||
: []
|
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
$: selectedLookupMap = getSelectedLookupMap(streamed)
|
$: selectedLookupMap = getSelectedLookupMap(value)
|
||||||
$: optionLookupMap = getOptionLookupMap(options)
|
$: optionLookupMap = getOptionLookupMap(options)
|
||||||
$: fieldText = getFieldText(streamed, optionLookupMap, placeholder)
|
$: fieldText = getFieldText(value, optionLookupMap, placeholder)
|
||||||
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true
|
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true
|
||||||
$: toggleOption = makeToggleOption(selectedLookupMap, streamed)
|
$: toggleOption = makeToggleOption(selectedLookupMap, value)
|
||||||
|
|
||||||
const getFieldText = (value, map, placeholder) => {
|
const getFieldText = (value, map, placeholder) => {
|
||||||
if (Array.isArray(value) && value.length > 0) {
|
if (Array.isArray(value) && value.length > 0) {
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
|
|
||||||
const getFieldText = (value, options, placeholder) => {
|
const getFieldText = (value, options, placeholder) => {
|
||||||
// Always use placeholder if no value
|
// Always use placeholder if no value
|
||||||
if (value == null || value === "") {
|
if (value == null || value === "" || value.length == 0) {
|
||||||
return placeholder || "Choose an option"
|
return placeholder || "Choose an option"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -305,6 +305,9 @@
|
||||||
getOptionLabel={x => x}
|
getOptionLabel={x => x}
|
||||||
getOptionValue={x => x}
|
getOptionValue={x => x}
|
||||||
value={rule.value}
|
value={rule.value}
|
||||||
|
on:change={e => {
|
||||||
|
rule.value = e.detail
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
{:else if rule.type === "boolean"}
|
{:else if rule.type === "boolean"}
|
||||||
<Select
|
<Select
|
||||||
|
|
|
@ -27,19 +27,12 @@
|
||||||
$: formField = formApi?.registerField(
|
$: formField = formApi?.registerField(
|
||||||
field,
|
field,
|
||||||
type,
|
type,
|
||||||
parseDefaultValue(defaultValue),
|
defaultValue,
|
||||||
disabled,
|
disabled,
|
||||||
validation,
|
validation,
|
||||||
formStep
|
formStep
|
||||||
)
|
)
|
||||||
|
|
||||||
const parseDefaultValue = defaultValue => {
|
|
||||||
if (Array.isArray(defaultValue) && type === "array") {
|
|
||||||
return defaultValue.map(val => String(val))
|
|
||||||
}
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
$: schemaType = fieldSchema?.type !== "formula" ? fieldSchema?.type : "string"
|
$: schemaType = fieldSchema?.type !== "formula" ? fieldSchema?.type : "string"
|
||||||
|
|
||||||
// Focus label when editing
|
// Focus label when editing
|
||||||
|
|
|
@ -128,6 +128,23 @@
|
||||||
return fields.find(field => get(field).name === name)
|
return fields.find(field => get(field).name === name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getDefault = (defaultValue, schema, type) => {
|
||||||
|
// Remove any values not present in the field schema
|
||||||
|
// Convert any values supplied to string
|
||||||
|
if (Array.isArray(defaultValue) && type == "array") {
|
||||||
|
return defaultValue.reduce((acc, entry) => {
|
||||||
|
let processedOption = String(entry)
|
||||||
|
let schemaOptions = schema.constraints.inclusion
|
||||||
|
if (schemaOptions.indexOf(processedOption) > -1) {
|
||||||
|
acc.push(processedOption)
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
} else {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const formApi = {
|
const formApi = {
|
||||||
registerField: (
|
registerField: (
|
||||||
field,
|
field,
|
||||||
|
@ -143,6 +160,7 @@
|
||||||
|
|
||||||
// Create validation function based on field schema
|
// Create validation function based on field schema
|
||||||
const schemaConstraints = schema?.[field]?.constraints
|
const schemaConstraints = schema?.[field]?.constraints
|
||||||
|
|
||||||
const validator = disableValidation
|
const validator = disableValidation
|
||||||
? null
|
? null
|
||||||
: createValidatorFromConstraints(
|
: createValidatorFromConstraints(
|
||||||
|
@ -152,8 +170,10 @@
|
||||||
table
|
table
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const parsedDefault = getDefault(defaultValue, schema?.[field], type)
|
||||||
|
|
||||||
// If we've already registered this field then keep some existing state
|
// If we've already registered this field then keep some existing state
|
||||||
let initialValue = Helpers.deepGet(initialValues, field) ?? defaultValue
|
let initialValue = Helpers.deepGet(initialValues, field) ?? parsedDefault
|
||||||
let initialError = null
|
let initialError = null
|
||||||
let fieldId = `id-${Helpers.uuid()}`
|
let fieldId = `id-${Helpers.uuid()}`
|
||||||
const existingField = getField(field)
|
const existingField = getField(field)
|
||||||
|
@ -186,11 +206,11 @@
|
||||||
error: initialError,
|
error: initialError,
|
||||||
disabled:
|
disabled:
|
||||||
disabled || fieldDisabled || (isAutoColumn && !editAutoColumns),
|
disabled || fieldDisabled || (isAutoColumn && !editAutoColumns),
|
||||||
defaultValue,
|
defaultValue: parsedDefault,
|
||||||
validator,
|
validator,
|
||||||
lastUpdate: Date.now(),
|
lastUpdate: Date.now(),
|
||||||
},
|
},
|
||||||
fieldApi: makeFieldApi(field, defaultValue),
|
fieldApi: makeFieldApi(field, parsedDefault),
|
||||||
fieldSchema: schema?.[field] ?? {},
|
fieldSchema: schema?.[field] ?? {},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue