every app components: switching from static to eval preserves the user data input

This commit is contained in:
Clement Zhang
2024-11-07 14:18:14 +01:00
parent 14ea9d6b55
commit 34249143e7
2 changed files with 22 additions and 21 deletions
@@ -229,9 +229,7 @@
newComponentInput.expr = JSON.stringify(newComponentInput.value)
newComponentInput.isLabeled = labeled
const newSubFieldType = labeled ? 'labeledselect' : 'simplestringselect'
dispatch('componentInputChange', { newComponentInput, newSubFieldType })
dispatch('componentInputChange', newComponentInput)
}
/** Transforms items string[] to ObjectOption[ and vice versa. Mutating items will fire handleItemsChange. */
@@ -39,30 +39,33 @@
onMount(() => {
/** preserve the `labeled` toggling when switching from eval to static */
if (
componentInput?.isLabeled &&
fieldType === 'array' &&
subFieldType === 'simplestringselect'
) {
subFieldType = 'labeledselect'
if (componentInput !== undefined) {
handleComponentInputChange(componentInput)
}
})
/**
* Workaround for MultiSelect
* Every components: switching from `static` to `eval` preserves the user data
*
* Changing fieldType in <ArrayStaticInputEditor /> needs to mutate this component data and this component data type.
* Multiselect: toggling `labeled` needs to mutate input value and input fieldType.
* */
function handleComponentInputChange(newData: {
newComponentInput: StaticInput<any>
newSubFieldType: InputType
}) {
if (!newData) {
console.error('fired a componentInputChange with no data?')
return
}
const { newComponentInput, newSubFieldType } = newData
function handleComponentInputChange(newComponentInput: StaticInput<any>) {
componentInput = { ...componentInput, ...newComponentInput }
subFieldType = newSubFieldType
if (componentInput?.isLabeled) {
if (fieldType === 'array' && subFieldType === 'simplestringselect') {
subFieldType = 'labeledselect'
}
if (fieldType === 'simplestringselect') {
fieldType = 'labeledselect'
}
} else if (componentInput?.isLabeled === false) {
if (fieldType === 'array' && subFieldType === 'labeledselect') {
subFieldType = 'simplestringselect'
}
if (fieldType === 'labeledselect') {
fieldType = 'simplestringselect'
}
}
}
</script>