Refactored approach to parsing multiselect values and ensuring any parsed values trigger a field update
This commit is contained in:
parent
c17cfc2666
commit
5e61575e30
|
@ -1,6 +1,6 @@
|
||||||
<script>
|
<script>
|
||||||
import Picker from "./Picker.svelte"
|
import Picker from "./Picker.svelte"
|
||||||
import { createEventDispatcher } from "svelte"
|
import { createEventDispatcher, onMount } from "svelte"
|
||||||
|
|
||||||
export let value = []
|
export let value = []
|
||||||
export let id = null
|
export let id = null
|
||||||
|
@ -16,8 +16,33 @@
|
||||||
export let autoWidth = false
|
export let autoWidth = false
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
|
const parseValues = value => {
|
||||||
|
return 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
|
||||||
|
}, [])
|
||||||
|
: []
|
||||||
|
}
|
||||||
|
let loaded = false
|
||||||
|
|
||||||
|
$: combinedValues = value ? [...value].concat(options) : []
|
||||||
|
$: superSet = new Set(combinedValues)
|
||||||
|
|
||||||
|
$: if (loaded && options.length != superSet.size) {
|
||||||
|
// ensure that the values being pushed in are valid.
|
||||||
|
dispatch("change", parseValues(value))
|
||||||
|
}
|
||||||
|
|
||||||
$: selectedLookupMap = getSelectedLookupMap(value)
|
$: selectedLookupMap = getSelectedLookupMap(value)
|
||||||
$: optionLookupMap = getOptionLookupMap(options)
|
$: optionLookupMap = getOptionLookupMap(options)
|
||||||
|
|
||||||
$: fieldText = getFieldText(value, optionLookupMap, placeholder)
|
$: fieldText = getFieldText(value, optionLookupMap, placeholder)
|
||||||
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true
|
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true
|
||||||
$: toggleOption = makeToggleOption(selectedLookupMap, value)
|
$: toggleOption = makeToggleOption(selectedLookupMap, value)
|
||||||
|
@ -70,6 +95,10 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
loaded = true
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Picker
|
<Picker
|
||||||
|
|
|
@ -128,23 +128,6 @@
|
||||||
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,
|
||||||
|
@ -170,10 +153,8 @@
|
||||||
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) ?? parsedDefault
|
let initialValue = Helpers.deepGet(initialValues, field) ?? defaultValue
|
||||||
let initialError = null
|
let initialError = null
|
||||||
let fieldId = `id-${Helpers.uuid()}`
|
let fieldId = `id-${Helpers.uuid()}`
|
||||||
const existingField = getField(field)
|
const existingField = getField(field)
|
||||||
|
@ -206,11 +187,11 @@
|
||||||
error: initialError,
|
error: initialError,
|
||||||
disabled:
|
disabled:
|
||||||
disabled || fieldDisabled || (isAutoColumn && !editAutoColumns),
|
disabled || fieldDisabled || (isAutoColumn && !editAutoColumns),
|
||||||
defaultValue: parsedDefault,
|
defaultValue,
|
||||||
validator,
|
validator,
|
||||||
lastUpdate: Date.now(),
|
lastUpdate: Date.now(),
|
||||||
},
|
},
|
||||||
fieldApi: makeFieldApi(field, parsedDefault),
|
fieldApi: makeFieldApi(field, defaultValue),
|
||||||
fieldSchema: schema?.[field] ?? {},
|
fieldSchema: schema?.[field] ?? {},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue