Allow static data set for autocomplete component

also header template
This commit is contained in:
Lance Edgar 2022-08-30 21:12:31 -05:00
parent 721600b12d
commit 7436d414c0

View file

@ -5,13 +5,15 @@
v-show="!selected" v-show="!selected"
v-model="autocompleteValue" v-model="autocompleteValue"
:placeholder="placeholder" :placeholder="placeholder"
field="label"
:icon="icon" :icon="icon"
:data="data" :data="autocompleteData"
@typing="getAsyncData" @typing="getAsyncData"
@select="selectionMade" @select="selectionMade"
:open-on-focus="openOnFocus"
keep-first> keep-first>
<template slot-scope="props"> <template #header>
{{ props.option.label }} <slot name="header"></slot>
</template> </template>
</b-autocomplete> </b-autocomplete>
@ -28,7 +30,18 @@ export default {
name: 'ByjoveAutocomplete', name: 'ByjoveAutocomplete',
props: { props: {
name: String, name: String,
serviceUrl: String, serviceUrl: {
type: String,
default: null,
},
data: {
type: Array,
default: null,
},
dataFilter: {
type: Function,
default: null,
},
value: String, value: String,
initialLabel: String, initialLabel: String,
placeholder: { placeholder: {
@ -39,7 +52,12 @@ export default {
type: String, type: String,
default: null, default: null,
}, },
openOnFocus: {
type: Boolean,
default: false,
},
}, },
data() { data() {
let selected = null let selected = null
if (this.value) { if (this.value) {
@ -49,12 +67,30 @@ export default {
} }
} }
return { return {
data: [], fetchedData: [],
selected: selected, selected: selected,
isFetching: false, isFetching: false,
autocompleteValue: this.value, autocompleteValue: this.value,
} }
}, },
computed: {
autocompleteData() {
return this.serviceUrl ? this.fetchedData : this.filteredData
},
filteredData() {
if (this.dataFilter) {
return this.data.filter((option) => {
return this.dataFilter(this.autocompleteValue, option)
})
} else {
return this.data
}
},
},
methods: { methods: {
clearSelection() { clearSelection() {
@ -66,6 +102,13 @@ export default {
}) })
}, },
// TODO: i am not clear yet what the best pattern is for setting
// the selected option. this does not seem complete but did the
// trick for what i needed atm..?
setSelected(option) {
this.selected = option
},
selectionMade(option) { selectionMade(option) {
this.selected = option this.selected = option
this.$emit('input', option ? option.value : null) this.$emit('input', option ? option.value : null)
@ -74,16 +117,22 @@ export default {
// TODO: buefy example uses `debounce()` here and perhaps we should too? // TODO: buefy example uses `debounce()` here and perhaps we should too?
// https://buefy.org/documentation/autocomplete // https://buefy.org/documentation/autocomplete
getAsyncData: function (entry) { getAsyncData: function (entry) {
// we only fetch async data via URL if we have one!
if (!this.serviceUrl) {
return
}
if (entry.length < 3) { if (entry.length < 3) {
this.data = [] this.fetchedData = []
return return
} }
this.isFetching = true this.isFetching = true
this.$http.get(this.serviceUrl, {params: {term: entry}}).then((response) => { this.$http.get(this.serviceUrl, {params: {term: entry}}).then((response) => {
this.data = response.data this.fetchedData = response.data
}).catch((error) => { }).catch((error) => {
this.data = [] this.fetchedData = []
throw error throw error
}).finally(() => { }).finally(() => {
this.isFetching = false this.isFetching = false