From d2cf9522b11b47ae1e051a7207abf858e0a111a9 Mon Sep 17 00:00:00 2001 From: axolotle Date: Thu, 22 Aug 2024 00:45:07 +0200 Subject: [PATCH] refactor: use defineModel --- app/src/components/globals/CardForm.vue | 10 +++++----- app/src/components/globals/FormField.vue | 6 ++---- .../components/globals/FormFieldMultiple.vue | 17 +++++++++-------- .../components/globals/FormFieldReadonly.vue | 5 +++-- app/src/components/globals/ViewSearch.vue | 2 -- .../components/globals/formItems/AdressItem.vue | 1 - .../globals/formItems/CheckboxItem.vue | 9 ++------- .../components/globals/formItems/FileItem.vue | 9 ++++++--- .../components/globals/formItems/InputItem.vue | 12 ++++-------- .../components/globals/formItems/TagsItem.vue | 5 ++--- .../globals/formItems/TagsSelectizeItem.vue | 9 ++++----- .../globals/formItems/TextAreaItem.vue | 4 ++-- app/src/types/form.ts | 11 ++++------- 13 files changed, 43 insertions(+), 57 deletions(-) diff --git a/app/src/components/globals/CardForm.vue b/app/src/components/globals/CardForm.vue index ad5e2988..a0ad8b6d 100644 --- a/app/src/components/globals/CardForm.vue +++ b/app/src/components/globals/CardForm.vue @@ -19,7 +19,6 @@ import { isDisplayComponent, isWritableComponent } from '@/types/form' const props = withDefaults( defineProps<{ id?: string - modelValue?: MV fields?: FFD validations?: FormValidation submitText?: string @@ -31,7 +30,6 @@ const props = withDefaults( }>(), { id: 'ynh-form', - modelValue: undefined, fields: undefined, validations: undefined, submitText: undefined, @@ -71,6 +69,8 @@ const slots = defineSlots< } >() +const modelValue = defineModel() + const { t } = useI18n() const globalErrorFeedback = computed(() => { @@ -94,7 +94,7 @@ const sections = computed(() => { function onModelUpdate(key: keyof MV, value: MV[keyof MV]) { emit('update:modelValue', { - ...props.modelValue!, + ...modelValue.value!, [key]: value, }) } @@ -119,7 +119,7 @@ const Fields = createReusableTemplate<{ @@ -130,7 +130,7 @@ const Fields = createReusableTemplate<{ >(), { prepend: undefined, rules: undefined, - modelValue: undefined, validation: undefined, }) @@ -51,7 +50,7 @@ const slots = defineSlots<{ description?: any }>() -const model = defineModel() +const modelValue = defineModel() const attrs = useAttrs() const { t } = useI18n() @@ -137,7 +136,6 @@ const [DefineTemplate, ReuseTemplate] = createReusableTemplate<{ v-bind="{ ...(props.cProps ?? ({} as ItemComponentToItemProps[C])), ariaDescribedby, - modelValue: props.modelValue, state, validation: validation, }" @@ -146,7 +144,7 @@ const [DefineTemplate, ReuseTemplate] = createReusableTemplate<{ () const slots = defineSlots<{ @@ -62,6 +61,8 @@ const slots = defineSlots<{ description?: () => any }>() +const modelValue = defineModel() + const { t } = useI18n() const attrs = useAttrs() @@ -104,7 +105,7 @@ const error = computed(() => { const subProps = computed>[]>(() => { return ( - props.modelValue?.map((modelValue: ArrInnerType, i) => { + modelValue.value?.map((modelValue: ArrInnerType, i) => { return { cProps: { ...(props.cProps ?? ({} as ItemComponentToItemProps[C])), @@ -143,22 +144,22 @@ const errorMessage = computed(() => { }) function addElement() { - const value = [...(props?.modelValue || []), props.defaultValue!()] as MV + const value = [...(modelValue.value || []), props.defaultValue!()] as MV emit('update:modelValue', value) // FIXME: Focus newly inserted form item } function removeElement(index: number) { - if (!props.modelValue) return - const value = [...props.modelValue] as MV + if (!modelValue.value) return + const value = [...modelValue.value] as MV value.splice(index, 1) emit('update:modelValue', value) } function updateElement(index: number, newValue: ArrInnerType) { - if (!props.modelValue) return - const value = [...props.modelValue] as MV + if (!modelValue.value) return + const value = [...modelValue.value] as MV value.splice(index, 1, newValue) emit('update:modelValue', value) } diff --git a/app/src/components/globals/FormFieldReadonly.vue b/app/src/components/globals/FormFieldReadonly.vue index 6214a32e..b753f93f 100644 --- a/app/src/components/globals/FormFieldReadonly.vue +++ b/app/src/components/globals/FormFieldReadonly.vue @@ -19,10 +19,11 @@ defineOptions({ const props = withDefaults(defineProps>(), { id: undefined, - modelValue: undefined, cols: () => ({ md: 4, lg: 3 }), }) +const modelValue = defineModel() + const { t } = useI18n() const cols = computed(() => ({ @@ -32,7 +33,7 @@ const cols = computed(() => ({ })) const text = computed(() => { - return parseValue(props.modelValue) + return parseValue(modelValue.value) }) function parseValue(value: any) { diff --git a/app/src/components/globals/ViewSearch.vue b/app/src/components/globals/ViewSearch.vue index 0b7c03d4..770625df 100644 --- a/app/src/components/globals/ViewSearch.vue +++ b/app/src/components/globals/ViewSearch.vue @@ -9,11 +9,9 @@ const props = withDefaults( defineProps<{ items?: T[] | null itemsName: string | null - modelValue?: string }>(), { items: undefined, - modelValue: undefined, }, ) diff --git a/app/src/components/globals/formItems/AdressItem.vue b/app/src/components/globals/formItems/AdressItem.vue index 5e2caf72..3922a34e 100644 --- a/app/src/components/globals/formItems/AdressItem.vue +++ b/app/src/components/globals/formItems/AdressItem.vue @@ -14,7 +14,6 @@ withDefaults( touchKey: undefined, type: 'email', - modelValue: undefined, state: undefined, validation: undefined, ariaDescribedby: undefined, diff --git a/app/src/components/globals/formItems/CheckboxItem.vue b/app/src/components/globals/formItems/CheckboxItem.vue index 51d53d7a..42d469e7 100644 --- a/app/src/components/globals/formItems/CheckboxItem.vue +++ b/app/src/components/globals/formItems/CheckboxItem.vue @@ -12,23 +12,18 @@ withDefaults( labels: () => ({ true: 'yes', false: 'no' }), ariaDescribedby: undefined, - modelValue: undefined, state: undefined, validation: undefined, }, ) -defineEmits<{ - 'update:modelValue': [value: boolean] -}>() - -const model = defineModel() +const modelValue = defineModel()