Skip to content

Instantly share code, notes, and snippets.

@theAlgorithmist
Created April 25, 2019 12:31
Show Gist options
  • Save theAlgorithmist/a2ce0b6da2e43cc2f6f63befc5710eae to your computer and use it in GitHub Desktop.
Save theAlgorithmist/a2ce0b6da2e43cc2f6f63befc5710eae to your computer and use it in GitHub Desktop.
Keyup handler for input editing
@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