fix(FE): generalized CheckBoxConditional.vue
This commit is contained in:
parent
2f62ec02cd
commit
a19fa8d159
1 changed files with 53 additions and 48 deletions
|
@ -1,68 +1,73 @@
|
|||
<script setup lang="ts">
|
||||
import { ref, watch, shallowRef, type PropType } from 'vue';
|
||||
import {ref, watch, shallowRef, type PropType, reactive} from 'vue';
|
||||
|
||||
const modelValue = defineModel<Boolean>({ required: true });
|
||||
|
||||
const props = defineProps({
|
||||
checkBoxForm: {
|
||||
type: Object as PropType<{
|
||||
checkBoxField: { label: string };
|
||||
fields: Record<string, {
|
||||
checkBoxField: {label: string},
|
||||
fields: {
|
||||
label: string;
|
||||
component: any;
|
||||
value: any;
|
||||
disabled: boolean;
|
||||
props?: Record<string, any>;
|
||||
}>;
|
||||
title?: string;
|
||||
min: number;
|
||||
}[];
|
||||
}>,
|
||||
required: true,
|
||||
},
|
||||
value: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Reactive state for the checkbox and nested fields
|
||||
const isChecked = ref(props.value);
|
||||
// TODO performance issue by using ref, but losing connectivity to main data struct if not
|
||||
// Either cycle trough each field and add it to a ref var, so its value is updated but the rest is not tracked
|
||||
const fields = ref(props.checkBoxForm.fields);
|
||||
|
||||
// Watch for changes in the checkbox state and update the disabled state of nested fields
|
||||
watch(isChecked, (isChecked) => {
|
||||
for (const key in fields.value) {
|
||||
fields.value[key].disabled = !isChecked;
|
||||
required: true
|
||||
}
|
||||
});
|
||||
|
||||
// Emit setup for nested fields
|
||||
const emit = defineEmits<{
|
||||
(e: 'checkbox-conditional-update', payload: { field: string; value: any }): void
|
||||
}>();
|
||||
|
||||
// Handle checkbox changes
|
||||
watch(modelValue, (newValue) => {
|
||||
if (!newValue) {
|
||||
const fields = props.checkBoxForm.fields;
|
||||
fields.forEach(field => {
|
||||
emit('checkbox-conditional-update', { field, value: null });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function handleFieldUpdate(key: string, value: any) {
|
||||
emit('checkbox-conditional-update', { key, value });
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<v-card>
|
||||
<v-card-text>
|
||||
<!-- Checkbox to toggle nested fields -->
|
||||
<v-checkbox
|
||||
:label="props.checkBoxForm.checkBoxField.label"
|
||||
v-model="isChecked"
|
||||
></v-checkbox>
|
||||
<!-- Checkbox to toggle nested fields -->
|
||||
<v-checkbox
|
||||
:label="props.checkBoxForm.checkBoxField.label"
|
||||
v-model="modelValue"
|
||||
></v-checkbox>
|
||||
|
||||
<!-- Dynamically render nested fields -->
|
||||
<template v-for="(field, key) in fields" :key="key">
|
||||
<component
|
||||
:is="field.component"
|
||||
v-model="field.value"
|
||||
:label="field.label"
|
||||
:disabled="field.disabled"
|
||||
:density="'compact'"
|
||||
rows="2"
|
||||
item-title="type_name"
|
||||
hide-details="auto"
|
||||
class="mb-2"
|
||||
clearable
|
||||
:active="true"
|
||||
v-bind="field.props || {}"
|
||||
/>
|
||||
</template>
|
||||
</v-card-text>
|
||||
</v-card>
|
||||
<!-- Dynamically render nested fields -->
|
||||
<template v-for="(field, key) in props.checkBoxForm.fields" :key="key">
|
||||
<component
|
||||
:is="field.component"
|
||||
:label="field.label"
|
||||
:title="field.title || ''"
|
||||
:disabled="!modelValue"
|
||||
:density="'compact'"
|
||||
:min="field.min"
|
||||
:active="true"
|
||||
v-bind="field.props || {}"
|
||||
v-model="field.value"
|
||||
@update:model-value="handleFieldUpdate(key, field.value)"
|
||||
rows="2"
|
||||
item-title="type_name"
|
||||
hide-details="auto"
|
||||
class="mb-2"
|
||||
clearable
|
||||
/>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<style scoped></style>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue