mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-29 03:10:42 +01:00
fix(SelectMenu): take option-attribute into account to display label
Resolves #1151
This commit is contained in:
@@ -13,7 +13,7 @@ const people = [{
|
|||||||
name: 'Tom Cook'
|
name: 'Tom Cook'
|
||||||
}]
|
}]
|
||||||
|
|
||||||
const selected = ref(people[0].name)
|
const selected = ref(people[0].id)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -21,7 +21,7 @@ const selected = ref(people[0].name)
|
|||||||
v-model="selected"
|
v-model="selected"
|
||||||
:options="people"
|
:options="people"
|
||||||
placeholder="Select people"
|
placeholder="Select people"
|
||||||
value-attribute="name"
|
value-attribute="id"
|
||||||
option-attribute="name"
|
option-attribute="name"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -36,8 +36,7 @@
|
|||||||
</span>
|
</span>
|
||||||
|
|
||||||
<slot name="label">
|
<slot name="label">
|
||||||
<span v-if="multiple && Array.isArray(modelValue) && modelValue.length" :class="uiMenu.label">{{ modelValue.length }} selected</span>
|
<span v-if="label" :class="uiMenu.label">{{ label }}</span>
|
||||||
<span v-else-if="!multiple && modelValue" :class="uiMenu.label">{{ ['string', 'number'].includes(typeof modelValue) ? modelValue : modelValue[optionAttribute] }}</span>
|
|
||||||
<span v-else :class="uiMenu.label">{{ placeholder || ' ' }}</span>
|
<span v-else :class="uiMenu.label">{{ placeholder || ' ' }}</span>
|
||||||
</slot>
|
</slot>
|
||||||
|
|
||||||
@@ -355,6 +354,23 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const label = computed(() => {
|
||||||
|
if (props.multiple) {
|
||||||
|
if (Array.isArray(props.modelValue) && props.modelValue.length) {
|
||||||
|
return `${props.modelValue.length} selected`
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (props.valueAttribute) {
|
||||||
|
const option = props.options.find(option => option[props.valueAttribute] === props.modelValue)
|
||||||
|
return option ? option[props.optionAttribute] : null
|
||||||
|
} else {
|
||||||
|
return ['string', 'number'].includes(typeof props.modelValue) ? props.modelValue : props.modelValue[props.optionAttribute]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const selectClass = computed(() => {
|
const selectClass = computed(() => {
|
||||||
const variant = ui.value.color?.[color.value as string]?.[props.variant as string] || ui.value.variant[props.variant]
|
const variant = ui.value.color?.[color.value as string]?.[props.variant as string] || ui.value.variant[props.variant]
|
||||||
|
|
||||||
@@ -509,6 +525,7 @@ export default defineComponent({
|
|||||||
popper,
|
popper,
|
||||||
trigger,
|
trigger,
|
||||||
container,
|
container,
|
||||||
|
label,
|
||||||
isLeading,
|
isLeading,
|
||||||
isTrailing,
|
isTrailing,
|
||||||
// eslint-disable-next-line vue/no-dupe-keys
|
// eslint-disable-next-line vue/no-dupe-keys
|
||||||
|
|||||||
Reference in New Issue
Block a user