Is it possible to refactor these four methods in just two in lit-element?
I have the same counter but duplicate, one for case 0 and one for case 1. As the variables are different, I have done the same method twice to take every variable baceuse I haven't found a way to do it together.
Here is my code:
import { LitElement, html } from 'lit-element';
class TypeSelect extends LitElement {
static get properties() {
return {
numAdults: {type: Number},
numChildren: {type: Number},
};
}
constructor() {
super();
this.numAdults = 1;
this.numChildren = 0;
this.adultsType = {
min: 1,
max: 5
};
this.childrenType = {
min: 0,
max: 5
};
this.numPassengers = 1;
}
adultIncrement(e) {
e.preventDefault();
if (this.numAdults < this.adultsType.max) this.numAdults++;
}
adultDecrement(e) {
e.preventDefault();
if (this.numAdults > this.adultsType.min) this.numAdults--;
}
childrenIncrement(e) {
e.preventDefault();
if (this.numChildren < this.childrenType.max) this.numChildren++;
}
childrenDecrement(e) {
e.preventDefault();
if (this.numChildren > this.childrenType.min) this.numChildren--;
}
createRenderRoot() {
return this;
}
render(){
return html`
<div class="counter">
<button @click="${this.adultDecrement}"></button>
<span>${this.numAdults}</span>
<button @click="${this.adultIncrement}"></button>
</div>
<div class="counter">
<button @click="${this.childrenDecrement}"></button>
<span>${this.numChildren}</span>
<button @click="${this.childrenIncrement}"></button>
</div>
`;
}
}
customElements.define('type-select', TypeSelect);