FreeTube/src/renderer/components/ft-input/ft-input.js

136 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-02-16 19:30:00 +01:00
import Vue from 'vue'
import FtTooltip from '../ft-tooltip/ft-tooltip.vue'
2020-02-16 19:30:00 +01:00
export default Vue.extend({
name: 'FtInput',
components: {
'ft-tooltip': FtTooltip
},
2020-02-16 19:30:00 +01:00
props: {
placeholder: {
type: String,
required: true
},
value: {
type: String,
default: ''
},
2020-02-16 19:30:00 +01:00
showArrow: {
type: Boolean,
default: true
},
showLabel: {
type: Boolean,
default: false
},
isSearch: {
type: Boolean,
default: false
},
2020-08-24 04:56:33 +02:00
disabled: {
type: Boolean,
default: false
},
dataList: {
type: Array,
default: () => { return [] }
},
tooltip: {
type: String,
default: ''
}
2020-02-16 19:30:00 +01:00
},
data: function () {
return {
id: '',
2020-12-11 19:14:11 +01:00
inputData: '',
searchState: {
showOptions: false,
selectedOption: -1,
isPointerInList: false
}
}
},
computed: {
barColor: function () {
return this.$store.getters.getBarColor
},
forceTextColor: function () {
return this.isSearch && this.barColor
},
idDataList: function () {
return `${this.id}_datalist`
2020-02-16 19:30:00 +01:00
}
},
2020-08-24 04:56:33 +02:00
watch: {
value: function (val) {
this.inputData = val
}
},
2020-02-16 19:30:00 +01:00
mounted: function () {
this.id = this._uid
this.inputData = this.value
2020-02-16 19:30:00 +01:00
setTimeout(this.addListener, 200)
},
methods: {
handleClick: function () {
2020-12-11 19:14:11 +01:00
this.searchState.showOptions = false
this.$emit('input', this.inputData)
2020-02-16 19:30:00 +01:00
this.$emit('click', this.inputData)
},
handleInput: function () {
2020-12-11 19:14:11 +01:00
if (this.isSearch &&
this.searchState.selectedOption !== -1 &&
this.inputData === this.dataList[this.searchState.selectedOption]) { return }
this.$emit('input', this.inputData)
},
2020-02-16 19:30:00 +01: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 19:14:11 +01: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 }
2020-02-16 19:30:00 +01:00
}
}
})