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

View file

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