2020-02-16 18:30:00 +00:00
|
|
|
import Vue from 'vue'
|
2020-10-19 15:32:53 +00:00
|
|
|
import FtTooltip from '../ft-tooltip/ft-tooltip.vue'
|
2020-02-16 18:30:00 +00:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
name: 'FtInput',
|
2020-10-19 15:32:53 +00:00
|
|
|
components: {
|
|
|
|
'ft-tooltip': FtTooltip
|
|
|
|
},
|
2020-02-16 18:30:00 +00:00
|
|
|
props: {
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
2020-06-02 02:42:29 +00:00
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
2020-02-16 18:30:00 +00:00
|
|
|
showArrow: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
2020-03-01 03:37:02 +00:00
|
|
|
},
|
2020-06-02 02:42:29 +00:00
|
|
|
showLabel: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2020-03-01 03:37:02 +00:00
|
|
|
isSearch: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2020-06-02 02:42:29 +00:00
|
|
|
},
|
2021-04-15 19:30:26 +00:00
|
|
|
selectOnFocus: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2020-08-24 02:56:33 +00:00
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2020-06-02 02:42:29 +00:00
|
|
|
dataList: {
|
|
|
|
type: Array,
|
|
|
|
default: () => { return [] }
|
2020-10-19 15:32:53 +00:00
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
2020-08-05 02:18:39 +00:00
|
|
|
}
|
2020-02-16 18:30:00 +00:00
|
|
|
},
|
|
|
|
data: function () {
|
|
|
|
return {
|
|
|
|
id: '',
|
2020-12-11 18:14:11 +00:00
|
|
|
inputData: '',
|
|
|
|
searchState: {
|
|
|
|
showOptions: false,
|
|
|
|
selectedOption: -1,
|
|
|
|
isPointerInList: false
|
|
|
|
}
|
2020-03-01 03:37:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
barColor: function () {
|
|
|
|
return this.$store.getters.getBarColor
|
|
|
|
},
|
|
|
|
|
|
|
|
forceTextColor: function () {
|
|
|
|
return this.isSearch && this.barColor
|
2020-06-02 02:42:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
idDataList: function () {
|
|
|
|
return `${this.id}_datalist`
|
2020-02-16 18:30:00 +00:00
|
|
|
}
|
|
|
|
},
|
2020-08-24 02:56:33 +00:00
|
|
|
watch: {
|
|
|
|
value: function (val) {
|
|
|
|
this.inputData = val
|
|
|
|
}
|
|
|
|
},
|
2020-02-16 18:30:00 +00:00
|
|
|
mounted: function () {
|
|
|
|
this.id = this._uid
|
2020-06-19 19:58:59 +00:00
|
|
|
this.inputData = this.value
|
2020-02-16 18:30:00 +00:00
|
|
|
|
|
|
|
setTimeout(this.addListener, 200)
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleClick: function () {
|
2020-12-11 18:14:11 +00:00
|
|
|
this.searchState.showOptions = false
|
|
|
|
this.$emit('input', this.inputData)
|
2020-02-16 18:30:00 +00:00
|
|
|
this.$emit('click', this.inputData)
|
|
|
|
},
|
|
|
|
|
2020-06-19 19:58:59 +00:00
|
|
|
handleInput: function () {
|
2020-12-11 18:14:11 +00:00
|
|
|
if (this.isSearch &&
|
|
|
|
this.searchState.selectedOption !== -1 &&
|
|
|
|
this.inputData === this.dataList[this.searchState.selectedOption]) { return }
|
2020-06-19 19:58:59 +00:00
|
|
|
this.$emit('input', this.inputData)
|
2020-06-02 02:42:29 +00:00
|
|
|
},
|
|
|
|
|
2020-02-16 18:30:00 +00:00
|
|
|
addListener: function () {
|
|
|
|
const inputElement = document.getElementById(this.id)
|
|
|
|
|
|
|
|
if (inputElement !== null) {
|
|
|
|
inputElement.addEventListener('keydown', (event) => {
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
this.handleClick()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-12-11 18:14:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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 }
|
2021-04-15 19:30:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleFocus: function(e) {
|
|
|
|
this.searchState.showOptions = true
|
|
|
|
if (this.selectOnFocus) {
|
|
|
|
e.target.select()
|
|
|
|
}
|
2020-02-16 18:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|