Files
gremiumhub/legalconsenthub/components/FormEngine.vue

52 lines
1.5 KiB
Vue

<template>
<template v-for="(formElement, index) in props.modelValue" :key="formElement.id">
<UFormField>
<component
:is="getResolvedComponent(formElement)"
:form-options="formElement.options"
:disabled="props.disabled"
@update:form-options="updateFormOptions($event, index)"
/>
</UFormField>
<USeparator />
</template>
</template>
<script setup lang="ts">
import type { FormElementDto, FormOptionDto } from '~/.api-client'
import { resolveComponent } from 'vue'
const props = defineProps<{
modelValue: FormElementDto[]
disabled?: boolean
}>()
const emit = defineEmits<{
(e: 'update:modelValue', value: FormElementDto[]): void
}>()
// TODO: Lazy loading?
function getResolvedComponent(formElement: FormElementDto) {
switch (formElement.type) {
case 'CHECKBOX':
return resolveComponent('TheCheckbox')
case 'SELECT':
return resolveComponent('TheSelect')
case 'RADIOBUTTON':
return resolveComponent('TheRadioGroup')
case 'SWITCH':
return resolveComponent('TheSwitch')
case 'TEXTFIELD':
return resolveComponent('TheInput')
default:
return resolveComponent('Unimplemented')
}
}
function updateFormOptions(formOptions: FormOptionDto[], formElementIndex: number) {
const updatedModelValue = [...props.modelValue]
updatedModelValue[formElementIndex] = { ...updatedModelValue[formElementIndex], options: formOptions }
emit('update:modelValue', updatedModelValue)
}
</script>