Newer
Older
Sébastien DA ROCHA
committed
<template>
<div
:id="`custom-dropdown${identifier}`"
Sébastien DA ROCHA
committed
:class="[
'ui search selection dropdown',
{ 'active visible': isOpen },
{ disabled },
]"
@click="toggleDropdown"
>
<input
v-if="search"
v-on:keyup.enter="select(0)"
v-on:keyup.esc="toggleDropdown(false)"
Sébastien DA ROCHA
committed
class="search"
autocomplete="off"
tabindex="0"
Sébastien DA ROCHA
committed
/>
<div v-if="!input" class="default text">
<!-- {{ selected }} -->
<div v-if="Array.isArray(selected)">
<span v-if="selected[0]"> {{ selected[0] }} - </span>
<span class="italic">{{ selected[1] }}</span>
</div>
<div v-else>{{ selected }}</div>
<i
:class="['dropdown icon', { clear: clearable && selected }]"
@click="clear"
></i>
Sébastien DA ROCHA
committed
<div :class="['menu', { 'visible transition': isOpen }]">
<div
v-for="(option, index) in filteredOptions || ['No results found.']"
Sébastien DA ROCHA
committed
@click="select(index)"
:key="option + index"
:class="[
filteredOptions ? 'item' : 'message',
{ 'active selected': option.name === selected },
Sébastien DA ROCHA
committed
]"
<div v-if="option.name && Array.isArray(option.name)">
<span v-if="option.name[0]"> {{ option.name[0] }} - </span>
<span class="italic">{{ option.name[1] }}</span>
</div>
<span v-else-if="option.name">
{{ option.name }}
</span>
<span v-else>
Sébastien DA ROCHA
committed
</div>
</div>
</template>
<script>
export default {
name: "Dropdown",
props: [
"options",
"selected",
"disabled",
"search",
"placeholder",
"clearable",
],
Sébastien DA ROCHA
committed
computed: {
Sébastien DA ROCHA
committed
if (this.search && this.input !== "") {
options = this.options.filter(this.matchInput);
Sébastien DA ROCHA
committed
}
return options.length > 0 ? options : null;
},
placehold() {
return this.input ? "" : this.placeholder;
},
Sébastien DA ROCHA
committed
},
data() {
return {
isOpen: false,
input: "",
Sébastien DA ROCHA
committed
};
},
Sébastien DA ROCHA
committed
methods: {
this.input = ""; // * clear input field when closing dropdown
//* focus on input if is a search dropdown
} else if (this.clearable && val.target && this.selected) {
this.clear(); //* clear selected and input
this.isOpen = typeof val === "boolean" ? val : !this.isOpen;
Sébastien DA ROCHA
committed
},
Sébastien DA ROCHA
committed
select(index) {
// * toggle dropdown is called several time, timeout delay this function to be the last
Sébastien DA ROCHA
committed
setTimeout(() => {

Timothee P
committed
if (this.filteredOptions) {
this.$emit("update:selection", this.filteredOptions[index]);
}
Sébastien DA ROCHA
committed
this.input = "";
},
matchInput(el) {
let match;
if (el.name && Array.isArray(el.name)) {
match =
el.name[0].toLowerCase().includes(this.input.toLowerCase()) ||
el.name[1].toLowerCase().includes(this.input.toLowerCase());
} else {
match = el.name
? el.name.toLowerCase().includes(this.input.toLowerCase())
: el.toLowerCase().includes(this.input.toLowerCase());
}
return match;
Sébastien DA ROCHA
committed
},
clear() {
if (this.clearable) {
this.input = "";
this.$emit("update:selection", "");
if (this.isOpen) this.toggleDropdown(false);
}
},
Sébastien DA ROCHA
committed
clickOutsideDropdown(e) {
if (!e.target.closest(`#custom-dropdown${this.identifier}`))
Sébastien DA ROCHA
committed
},
},
created() {

Timothee P
committed
this.identifier = Math.floor(Math.random() * 10000);
Sébastien DA ROCHA
committed
window.addEventListener("mousedown", this.clickOutsideDropdown);
},
beforeDestroy() {
window.removeEventListener("mousedown", this.clickOutsideDropdown);
},
};
</script>
<style scoped>
.ui.selection.dropdown .menu > .item {
white-space: nowrap;
}
.italic {
font-style: italic;
}