Merge: Refactor form-dialog.vue to Composition API

FoundKeyGang/FoundKey#17
This commit is contained in:
Johann150 2022-07-21 21:13:03 +02:00
commit fef2d9173e
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -35,11 +35,11 @@
</FormSwitch> </FormSwitch>
<FormSelect v-else-if="form[item].type === 'enum'" v-model="values[item]" class="_formBlock"> <FormSelect v-else-if="form[item].type === 'enum'" v-model="values[item]" class="_formBlock">
<template #label><span v-text="form[item].label || item"></span><span v-if="form[item].required === false"> ({{ $ts.optional }})</span></template> <template #label><span v-text="form[item].label || item"></span><span v-if="form[item].required === false"> ({{ $ts.optional }})</span></template>
<option v-for="item in form[item].enum" :key="item.value" :value="item.value">{{ item.label }}</option> <option v-for="blockItem in form[item].enum" :key="blockItem.value" :value="blockItem.value">{{ blockItem.label }}</option>
</FormSelect> </FormSelect>
<FormRadios v-else-if="form[item].type === 'radio'" v-model="values[item]" class="_formBlock"> <FormRadios v-else-if="form[item].type === 'radio'" v-model="values[item]" class="_formBlock">
<template #label><span v-text="form[item].label || item"></span><span v-if="form[item].required === false"> ({{ $ts.optional }})</span></template> <template #label><span v-text="form[item].label || item"></span><span v-if="form[item].required === false"> ({{ $ts.optional }})</span></template>
<option v-for="item in form[item].options" :key="item.value" :value="item.value">{{ item.label }}</option> <option v-for="blockItem in form[item].options" :key="blockItem.value" :value="blockItem.value">{{ blockItem.label }}</option>
</FormRadios> </FormRadios>
<FormRange v-else-if="form[item].type === 'range'" v-model="values[item]" :min="form[item].min" :max="form[item].max" :step="form[item].step" :text-converter="form[item].textConverter" class="_formBlock"> <FormRange v-else-if="form[item].type === 'range'" v-model="values[item]" :min="form[item].min" :max="form[item].max" :step="form[item].step" :text-converter="form[item].textConverter" class="_formBlock">
<template #label><span v-text="form[item].label || item"></span><span v-if="form[item].required === false"> ({{ $ts.optional }})</span></template> <template #label><span v-text="form[item].label || item"></span><span v-if="form[item].required === false"> ({{ $ts.optional }})</span></template>
@ -54,8 +54,8 @@
</XModalWindow> </XModalWindow>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { } from 'vue';
import FormInput from './form/input.vue'; import FormInput from './form/input.vue';
import FormTextarea from './form/textarea.vue'; import FormTextarea from './form/textarea.vue';
import FormSwitch from './form/switch.vue'; import FormSwitch from './form/switch.vue';
@ -65,63 +65,37 @@ import MkButton from './ui/button.vue';
import FormRadios from './form/radios.vue'; import FormRadios from './form/radios.vue';
import XModalWindow from '@/components/ui/modal-window.vue'; import XModalWindow from '@/components/ui/modal-window.vue';
export default defineComponent({ let dialog = $ref<InstanceType<typeof XModalWindow>>();
components: { let values: object = $ref({});
XModalWindow,
FormInput,
FormTextarea,
FormSwitch,
FormSelect,
FormRange,
MkButton,
FormRadios,
},
props: { const props = defineProps<{
title: { title: string,
type: String, form: Record<string, any>,
required: true, }>();
},
form: {
type: Object,
required: true,
},
},
emits: ['done'], const emit = defineEmits<{
(ev: 'done', v: { result?: object, canceled?: boolean }): void,
(ev: 'closed'): void
}>();
data() { function ok(): void {
return { emit('done', {
values: {}, result: values,
}; });
}, dialog.close();
}
created() { function cancel(): void {
for (const item in this.form) { emit('done', {
this.values[item] = this.form[item].default ?? null; canceled: true,
} });
}, dialog.close();
}
methods: { for (const item in props.form) {
ok() { values[item] = props.form[item].default ?? null;
this.$emit('done', { }
result: this.values,
});
this.$refs.dialog.close();
},
cancel() {
this.$emit('done', {
canceled: true,
});
this.$refs.dialog.close();
},
},
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.xkpnjxcv {
}
</style> </style>