Autocomplete is now its own component.

This commit is contained in:
Fabio Manganiello 2023-10-10 18:42:41 +02:00
parent f6bf9438b9
commit 923eb7cadb
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
5 changed files with 248 additions and 161 deletions

View file

@ -1,128 +0,0 @@
function autocomplete(inp, arr, listener) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
let currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function() {
let a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) {
return false;
}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() === val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*trigger event listener if any:*/
if (listener) {
listener(e, inp.value);
}
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keyup", function(e) {
if (["ArrowUp", "ArrowDown", "Tab", "Enter"].indexOf(e.key) >= 0) {
e.stopPropagation();
}
if (e.key === "Enter") {
this.blur();
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
let x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.key === 'ArrowDown' || (e.key === 'Tab' && !e.shiftKey)) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
e.preventDefault();
} else if (e.key === 'ArrowUp' || (e.key === 'Tab' && e.shiftKey)) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
e.preventDefault();
} else if (e.key === 'Enter') {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
if (currentFocus > -1 && x && x.length) {
e.preventDefault();
/*and simulate a click on the "active" item:*/
x[currentFocus].click();
/*and restore the focus on the input element:*/
this.focus();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (let i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
const x = document.getElementsByClassName("autocomplete-items");
for (let i = 0; i < x.length; i++) {
if (elmnt !== x[i] && elmnt !== inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
export default autocomplete;

View file

@ -0,0 +1,247 @@
<template>
<div class="autocomplete">
<label :text="label">
<input
type="text"
class="input"
ref="input"
:placeholder="placeholder"
:disabled="disabled"
:value="value"
@focus="onFocus"
@input="onInput"
@blur="onInput"
@keydown="onInputKeyDown"
@keyup="onInputKeyUp"
>
</label>
<div class="items" v-if="showItems">
<div
class="item"
:class="{ active: i === curIndex }"
:key="item"
:data-item="item"
v-for="(item, i) in visibleItems"
@click="onItemSelect(item)"
>
<span class="matching">{{ item.substr(0, value.length) }}</span>
<span class="normal">{{ item.substr(value.length) }}</span>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Autocomplete",
emits: ["input"],
props: {
items: {
type: Array,
required: true,
},
value: {
type: String,
default: "",
},
disabled: {
type: Boolean,
default: false,
},
label: {
type: String,
},
placeholder: {
type: String,
},
showResultsWhenBlank: {
type: Boolean,
default: false,
},
},
data() {
return {
visible: false,
curIndex: -1,
}
},
computed: {
visibleItems() {
const val = this.value.toUpperCase()
if (!val?.length)
return this.showResultsWhenBlank ? this.items : []
return this.items.filter(
(item) => item.substr(0, val.length).toUpperCase() === val
)
},
showItems() {
return this.visible && this.items?.length
},
},
methods: {
selectNextItem() {
this.curIndex++
this.normalizeIndex()
},
selectPrevItem() {
this.curIndex--
this.normalizeIndex()
},
normalizeIndex() {
// Go to the beginning after reaching the end
if (this.curIndex >= this.visibleItems.length)
this.curIndex = 0
// Go to the end after moving back from the start
if (this.curIndex < 0)
this.curIndex = this.visibleItems.length - 1
// Scroll to the element
const el = this.$el.querySelector("[data-item='" + this.visibleItems[this.curIndex] + "']")
if (el)
el.scrollIntoView({
block: "start",
inline: "nearest",
behavior: "smooth",
})
},
valueIsInItems() {
return this.items.indexOf(this.value) >= 0
},
onFocus() {
if (this.showResultsWhenBlank || this.value?.length)
this.visible = true
},
onInput(e) {
let val = e.target.value
if (this.valueIsInItems())
this.visible = false
e.stopPropagation()
this.$emit("input", val)
this.curIndex = -1
this.visible = true
},
onItemSelect(item) {
this.$emit("input", item)
this.$nextTick(() => {
if (this.valueIsInItems()) {
this.visible = false
}
})
},
onInputKeyUp(e) {
if (["ArrowUp", "ArrowDown", "Tab", "Enter", "Escape"].indexOf(e.key) >= 0)
e.stopPropagation()
if (e.key === "Enter" && this.valueIsInItems()) {
this.$refs.input.blur()
this.visible = false
}
},
onInputKeyDown(e) {
if (
e.key === 'ArrowDown' ||
(e.key === 'Tab' && !e.shiftKey) ||
(e.key === 'j' && e.ctrlKey)
) {
this.selectNextItem()
e.preventDefault()
} else if (
e.key === 'ArrowUp' ||
(e.key === 'Tab' && e.shiftKey) ||
(e.key === 'k' && e.ctrlKey)
) {
this.selectPrevItem()
e.preventDefault()
} else if (e.key === 'Enter') {
if (this.curIndex > -1 && this.visible) {
e.preventDefault()
this.onItemSelect(this.visibleItems[this.curIndex])
this.$refs.input.focus()
}
} else if (e.key === 'Escape') {
this.visible = false
}
},
onDocumentClick(e) {
if (this.$el.contains(e.target) || e.target.classList.contains("item"))
return
this.visible = false
},
},
mounted() {
document.addEventListener("click", this.onDocumentClick)
},
}
</script>
<style lang="scss" scoped>
.autocomplete {
width: 100%;
position: relative;
display: inline-block;
.input {
width: 100%;
box-shadow: $search-bar-shadow;
}
.items {
width: 100%;
max-height: 50vh;
position: absolute;
overflow: auto;
border: $default-border-2;
border-bottom: none;
border-top: none;
border-radius: 1em;
box-shadow: $search-bar-shadow;
z-index: 99;
top: 100%;
left: 0;
right: 0;
.item {
padding: 1em;
cursor: pointer;
border-bottom: $default-border-2;
background-color: $background-color;
&:hover {
background-color: $hover-bg-2;
}
.matching {
font-weight: bold;
}
}
}
.active {
background-color: $hover-bg-2 !important;
}
}
</style>

View file

@ -2,7 +2,6 @@ $section-shadow: 0 3px 3px 0 rgba(187,187,187,0.75), 0 3px 3px 0 rgba(187,187,18
$title-bg: #eee;
$title-border: 1px solid #ddd;
$title-shadow: 0 3px 3px 0 rgba(187,187,187,0.75);
$action-name-shadow: 1px 1px 1px 1px #ddd;
$extra-params-btn-bg: #eee;
$response-bg: linear-gradient(#dbffe5, #d5ecdc);
$error-bg: linear-gradient(#ffd9d9, #e6cbcb);

View file

@ -1,32 +0,0 @@
.autocomplete {
/*the container must be positioned relative:*/
position: relative;
display: inline-block;
}
.autocomplete-items {
position: absolute;
border: $default-border-2;
border-bottom: none;
border-top: none;
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
top: 100%;
left: 0;
right: 0;
div {
padding: 1em;
cursor: pointer;
border-bottom: $default-border-2;
background-color: $background-color;
&:hover {
background-color: $hover-bg-2;
}
}
}
.autocomplete-active {
background-color: $hover-bg-2 !important;
}

View file

@ -62,6 +62,7 @@ $border-shadow-bottom-right: 2.5px 2.5px 3px 0 $default-shadow-color;
$header-shadow: 0px 1px 3px 1px #bbb !default;
$group-shadow: 3px -2px 6px 1px #98b0a0;
$primary-btn-shadow: 1px 1px 0.5px 0.75px #32b64640 !default;
$search-bar-shadow: 1px 1px 1px 1px #ddd !default;
//// Modals
$modal-header-bg: #e0e0e0 !default;