refactor: SelectItem typing

This commit is contained in:
axolotle 2024-07-05 17:17:47 +02:00
parent 9a48aa1a45
commit ea7f070b2c
2 changed files with 35 additions and 17 deletions

View file

@ -1,36 +1,50 @@
<script setup lang="ts"> <script setup lang="ts">
import { inject } from 'vue' import { computed, inject } from 'vue'
withDefaults( import { ValidationTouchSymbol } from '@/composables/form'
defineProps<{ import type { BaseItemComputedProps, SelectItemProps } from '@/types/form'
modelValue?: string | null
id?: string const props = withDefaults(
choices: string[] defineProps<SelectItemProps & BaseItemComputedProps<string | null>>(),
required?: boolean
name?: string
}>(),
{ {
modelValue: null,
id: undefined, id: undefined,
required: false,
name: undefined, name: undefined,
placeholder: undefined,
touchKey: undefined,
ariaDescribedby: undefined,
modelValue: undefined,
state: undefined,
validation: undefined,
}, },
) )
const emit = defineEmits<{ defineEmits<{
'update:modelValue': [value: string | null] 'update:modelValue': [value: string | null]
}>() }>()
const touch = inject('touch') const model = defineModel<string | number | null>()
const touch = inject(ValidationTouchSymbol)
const required = computed(() => 'required' in (props?.validation ?? {}))
</script> </script>
<template> <template>
<BFormSelect <BFormSelect
:modelValue="modelValue"
@update:modelValue="emit('update:modelValue', $event)"
:id="id" :id="id"
v-model="model"
:name="name"
:options="choices" :options="choices"
:aria-describedby="ariaDescribedby"
:state="state"
:required="required" :required="required"
@blur="touch(name)" @blur="touch?.(touchKey)"
/> >
<template #first>
<BFormSelectOption value="null" :disabled="required">
-- {{ required ? $t('select_an_option') : $t('words.none') }} --
</BFormSelectOption>
</template>
</BFormSelect>
</template> </template>

View file

@ -72,3 +72,7 @@ export type InputItemProps = BaseWritableItemProps & {
| 'time' | 'time'
| 'url' | 'url'
} }
export type SelectItemProps = BaseWritableItemProps & {
choices: string[] | { text: string; value: string }[]
}