Skip to content

Instantly share code, notes, and snippets.

@hitochan777
Last active February 20, 2024 04:21
Show Gist options
  • Save hitochan777/a2735b499ce5c4fc8905b151eb880b71 to your computer and use it in GitHub Desktop.
Save hitochan777/a2735b499ce5c4fc8905b151eb880b71 to your computer and use it in GitHub Desktop.
[Vuetify] VSelect with empty selection text when no matching item
<template>
<v-select
:items="items"
:model-value="isValid ? modelValue : null"
:item-value="itemValue"
v-bind="$attrs"
>
<template #[name]="vars" v-for="_, name in $slots">
<slot :name="name" v-bind="vars" />
</template>
</v-select>
</template>
<script setup lang="ts">
import {computed} from "vue"
interface Props {
items: any,
modelValue: any,
itemValue?: string
}
const props = withDefaults(defineProps<Props>(), {
itemValue: "value",
fallbackTitle: ""
})
const values = computed(() => {
return new Set(props.items.map((item) => {
if (typeof item === "object") {
return item[props.itemValue]
}
else {
return item
}
}))
})
const isValid = computed(() => {
if (Array.isArray(props.modelValue)) {
return true;
}
return values.value.has(props.modelValue)
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment