Created
April 25, 2019 12:31
-
-
Save theAlgorithmist/a2ce0b6da2e43cc2f6f63befc5710eae to your computer and use it in GitHub Desktop.
Keyup handler for input editing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HostListener('keyup', ['$event']) onKeyUp(evt: KeyboardEvent): boolean | |
{ | |
// test for singleton leading negative sign as first character | |
const v: string = this._input.value; | |
const n: number = v.length; | |
// for now, allow a blank field as it is possible that the entire number could be deleted by backspace before | |
// entering a new number | |
if (n == 0) { | |
return true; | |
} | |
// physical quantities may not be negative and a decimal is currently not allowed | |
if ( (n == 1 && v == "-") || (evt.key == ".") ) | |
{ | |
this.hasError = true; | |
this._input.value = this._currentValue.toString(); | |
return true; | |
} | |
// check for most recent keystroke being an enter, which is currently the only way to indicate an edit | |
const code: string = evt.code.toLowerCase(); | |
if (code == 'enter' || code == 'return') | |
{ | |
if (!isNaN(+v) && isFinite(+v)) | |
{ | |
this.hasError = false; | |
this._currentValue = +v; | |
// set 'edited' border color and emit the changed event | |
this.borderColor = '#66CD00'; | |
this._changed.emit({id: this._currentID, value: +v}); | |
} | |
else | |
{ | |
this.hasError = true; | |
this._input.value = this._currentValue.toString(); | |
} | |
return true; | |
} | |
this.hasError = !Validation.checkNumber(evt); | |
if (this.hasError) | |
{ | |
console.log( "error: ", this._currentValue ); | |
// indicate an error by replacing the bad input with the 'current' or last-known good value | |
// this may be altered in a future release | |
this._input.value = this._currentValue.toString(); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment