I have a Lightning-Input-Field
with onchange
and handleChange
method. I dispatch an event to the grandparent component to do some calculations.
1st component
handleOnChange(event){
this.oldCellValue = this.cellValue;
this.cellValue = event.target.value;
this.cellValues = [];
this.cellValuesForSum = [];
if(this.cellValue == ""){
this.cellValues.push({ rowId: this.rowid, columnId: this.columnid, quotep: null });
this.cellValuesForSum.push({ rowId: this.rowid, columnId: this.columnid, quotep: this.oldCellValue, old : true });
} else {
this.cellValues.push({ rowId: this.rowid, columnId: this.columnid, quotep: this.cellValue });
this.cellValuesForSum.push({ rowId: this.rowid, columnId: this.columnid, quotep: this.cellValue, old : false });
}
this.dispatchEvent(new CustomEvent('enablebutton', { bubbles: true, composed: true, detail: this.cellValuesForSum[0] }));
}
Parent component
handleDisabled(event) {
console.log('hereeee ' + JSON.stringify(this.tantiemesRestants));
var result = this.tantiemesRestants.find(item => item.columnId == event.detail.columnId);
if (result != undefined && event.detail.old == false) {
this.tantiemesRestants.find(item => item.columnId == event.detail.columnId).sum = this.tantiemesRestants.find(item => item.columnId == event.detail.columnId).sum - Number(event.detail.quotep);
}
if (result != undefined && event.detail.old == true) {
this.tantiemesRestants.find(item => item.columnId == event.detail.columnId).sum = this.tantiemesRestants.find(item => item.columnId == event.detail.columnId).sum + Number(event.detail.quotep);
}
this.disabled = false;
}
The problem is, since the event is onChange
the calculation happens many times (every time the user enters a number).
For example : I want to type 27, the subtraction happens twice : once with 2 and twice with 27.
What can I do to prevent this from happening?
onblur
instead so you capture the input when the user clicks or tabs away from the input.