From 30cfc4c1a338152c0b16d4b8d1e52eb70c7e03a5 Mon Sep 17 00:00:00 2001 From: Imperial-Dragon Date: Fri, 11 Dec 2020 23:44:11 +0530 Subject: [PATCH] added autofill for searchbar --- src/renderer/components/ft-input/ft-input.css | 23 ++++++++++ src/renderer/components/ft-input/ft-input.js | 43 ++++++++++++++++++- src/renderer/components/ft-input/ft-input.vue | 33 +++++++++----- 3 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/renderer/components/ft-input/ft-input.css b/src/renderer/components/ft-input/ft-input.css index a87b3605..eb1a955d 100644 --- a/src/renderer/components/ft-input/ft-input.css +++ b/src/renderer/components/ft-input/ft-input.css @@ -82,3 +82,26 @@ .forceTextColor .inputAction: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; */ +} \ No newline at end of file diff --git a/src/renderer/components/ft-input/ft-input.js b/src/renderer/components/ft-input/ft-input.js index 3b845d36..9ff3b1fb 100644 --- a/src/renderer/components/ft-input/ft-input.js +++ b/src/renderer/components/ft-input/ft-input.js @@ -43,7 +43,12 @@ export default Vue.extend({ data: function () { return { id: '', - inputData: '' + inputData: '', + searchState: { + showOptions: false, + selectedOption: -1, + isPointerInList: false + } } }, computed: { @@ -72,10 +77,15 @@ export default Vue.extend({ }, methods: { handleClick: function () { + this.searchState.showOptions = false + this.$emit('input', this.inputData) this.$emit('click', this.inputData) }, handleInput: function () { + if (this.isSearch && + this.searchState.selectedOption !== -1 && + this.inputData === this.dataList[this.searchState.selectedOption]) { return } 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 } } } }) diff --git a/src/renderer/components/ft-input/ft-input.vue b/src/renderer/components/ft-input/ft-input.vue index 4e461a62..926fd9b6 100644 --- a/src/renderer/components/ft-input/ft-input.vue +++ b/src/renderer/components/ft-input/ft-input.vue @@ -27,6 +27,9 @@ :placeholder="placeholder" :disabled="disabled" @input="e => handleInput(e.target.value)" + @focus="searchState.showOptions = true" + @blur="handleInputBlur" + @keydown="e => handleKeyDown(e.keyCode)" > - - + +
+
    +
  • + {{ list }} +
  • +
+