api-client/components/ui/autocomplete.vue

218 lines
4.9 KiB
Vue
Raw Normal View History

<template>
2019-09-04 02:48:24 +00:00
<div class="autocomplete-wrapper">
2019-10-05 09:27:24 +00:00
<input
type="text"
:placeholder="placeholder"
v-model="text"
2019-10-05 09:27:24 +00:00
@input="updateSuggestions"
@keyup="updateSuggestions"
@click="updateSuggestions"
@keydown="handleKeystroke"
ref="acInput"
:spellcheck="spellcheck"
:autocapitalize="spellcheck"
:autocorrect="spellcheck"
/>
<ul
class="suggestions"
v-if="suggestions.length > 0 && suggestionsVisible"
:style="{ transform: `translate(${suggestionsOffsetLeft}px, 0)` }"
>
<li
v-for="(suggestion, index) in suggestions"
@click.prevent="forceSuggestion(suggestion)"
:class="{ active: currentSuggestionIndex === index }"
:key="index"
2019-11-12 04:52:50 +00:00
>
{{ suggestion }}
</li>
2019-10-05 09:27:24 +00:00
</ul>
2019-09-04 02:48:24 +00:00
</div>
</template>
2019-12-06 01:41:38 +00:00
<style scoped lang="scss">
2019-11-02 05:32:21 +00:00
.autocomplete-wrapper {
position: relative;
2019-09-04 02:48:24 +00:00
2019-11-02 05:32:21 +00:00
input:focus + ul.suggestions,
ul.suggestions:hover {
display: block;
}
ul.suggestions {
display: none;
background-color: var(--atc-color);
position: absolute;
top: calc(100% - 4px);
margin: 0 4px;
left: 0;
padding: 0;
2019-11-13 23:30:44 +00:00
border-radius: 0 0 8px 8px;
2019-11-02 05:32:21 +00:00
z-index: 9999;
transition: transform 0.2s ease-out;
box-shadow: 0 5px 30px rgba(black, 0.1);
2019-11-02 05:32:21 +00:00
li {
width: 100%;
2019-09-04 02:48:24 +00:00
display: block;
2019-11-02 05:32:21 +00:00
padding: 8px 16px;
2019-11-21 01:25:37 +00:00
font-size: 16px;
font-family: "Roboto Mono", monospace;
2020-01-10 00:00:38 +00:00
font-weight: 400;
2019-09-04 02:48:24 +00:00
2019-11-02 05:32:21 +00:00
&:last-child {
2019-11-13 23:30:44 +00:00
border-radius: 0 0 8px 8px;
2019-11-02 05:32:21 +00:00
}
&:hover,
&.active {
background-color: var(--ac-color);
color: var(--act-color);
cursor: pointer;
2019-09-04 02:48:24 +00:00
}
}
}
2019-11-02 05:32:21 +00:00
}
</style>
<script>
2020-02-24 18:44:50 +00:00
const KEY_TAB = 9
const KEY_ESC = 27
2019-11-02 05:32:21 +00:00
2020-02-24 18:44:50 +00:00
const KEY_ARROW_UP = 38
const KEY_ARROW_DOWN = 40
2019-11-02 05:32:21 +00:00
export default {
props: {
spellcheck: {
type: Boolean,
default: true,
2020-02-24 18:44:50 +00:00
required: false,
2019-09-04 02:48:24 +00:00
},
2019-11-02 05:32:21 +00:00
placeholder: {
type: String,
default: "",
2020-02-24 18:44:50 +00:00
required: false,
2019-09-04 02:48:24 +00:00
},
2019-11-02 05:32:21 +00:00
source: {
type: Array,
2020-02-24 18:44:50 +00:00
required: true,
},
value: {
type: String,
default: "",
2020-02-24 18:44:50 +00:00
required: false,
},
2019-11-02 05:32:21 +00:00
},
2019-11-02 05:32:21 +00:00
watch: {
text() {
this.$emit("input", this.text)
2020-02-24 18:44:50 +00:00
},
2019-11-02 05:32:21 +00:00
},
data() {
return {
text: this.value,
2019-11-02 05:32:21 +00:00
selectionStart: 0,
suggestionsOffsetLeft: 0,
currentSuggestionIndex: -1,
2020-02-24 18:44:50 +00:00
suggestionsVisible: false,
}
2019-11-02 05:32:21 +00:00
},
methods: {
updateSuggestions(event) {
// Hide suggestions if ESC pressed.
if (event.which && event.which === KEY_ESC) {
2020-02-24 18:44:50 +00:00
event.preventDefault()
this.suggestionsVisible = false
this.currentSuggestionIndex = -1
return
2019-09-04 02:48:24 +00:00
}
2019-11-02 05:32:21 +00:00
// As suggestions is a reactive property, this implicitly
// causes suggestions to update.
2020-02-24 18:44:50 +00:00
this.selectionStart = this.$refs.acInput.selectionStart
this.suggestionsOffsetLeft = 12 * this.selectionStart
this.suggestionsVisible = true
2019-09-04 02:48:24 +00:00
},
2019-11-02 05:32:21 +00:00
forceSuggestion(text) {
2020-02-24 18:44:50 +00:00
let input = this.text.substring(0, this.selectionStart)
this.text = input + text
2019-11-02 05:32:21 +00:00
2020-02-24 18:44:50 +00:00
this.selectionStart = this.text.length
this.suggestionsVisible = true
this.currentSuggestionIndex = -1
},
2019-09-04 02:48:24 +00:00
2019-11-02 05:32:21 +00:00
handleKeystroke(event) {
switch (event.which) {
case KEY_ARROW_UP:
2020-02-24 18:44:50 +00:00
event.preventDefault()
2019-11-02 05:32:21 +00:00
this.currentSuggestionIndex =
2020-02-24 18:44:50 +00:00
this.currentSuggestionIndex - 1 >= 0 ? this.currentSuggestionIndex - 1 : 0
break
2019-11-02 05:32:21 +00:00
case KEY_ARROW_DOWN:
2020-02-24 18:44:50 +00:00
event.preventDefault()
2019-11-02 05:32:21 +00:00
this.currentSuggestionIndex =
this.currentSuggestionIndex < this.suggestions.length - 1
? this.currentSuggestionIndex + 1
2020-02-24 18:44:50 +00:00
: this.suggestions.length - 1
break
2019-11-02 05:32:21 +00:00
case KEY_TAB:
2020-02-24 18:44:50 +00:00
event.preventDefault()
2019-11-02 05:32:21 +00:00
let activeSuggestion = this.suggestions[
this.currentSuggestionIndex >= 0 ? this.currentSuggestionIndex : 0
2020-02-24 18:44:50 +00:00
]
2019-11-02 05:32:21 +00:00
if (activeSuggestion) {
2020-02-24 18:44:50 +00:00
let input = this.text.substring(0, this.selectionStart)
this.text = input + activeSuggestion
2019-11-02 05:32:21 +00:00
}
2020-02-24 18:44:50 +00:00
break
2019-11-02 05:32:21 +00:00
default:
2020-02-24 18:44:50 +00:00
break
2019-11-02 05:32:21 +00:00
}
2020-02-24 18:44:50 +00:00
},
2019-11-02 05:32:21 +00:00
},
computed: {
/**
* Gets the suggestions list to be displayed under the input box.
*
* @returns {default.props.source|{type, required}}
*/
suggestions() {
2020-02-24 18:44:50 +00:00
let input = this.text.substring(0, this.selectionStart)
2019-11-02 05:32:21 +00:00
return (
this.source
2020-06-19 06:56:04 +00:00
.filter((entry) => {
2019-11-02 05:32:21 +00:00
return (
entry.toLowerCase().startsWith(input.toLowerCase()) &&
input.toLowerCase() !== entry.toLowerCase()
2020-02-24 18:44:50 +00:00
)
2019-11-02 05:32:21 +00:00
})
// Cut off the part that's already been typed.
2020-06-19 06:56:04 +00:00
.map((entry) => entry.substring(this.selectionStart))
2019-11-13 23:55:12 +00:00
// We only want the top 6 suggestions.
2019-11-13 23:30:44 +00:00
.slice(0, 6)
2020-02-24 18:44:50 +00:00
)
},
2019-11-02 05:32:21 +00:00
},
mounted() {
this.updateSuggestions({
2020-02-24 18:44:50 +00:00
target: this.$refs.acInput,
})
},
}
</script>