I have some logic in the watch prop in one of my Vue component's that switches between elements in sequential order upon press the down arrow key (key code 40). Its not total clutter at the moment however longterm it will become extremely inefficient. Heres the structure:
data () {
return {
firstElActive: true,
secondElActive: false,
thirdElActive: false
...
}
},
props: {
nextEl: {
type: Boolean,
default: false,
required: true
}
},
watch: {
nextEl: function (value) {
if (this.nextEl) {
if (this.firstElActive) {
this.firstElActive = !this.firstElActive;
this.secondElActive = !this.secondElActive;
... // other specific logic
}
else if (this.secondElActive) {
this.secondElActive = !this.secondElActive;
this.thirdElActive = !this.thirdElActive;
... // other specific logic
}
... // so forth
}
}
}
As you can probably assess, this will get pretty bad, pretty quick. I have Lodash bootstrapped globally (window._ = require('lodash')), and would like to utilize it... I'm just in a quandary as to which method(s) will refactor this most efficiently. Suggestions?
nextElboolean will be togglingfalse/trueand every time it goestrueyou want to "move forward" the active el?activeElementand hide/show based on that. When your key is pressed, all you would do is determine which element is active next and set it. Then it's trivial to handle any number of elements. lodash is really not necessary.activeElementin onKeyDown.