2021-10-01 01:52:32 +02:00
|
|
|
<template>
|
2021-11-10 03:30:44 +01:00
|
|
|
<div class="relative w-full" v-click-outside="clickOutsideObj">
|
2023-12-21 20:52:42 +01:00
|
|
|
<p v-if="label" class="text-sm font-semibold px-1" :class="disabled ? 'text-gray-300' : ''">{{ label }}</p>
|
2022-12-30 19:14:04 +01:00
|
|
|
<button type="button" :aria-label="longLabel" :disabled="disabled" class="relative w-full border rounded shadow-sm pl-3 pr-8 py-2 text-left sm:text-sm" :class="buttonClass" aria-haspopup="listbox" aria-expanded="true" @click.stop.prevent="clickShowMenu">
|
2021-10-01 01:52:32 +02:00
|
|
|
<span class="flex items-center">
|
2022-09-25 16:46:45 +02:00
|
|
|
<span class="block truncate font-sans" :class="{ 'font-semibold': selectedSubtext, 'text-sm': small }">{{ selectedText }}</span>
|
|
|
|
<span v-if="selectedSubtext">: </span>
|
|
|
|
<span v-if="selectedSubtext" class="font-normal block truncate font-sans text-sm text-gray-400">{{ selectedSubtext }}</span>
|
2021-10-01 01:52:32 +02:00
|
|
|
</span>
|
|
|
|
<span class="ml-3 absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none">
|
2022-11-21 14:18:10 +01:00
|
|
|
<span class="material-icons text-2xl">expand_more</span>
|
2021-10-01 01:52:32 +02:00
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<transition name="menu">
|
2023-10-29 17:28:34 +01:00
|
|
|
<ul v-show="showMenu" class="absolute z-10 -mt-px w-full bg-primary border border-black-200 shadow-lg max-h-56 rounded-md py-1 ring-1 ring-black ring-opacity-5 overflow-auto sm:text-sm" tabindex="-1" role="listbox">
|
2022-10-15 22:31:07 +02:00
|
|
|
<template v-for="item in itemsToShow">
|
2022-12-29 05:00:40 +01:00
|
|
|
<li :key="item.value" class="text-gray-100 relative py-2 cursor-pointer hover:bg-black-400" :id="'listbox-option-' + item.value" role="option" tabindex="0" @keyup.enter="clickedOption(item.value)" @click="clickedOption(item.value)">
|
2021-10-01 01:52:32 +02:00
|
|
|
<div class="flex items-center">
|
2022-09-25 16:46:45 +02:00
|
|
|
<span class="ml-3 block truncate font-sans text-sm" :class="{ 'font-semibold': item.subtext }">{{ item.text }}</span>
|
|
|
|
<span v-if="item.subtext">: </span>
|
|
|
|
<span v-if="item.subtext" class="font-normal block truncate font-sans text-sm text-gray-400">{{ item.subtext }}</span>
|
2021-10-01 01:52:32 +02:00
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</template>
|
|
|
|
</ul>
|
|
|
|
</transition>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
value: [String, Number],
|
|
|
|
label: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
items: {
|
|
|
|
type: Array,
|
|
|
|
default: () => []
|
2021-10-06 04:10:49 +02:00
|
|
|
},
|
2021-10-28 21:41:42 +02:00
|
|
|
disabled: Boolean,
|
|
|
|
small: Boolean
|
2021-10-01 01:52:32 +02:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2021-11-10 03:30:44 +01:00
|
|
|
clickOutsideObj: {
|
|
|
|
handler: this.clickedOutside,
|
|
|
|
events: ['mousedown'],
|
|
|
|
isActive: true
|
|
|
|
},
|
2021-10-01 01:52:32 +02:00
|
|
|
showMenu: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
selected: {
|
|
|
|
get() {
|
|
|
|
return this.value
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this.$emit('input', val)
|
|
|
|
}
|
|
|
|
},
|
2022-10-15 22:31:07 +02:00
|
|
|
itemsToShow() {
|
|
|
|
return this.items.map((i) => {
|
2023-12-21 20:52:42 +01:00
|
|
|
if (typeof i === 'string' || typeof i === 'number') {
|
2022-10-15 22:31:07 +02:00
|
|
|
return {
|
|
|
|
text: i,
|
|
|
|
value: i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
})
|
|
|
|
},
|
2021-10-01 01:52:32 +02:00
|
|
|
selectedItem() {
|
2022-10-15 22:31:07 +02:00
|
|
|
return this.itemsToShow.find((i) => i.value === this.selected)
|
2021-10-01 01:52:32 +02:00
|
|
|
},
|
|
|
|
selectedText() {
|
|
|
|
return this.selectedItem ? this.selectedItem.text : ''
|
2022-03-18 23:09:17 +01:00
|
|
|
},
|
2022-09-25 16:46:45 +02:00
|
|
|
selectedSubtext() {
|
|
|
|
return this.selectedItem ? this.selectedItem.subtext : ''
|
|
|
|
},
|
2022-03-18 23:09:17 +01:00
|
|
|
buttonClass() {
|
|
|
|
var classes = []
|
|
|
|
if (this.small) classes.push('h-9')
|
|
|
|
else classes.push('h-10')
|
|
|
|
|
|
|
|
if (this.disabled) classes.push('cursor-not-allowed border-gray-600 bg-primary bg-opacity-70 border-opacity-70 text-gray-400')
|
|
|
|
else classes.push('cursor-pointer border-gray-600 bg-primary text-gray-100')
|
|
|
|
|
|
|
|
return classes.join(' ')
|
2022-12-30 19:14:04 +01:00
|
|
|
},
|
|
|
|
longLabel() {
|
|
|
|
let result = ''
|
|
|
|
if (this.label) result += this.label + ': '
|
|
|
|
if (this.selectedText) result += this.selectedText
|
|
|
|
if (this.selectedSubtext) result += ' ' + this.selectedSubtext
|
|
|
|
return result
|
2021-10-01 01:52:32 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2021-10-06 04:10:49 +02:00
|
|
|
clickShowMenu() {
|
|
|
|
if (this.disabled) return
|
|
|
|
this.showMenu = !this.showMenu
|
|
|
|
},
|
2021-11-10 03:30:44 +01:00
|
|
|
clickedOutside() {
|
2021-10-01 01:52:32 +02:00
|
|
|
this.showMenu = false
|
|
|
|
},
|
|
|
|
clickedOption(itemValue) {
|
|
|
|
this.selected = itemValue
|
|
|
|
this.showMenu = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {}
|
|
|
|
}
|
|
|
|
</script>
|