export default {
template: `
`,
props: {
id: String,
autocomplete: Array,
value: String,
},
data() {
return {
inputValue: this.value,
emitting: true,
};
},
watch: {
value(newValue) {
this.emitting = false;
this.inputValue = newValue;
this.$nextTick(() => (this.emitting = true));
},
inputValue(newValue) {
if (!this.emitting) return;
this.$emit("update:value", newValue);
},
},
computed: {
shadowText() {
if (!this.inputValue) return "";
const matchingOption = this.autocomplete.find((option) =>
option.toLowerCase().startsWith(this.inputValue.toLowerCase())
);
return matchingOption ? matchingOption.slice(this.inputValue.length) : "";
},
withDatalist() {
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
return isMobile && this.autocomplete && this.autocomplete.length > 0;
},
},
methods: {
updateValue() {
this.inputValue = this.value;
},
perform_autocomplete(e) {
if (this.shadowText) {
this.inputValue += this.shadowText;
e.preventDefault();
}
},
},
};