added autofill for searchbar

This commit is contained in:
Imperial-Dragon 2020-12-11 23:44:11 +05:30
parent 0df235dc7e
commit 30cfc4c1a3
3 changed files with 88 additions and 11 deletions

View File

@ -82,3 +82,26 @@
.forceTextColor .inputAction:active { .forceTextColor .inputAction:active {
background-color: var(--primary-color-active); background-color: var(--primary-color-active);
} }
.list {
position: absolute;
width: 100%;
list-style: none;
margin: 0;
padding: 5px 0;
border-radius: 0 0 5px 5px;
border: 1px #ccc solid;
background-color: white;
color: black;
}
.list li {
display: block;
padding: 0px 15px;
line-height: 2rem;
}
.hover {
background-color: #ccc;
/* color: white; */
}

View File

@ -43,7 +43,12 @@ export default Vue.extend({
data: function () { data: function () {
return { return {
id: '', id: '',
inputData: '' inputData: '',
searchState: {
showOptions: false,
selectedOption: -1,
isPointerInList: false
}
} }
}, },
computed: { computed: {
@ -72,10 +77,15 @@ export default Vue.extend({
}, },
methods: { methods: {
handleClick: function () { handleClick: function () {
this.searchState.showOptions = false
this.$emit('input', this.inputData)
this.$emit('click', this.inputData) this.$emit('click', this.inputData)
}, },
handleInput: function () { handleInput: function () {
if (this.isSearch &&
this.searchState.selectedOption !== -1 &&
this.inputData === this.dataList[this.searchState.selectedOption]) { return }
this.$emit('input', this.inputData) this.$emit('input', this.inputData)
}, },
@ -89,6 +99,37 @@ export default Vue.extend({
} }
}) })
} }
},
handleOptionClick: function (index) {
this.searchState.showOptions = false
this.inputData = this.dataList[index]
this.$emit('input', this.inputData)
this.handleClick()
},
handleKeyDown: function (keyCode) {
if (this.dataList.length === 0) { return }
// Update selectedOption based on arrow key pressed
if (keyCode === 40) {
this.searchState.selectedOption = (this.searchState.selectedOption + 1) % this.dataList.length
} else if (keyCode === 38) {
if (this.searchState.selectedOption === -1) {
this.searchState.selectedOption = this.dataList.length - 1
} else {
this.searchState.selectedOption--
}
} else {
this.searchState.selectedOption = -1
}
// Update Input box value if arrow keys were pressed
if ((keyCode === 40 || keyCode === 38) && this.searchState.selectedOption !== -1) { this.inputData = this.dataList[this.searchState.selectedOption] }
},
handleInputBlur: function () {
if (!this.searchState.isPointerInList) { this.searchState.showOptions = false }
} }
} }
}) })

View File

@ -27,6 +27,9 @@
:placeholder="placeholder" :placeholder="placeholder"
:disabled="disabled" :disabled="disabled"
@input="e => handleInput(e.target.value)" @input="e => handleInput(e.target.value)"
@focus="searchState.showOptions = true"
@blur="handleInputBlur"
@keydown="e => handleKeyDown(e.keyCode)"
> >
<font-awesome-icon <font-awesome-icon
v-if="showArrow" v-if="showArrow"
@ -34,16 +37,26 @@
class="inputAction" class="inputAction"
@click="handleClick" @click="handleClick"
/> />
<datalist
v-if="dataList.length > 0" <div class="options">
:id="idDataList" <ul
> v-if="inputData !== '' && dataList.length > 0 && searchState.showOptions"
<option :id="idDataList"
v-for="(list, index) in dataList" :class="'list'"
:key="index" @mouseenter="searchState.isPointerInList = true"
:value="list" @mouseleave="searchState.isPointerInList = false"
/> >
</datalist> <li
v-for="(list, index) in dataList"
:key="index"
:class="searchState.selectedOption == index ? 'hover': ''"
@click="handleOptionClick(index)"
@mouseenter="searchState.selectedOption = index"
>
{{ list }}
</li>
</ul>
</div>
</div> </div>
</template> </template>