refactor: FormFieldReadonly ts + composition

This commit is contained in:
axolotle 2024-07-06 12:26:06 +02:00
parent 01ff371eed
commit 50fda7861d
2 changed files with 34 additions and 17 deletions

View file

@ -1,27 +1,27 @@
<script setup lang="ts">
<script
setup
lang="ts"
generic="C extends AnyWritableComponents, MV extends any"
>
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import type { Cols } from '@/types/commons'
import type {
AnyWritableComponents,
FormFieldReadonlyProps,
} from '@/types/form'
defineOptions({
name: 'FormFieldReadonly',
inheritAttrs: false,
})
const props = withDefaults(
defineProps<{
label: string
component?: string
// FIXME modelValue? not a modelValue but idk
value?: any
cols?: Cols
}>(),
{
component: 'InputItem',
value: null,
cols: () => ({ md: 4, lg: 3 }),
},
)
const props = withDefaults(defineProps<FormFieldReadonlyProps<C, MV>>(), {
id: undefined,
modelValue: undefined,
cols: () => ({ md: 4, lg: 3 }),
})
const { t } = useI18n()
@ -32,7 +32,7 @@ const cols = computed<Cols>(() => ({
}))
const text = computed(() => {
return parseValue(props.value)
return parseValue(props.modelValue)
})
function parseValue(value: any) {
@ -43,7 +43,7 @@ function parseValue(value: any) {
if (Array.isArray(value)) {
value = value.length ? value.join(t('words.separator')) : null
}
if ([null, undefined, ''].includes(props.value)) value = t('words.none')
if ([null, undefined, ''].includes(value)) value = t('words.none')
return value
}
</script>

View file

@ -5,6 +5,8 @@ import type {
} from '@vuelidate/core'
import type { RouteLocationRaw } from 'vue-router'
import type { Cols } from '@/types/commons'
type StateValidation = false | null
type StateVariant = 'success' | 'info' | 'warning' | 'danger'
@ -194,8 +196,23 @@ export type FormField<
readonly?: false
}
type FormFieldReadonly<
C extends AnyWritableComponents = AnyWritableComponents,
> = BaseFormField<C> & {
label: string
cols?: Cols
readonly: true
}
export type FormFieldProps<
C extends AnyWritableComponents,
MV extends any,
> = Omit<FormField<C, MV>, 'hr' | 'visible' | 'readonly'> &
BaseFormFieldComputedProps<MV>
export type FormFieldReadonlyProps<
C extends AnyWritableComponents,
MV extends any,
> = Omit<FormFieldReadonly<C>, 'hr' | 'visible' | 'readonly'> & {
modelValue?: MV
}