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"

Timothee P
committed
v-model="input"
v-on:keyup.enter="select(0)"
@input="handelInput"
Sébastien DA ROCHA
committed
class="search"
autocomplete="off"
tabindex="0"

Timothee P
committed
:placeholder="placehold"
Sébastien DA ROCHA
committed
/>
<!-- {{placeholder}} -->

Timothee P
committed
<div v-if="!input" class="default text">{{ selected || placeholder }}</div>
Sébastien DA ROCHA
committed
<i
:class="['dropdown icon', { clear: search && selected }]"
@click="clear"
></i>
<div :class="['menu', { 'visible transition': isOpen }]">
<div
v-for="(option, index) in processedOptions || ['No results found.']"
@click="select(index)"
:key="option + index"
:class="[
processedOptions ? 'item' : 'message',
{ 'active selected': option === selected },
]"
Sébastien DA ROCHA
committed
</div>
</div>
</template>
<script>
export default {
name: "Dropdown",
props: ["options", "selected", "disabled", "search", "placeholder"],
computed: {
processedOptions: function () {
//* si un objet {name, value}
Sébastien DA ROCHA
committed
let options = this.options.map((el) =>
Sébastien DA ROCHA
committed
);
if (this.search && this.input !== "") {
options = this.searchOptions(options);
}
return options.length > 0 ? options : null;
},

Timothee P
committed
placehold() {
return this.input ? "" : this.placeholder;
},
Sébastien DA ROCHA
committed
},
data() {
return {
isOpen: false,
input: "",
Sébastien DA ROCHA
committed
};
},
methods: {
toggleDropdown() {
this.isOpen = !this.isOpen;
},

Timothee P
committed
Sébastien DA ROCHA
committed
select(index) {
setTimeout(() => {
this.isOpen = false; // * quick & dirty, car toggle dropdown est rappelé plusieurs fois aileurs, à creuser...
}, 500);
this.$emit("update:selection", this.options[index]);
this.input = "";
},

Timothee P
committed
Sébastien DA ROCHA
committed
searchOptions(options) {
return options.filter((el) =>
el.toLowerCase().includes(this.input.toLowerCase())
);
},

Timothee P
committed
Sébastien DA ROCHA
committed
clear() {
if (this.search) {
this.input = "";
this.clearSelected();
}
},

Timothee P
committed
Sébastien DA ROCHA
committed
clearSelected() {
this.$emit("update:selection", "");
},

Timothee P
committed
Sébastien DA ROCHA
committed
handelInput() {
this.isOpen = true;
this.clearSelected();
},

Timothee P
committed
Sébastien DA ROCHA
committed
clickOutsideDropdown(e) {
if (!e.target.closest(`#custom-dropdown${this.identifier}`))
this.isOpen = false;
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;
}
</style>