2021-08-22 15:52:37 +02:00
|
|
|
<template>
|
2022-02-17 21:33:12 +01:00
|
|
|
<div class="w-full">
|
|
|
|
<p class="px-1 text-sm font-semibold" :class="disabled ? 'text-gray-400' : ''">{{ label }}</p>
|
2021-08-22 15:52:37 +02:00
|
|
|
<div ref="wrapper" class="relative">
|
|
|
|
<form @submit.prevent="submitForm">
|
2022-02-17 21:33:12 +01:00
|
|
|
<div ref="inputWrapper" class="input-wrapper flex-wrap relative w-full shadow-sm flex items-center border border-gray-600 rounded px-2 py-2" :class="disabled ? 'pointer-events-none bg-black-300 text-gray-400' : 'bg-primary'">
|
2021-09-06 01:20:29 +02:00
|
|
|
<input ref="input" v-model="textInput" :disabled="disabled" :readonly="!editable" class="h-full w-full bg-transparent focus:outline-none px-1" @keydown="keydownInput" @focus="inputFocus" @blur="inputBlur" />
|
2021-08-22 15:52:37 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
2021-08-25 03:24:40 +02:00
|
|
|
<ul ref="menu" v-show="isFocused && items.length && (itemsToShow.length || currentSearch)" class="absolute z-50 mt-0 w-full bg-bg border border-black-200 shadow-lg max-h-56 rounded py-1 text-base ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm" role="listbox" aria-labelledby="listbox-label">
|
2021-08-22 15:52:37 +02:00
|
|
|
<template v-for="item in itemsToShow">
|
|
|
|
<li :key="item" class="text-gray-50 select-none relative py-2 pr-3 cursor-pointer hover:bg-black-400" role="option" @click="clickedOption($event, item)" @mouseup.stop.prevent @mousedown.prevent>
|
|
|
|
<div class="flex items-center">
|
|
|
|
<span class="font-normal ml-3 block truncate">{{ item }}</span>
|
|
|
|
</div>
|
|
|
|
<span v-if="input === item" class="text-yellow-400 absolute inset-y-0 right-0 flex items-center pr-4">
|
|
|
|
<span class="material-icons text-xl">checkmark</span>
|
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
</template>
|
|
|
|
<li v-if="!itemsToShow.length" class="text-gray-50 select-none relative py-2 pr-9" role="option">
|
|
|
|
<div class="flex items-center justify-center">
|
|
|
|
<span class="font-normal">No items</span>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
value: [String, Number],
|
2021-09-06 01:20:29 +02:00
|
|
|
disabled: Boolean,
|
2021-08-22 15:52:37 +02:00
|
|
|
label: String,
|
|
|
|
items: {
|
|
|
|
type: Array,
|
|
|
|
default: () => []
|
2021-08-27 14:01:47 +02:00
|
|
|
},
|
|
|
|
editable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
2021-08-22 15:52:37 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isFocused: false,
|
|
|
|
currentSearch: null,
|
|
|
|
typingTimeout: null,
|
|
|
|
textInput: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value: {
|
|
|
|
immediate: true,
|
|
|
|
handler(newVal) {
|
|
|
|
this.textInput = newVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
input: {
|
|
|
|
get() {
|
|
|
|
return this.value
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this.$emit('input', val)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
itemsToShow() {
|
|
|
|
if (!this.currentSearch || !this.textInput || this.textInput === this.input) {
|
|
|
|
return this.items
|
|
|
|
}
|
|
|
|
return this.items.filter((i) => {
|
|
|
|
var iValue = String(i).toLowerCase()
|
|
|
|
return iValue.includes(this.currentSearch.toLowerCase())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
keydownInput() {
|
|
|
|
clearTimeout(this.typingTimeout)
|
|
|
|
this.typingTimeout = setTimeout(() => {
|
|
|
|
this.currentSearch = this.textInput
|
|
|
|
}, 100)
|
|
|
|
},
|
|
|
|
inputFocus() {
|
|
|
|
this.isFocused = true
|
|
|
|
},
|
2021-12-02 02:07:03 +01:00
|
|
|
blur() {
|
|
|
|
// Handle blur immediately
|
|
|
|
this.isFocused = false
|
|
|
|
if (this.input !== this.textInput) {
|
|
|
|
var val = this.textInput ? this.textInput.trim() : null
|
|
|
|
this.input = val
|
|
|
|
if (val && !this.items.includes(val)) {
|
|
|
|
this.$emit('newItem', val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.$refs.input) {
|
|
|
|
this.$refs.input.blur()
|
|
|
|
}
|
|
|
|
},
|
2021-08-22 15:52:37 +02:00
|
|
|
inputBlur() {
|
2021-12-02 02:07:03 +01:00
|
|
|
if (!this.isFocused) return
|
|
|
|
|
2021-08-22 15:52:37 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
if (document.activeElement === this.$refs.input) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.isFocused = false
|
|
|
|
if (this.input !== this.textInput) {
|
2021-09-16 15:37:09 +02:00
|
|
|
var val = this.textInput ? this.textInput.trim() : null
|
2021-08-27 01:32:05 +02:00
|
|
|
this.input = val
|
|
|
|
if (val && !this.items.includes(val)) {
|
|
|
|
this.$emit('newItem', val)
|
|
|
|
}
|
2021-08-22 15:52:37 +02:00
|
|
|
}
|
|
|
|
}, 50)
|
|
|
|
},
|
|
|
|
submitForm() {
|
2021-09-16 15:37:09 +02:00
|
|
|
var val = this.textInput ? this.textInput.trim() : null
|
2021-08-27 01:32:05 +02:00
|
|
|
this.input = val
|
|
|
|
if (val && !this.items.includes(val)) {
|
|
|
|
this.$emit('newItem', val)
|
|
|
|
}
|
2021-08-22 15:52:37 +02:00
|
|
|
this.currentSearch = null
|
|
|
|
},
|
|
|
|
clickedOption(e, item) {
|
|
|
|
this.textInput = null
|
|
|
|
this.currentSearch = null
|
2021-09-24 14:32:38 +02:00
|
|
|
this.input = item
|
2021-08-22 15:52:37 +02:00
|
|
|
if (this.$refs.input) this.$refs.input.blur()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {}
|
|
|
|
}
|
2021-09-06 01:20:29 +02:00
|
|
|
</script>
|