fix: selector value binding (#87)

This commit is contained in:
Hayden 2022-10-15 13:56:08 -08:00 committed by GitHub
parent bb86a51b05
commit 5596740cd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 10 deletions

View file

@ -17,7 +17,7 @@
</template>
<script lang="ts" setup>
const emit = defineEmits(["update:modelValue"]);
const emit = defineEmits(["update:modelValue", "update:value"]);
const props = defineProps({
label: {
type: String,
@ -37,26 +37,32 @@
type: String,
default: "name",
},
value: {
valueKey: {
type: String,
default: null,
required: false,
},
value: {
type: String,
default: "",
},
});
const selectedIdx = ref(-1);
const internalSelected = useVModel(props, "modelValue", emit);
watch(selectedIdx, newVal => {
internalSelected.value = props.items[newVal];
});
watch(internalSelected, newVal => {
if (props.valueKey) {
emit("update:value", newVal[props.valueKey]);
}
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function compare(a: any, b: any): boolean {
if (props.value) {
return a[props.value] === b[props.value];
}
if (a === b) {
return true;
}