This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AutoSuggest.vue
200 lines (196 loc) · 5.97 KB
/
AutoSuggest.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<template>
<div class="v-autocomplete">
<input :class="inputClass" type="text" v-model.trim="searchData"
@input="searchInputChange" @keydown.up="moveUp" @keydown.down="moveDown" @keyup.enter="enterPress"/>
<ul class="v-suggestion-box" v-show = "currentStatus !== status.closeStatus">
<li>
<div :is="statusComponent" :suggestionStatusEnum="this.currentStatus"></div>
</li>
<li class="v-suggestion-item" :key="index" v-for="(item ,index) in suggestionArray.slice(0, this.maxSuggestion)" @click="selectSuggestion(item)">
<div :is="suggItemComponent" :item="item" :valueProp="suggValue"></div>
</li>
</ul>
</div>
</template>
<style scoped>
li{
max-width:100%;
list-style: none;
}
.v-autocomplete {
width: 100%;
}
.v-autocomplete-input{
width: 100%;
font-weight: 100%;
}
.v-suggestion-box{
width: 100%;
padding-left: 0px;
border-style: solid;
border-width: 0.666667px;
padding: 1.33333px;
border-color: white black black black;
margin: 0 0 0 0;
}
.v-suggestion-item.active{
border: 4px solid black;
}
</style>
<script>
import suggestionItem from './SuggestionItem.vue'
import suggestionStatus from './SuggestionStatus.vue'
function debounce (func, wait, immediate) {
let timeout
return function () {
const context = this
const args = arguments
const later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
};
export default {
name: 'autoSuggest',
props: {
getItemFromAjax: {
type: Function,
required: false
},
suggStatusComp: {
type: Object,
required: false
},
suggItemComp: {
type: Object,
required: false
},
suggValue: {
type: String,
required: false,
default: 'value'
},
inputClass:{
type: String,
required: false,
default: 'v-autocomplete-input'
},
items: {
type: Array,
required: false
},
maxSuggestion: {
type: Number,
required: false,
default: 5
}
},
data () {
return {
statusComponent: this.suggStatusComp || suggestionStatus,
suggItemComponent: this.suggItemComp || suggestionItem,
searchData: '',
suggestionArray: [], // this is the array of data that will be shown to the user as suggestions
currentStatus: 3,
status: {
nuetralStatus: 0,
noDataFound: 1,
loading: 2,
closeStatus: 3
}
}
},
watch: {
searchData (value) {
this.$emit('input', value)
}
},
methods: {
searchInputChange: debounce(function (e) {
if (e.target.value !== '') {
this.currentStatus = this.status.loading
this.suggestionArray = []
this.getData()
}
// this is here because if the person is deleting the data and there's a delay in the deleting
// the top if will be trigger instead of the else if he is clearing,
// this is place here as a contingency place of sorts
if (e.target.value === '') {
this.suggestionArray = []
this.currentStatus = this.status.closeStatus
}
}, 500, false),
getData: async function () {
if (this.items) {
this.suggestionArray = this.items.reduce((accumulative, current) => {
let currVal = current;
const objKeys = this.suggValue.split('.')
for(let i = 0 ; i<objKeys.length; i++){
currVal = currVal[objKeys[i]]
}
if (currVal.trim().toLowerCase().includes(this.searchData.toLowerCase())) { accumulative.push(current) }
return accumulative
}, [])
} else {
this.suggestionArray = await this.getItemFromAjax(this.searchData)
}
if (this.suggestionArray.length < 1) {
this.currentStatus = this.status.noDataFound
} else {
this.currentStatus = this.status.nuetralStatus
}
},
selectSuggestion (item) {
let currVal = item;
const objKeys = this.suggValue.split('.')
for(let i = 0 ; i<objKeys.length; i++){
currVal = currVal[objKeys[i]]
}
this.searchData = currVal
this.suggestionArray = []
this.currentStatus = this.status.closeStatus
},
moveDown (e) {
const selectedSuggElement = this.$el.querySelector('.v-suggestion-item.active')
if (selectedSuggElement) {
if (selectedSuggElement.nextSibling) {
selectedSuggElement.nextSibling.classList.add('active')
selectedSuggElement.classList.remove('active')
} else {
selectedSuggElement.classList.remove('active')
}
} else {
const suggElement = this.$el.querySelectorAll('.v-suggestion-item')
if (suggElement.length > 0) { // this is to prevent the error messages from popping up in console to make it look ugly
suggElement[0].classList.add('active')
}
}
},
moveUp (e) {
const selectedSuggElement = this.$el.querySelector('.v-suggestion-item.active')
if (selectedSuggElement) {
if (selectedSuggElement.previousSibling.classList) { // well i dont have a good way of detecting it yet
selectedSuggElement.previousSibling.classList.add('active')
selectedSuggElement.classList.remove('active')
} else {
selectedSuggElement.classList.remove('active')
}
} else {
const suggElement = this.$el.querySelectorAll('.v-suggestion-item')
if (suggElement.length > 0) { // this is to prevent the error messages from popping up in console to make it look ugly
suggElement[suggElement.length - 1].classList.add('active')
}
}
},
enterPress () {
const selectedSuggElement = this.$el.querySelector('.v-suggestion-item.active')
selectedSuggElement.click()
}
}
}
</script>