Last active
February 20, 2024 04:21
-
-
Save hitochan777/a2735b499ce5c4fc8905b151eb880b71 to your computer and use it in GitHub Desktop.
[Vuetify] VSelect with empty selection text when no matching item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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