|
| 1 | +<template> |
| 2 | + <span> |
| 3 | + <slot> |
| 4 | + <input |
| 5 | + type="text" |
| 6 | + v-model="model" |
| 7 | + /> |
| 8 | + </slot> |
| 9 | + <q-popover ref="popover" :anchorClick="false"> |
| 10 | + <div v-if="searching" class="row justify-center" :style="{width: width, padding: '3px 10px'}"> |
| 11 | + <spinner name="dots" :size="40"></spinner> |
| 12 | + </div> |
| 13 | + <div v-else class="list no-border" :style="computedWidth"> |
| 14 | + <div |
| 15 | + class="item item-link" |
| 16 | + v-for="(result, index) in computedResults" |
| 17 | + @click="setValue(result.value)" |
| 18 | + :class="{active: selectedIndex === index, 'two-lines': result.secondLabel}" |
| 19 | + > |
| 20 | + <i v-if="result.icon" class="item-primary">{{result.icon}}</i> |
| 21 | + <img v-if="result.img" class="item-primary thumbnail" :src="result.img" /> |
| 22 | + |
| 23 | + <div class="item-content" :class="{'has-secondary': result.secondIcon || result.secondImg || result.stamp}"> |
| 24 | + <div v-html="result.label"></div> |
| 25 | + <div v-if="result.secondLabel" v-html="result.secondLabel"></div> |
| 26 | + </div> |
| 27 | + |
| 28 | + <div v-if="result.stamp" class="item-secondary stamp" v-html="result.stamp"></div> |
| 29 | + <i v-if="result.secondIcon" class="item-secondary">{{result.secondIcon}}</i> |
| 30 | + <img v-if="result.secondImg" class="item-secondary thumbnail" :src="result.secondImg" /> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + </q-popover> |
| 34 | + </span> |
| 35 | +</template> |
| 36 | + |
| 37 | +<script> |
| 38 | +import Utils from '../../utils' |
| 39 | +
|
| 40 | +export default { |
| 41 | + props: { |
| 42 | + value: { |
| 43 | + type: String, |
| 44 | + required: true |
| 45 | + }, |
| 46 | + minCharacters: { |
| 47 | + type: Number, |
| 48 | + default: 1 |
| 49 | + }, |
| 50 | + resultsLimit: { |
| 51 | + type: Number, |
| 52 | + default: 6 |
| 53 | + }, |
| 54 | + delay: { |
| 55 | + type: Number, |
| 56 | + default: 500 |
| 57 | + }, |
| 58 | + setWidth: Boolean |
| 59 | + }, |
| 60 | + data () { |
| 61 | + return { |
| 62 | + searchId: '', |
| 63 | + results: [], |
| 64 | + selectedIndex: -1, |
| 65 | + width: 0 |
| 66 | + } |
| 67 | + }, |
| 68 | + watch: { |
| 69 | + delay (val) { |
| 70 | + this.__updateDelay(val) |
| 71 | + } |
| 72 | + }, |
| 73 | + computed: { |
| 74 | + model: { |
| 75 | + get () { |
| 76 | + return this.value |
| 77 | + }, |
| 78 | + set (val) { |
| 79 | + this.$emit('input', val) |
| 80 | + } |
| 81 | + }, |
| 82 | + computedResults () { |
| 83 | + if (this.resultsLimit && this.results.length > 0) { |
| 84 | + return this.results.slice(0, this.resultsLimit) |
| 85 | + } |
| 86 | + }, |
| 87 | + computedWidth () { |
| 88 | + if (this.setWidth) { |
| 89 | + return {width: this.width} |
| 90 | + } |
| 91 | + }, |
| 92 | + searching () { |
| 93 | + return this.searchId.length > 0 |
| 94 | + } |
| 95 | + }, |
| 96 | + methods: { |
| 97 | + trigger () { |
| 98 | + this.width = Utils.dom.width(this.inputEl) + 'px' |
| 99 | + this.$nextTick(() => { |
| 100 | + const searchId = Utils.uid() |
| 101 | + this.searchId = searchId |
| 102 | + if (this.model.length < this.minCharacters) { |
| 103 | + this.searchId = '' |
| 104 | + this.close() |
| 105 | + return |
| 106 | + } |
| 107 | +
|
| 108 | + this.$refs.popover.open() |
| 109 | + this.$emit('search', this.model, results => { |
| 110 | + if (results && this.searchId === searchId) { |
| 111 | + this.searchId = '' |
| 112 | + this.results = results |
| 113 | + } |
| 114 | + }) |
| 115 | + }) |
| 116 | + }, |
| 117 | + close () { |
| 118 | + this.$refs.popover.close() |
| 119 | + this.results = [] |
| 120 | + this.selectedIndex = -1 |
| 121 | + }, |
| 122 | + setValue (val) { |
| 123 | + this.model = val |
| 124 | + this.close() |
| 125 | + }, |
| 126 | + move (offset) { |
| 127 | + this.selectedIndex = Math.max(-1, Math.min(this.computedResults.length - 1, this.selectedIndex + offset)) |
| 128 | + }, |
| 129 | + setCurrentSelection () { |
| 130 | + if (this.selectedIndex >= 0) { |
| 131 | + this.setValue(this.results[this.selectedIndex].value) |
| 132 | + } |
| 133 | + }, |
| 134 | + __updateDelay () { |
| 135 | + this.inputEl.removeEventListener('input', this.delayedTrigger) |
| 136 | + this.delayedTrigger = this.delay ? Utils.debounce(this.trigger, this.delay) : this.trigger |
| 137 | + this.inputEl.addEventListener('input', this.delayedTrigger) |
| 138 | + }, |
| 139 | + handleKeydown (e) { |
| 140 | + switch (e.keyCode || e.which) { |
| 141 | + case 38: // up |
| 142 | + this.move(-1) |
| 143 | + break |
| 144 | + case 40: // down |
| 145 | + if (!this.$refs.popover.opened) { |
| 146 | + this.trigger() |
| 147 | + } |
| 148 | + else { |
| 149 | + this.move(1) |
| 150 | + } |
| 151 | + break |
| 152 | + case 13: // enter |
| 153 | + this.setCurrentSelection() |
| 154 | + break |
| 155 | + } |
| 156 | + } |
| 157 | + }, |
| 158 | + mounted () { |
| 159 | + this.$nextTick(() => { |
| 160 | + this.inputEl = this.$el.querySelector('input') |
| 161 | + if (!this.inputEl) { |
| 162 | + console.error('Autocomplete needs to contain one input field in its slot.') |
| 163 | + return |
| 164 | + } |
| 165 | + this.__updateDelay() |
| 166 | + this.inputEl.addEventListener('keydown', this.handleKeydown) |
| 167 | + }) |
| 168 | + }, |
| 169 | + beforeDestroy () { |
| 170 | + this.close() |
| 171 | + this.inputEl.removeEventListener('input', this.delayedTrigger) |
| 172 | + this.inputEl.removeEventListener('keydown', this.handleKeydown) |
| 173 | + } |
| 174 | +} |
| 175 | +</script> |
0 commit comments