diff --git a/src/app/components/inputnumber/inputnumber.spec.ts b/src/app/components/inputnumber/inputnumber.spec.ts index f0e105eebe4..76776fbff01 100755 --- a/src/app/components/inputnumber/inputnumber.spec.ts +++ b/src/app/components/inputnumber/inputnumber.spec.ts @@ -70,5 +70,35 @@ describe('InputNumber', () => { inputNumber.onInputKeyPress(pressFiveEvent); expect(testComponent.val).toEqual(5.5); }); + it('should model value', () => { + inputNumber.onInputKeyPress(pressFiveEvent); + inputNumber.onInputKeyPress(pressNumpadDecimalWithDotEvent); + inputNumber.onInputKeyPress(pressFiveEvent); + expect(typeof inputNumber.value).toEqual('number'); + expect(inputNumber.value).toEqual(5.5); + inputNumber.onInputBlur({} as Event); + expect(typeof inputNumber.value).toEqual('number'); + expect(inputNumber.value).toEqual(5.5); + const inputMaskEl = fixture.debugElement.query(By.css('input')); + inputMaskEl.nativeElement.value = ''; + const pressMinusEvent = new KeyboardEvent('event', { + code: 'Minus', + key: '-', + keyCode: '-'.charCodeAt(0) + }); + inputNumber.onInputKeyPress(pressMinusEvent); + //@ts-ignore primeNG can can set value to string '-' + expect(inputNumber.value).toEqual('-'); + expect(typeof inputNumber.value).toEqual('string'); + inputNumber.onInputBlur({} as Event); + expect(inputNumber.value).toEqual(null); + inputNumber.onInputKeyPress(pressMinusEvent); + inputNumber.onInputKeyPress(pressFiveEvent); + expect(typeof inputNumber.value).toEqual('number'); + expect(inputNumber.value).toEqual(-5); + inputNumber.onInputBlur({} as Event); + expect(typeof inputNumber.value).toEqual('number'); + expect(inputNumber.value).toEqual(-5); + }); }); }); diff --git a/src/app/components/inputnumber/inputnumber.ts b/src/app/components/inputnumber/inputnumber.ts index 4b1ccb338d9..090d5f1d686 100644 --- a/src/app/components/inputnumber/inputnumber.ts +++ b/src/app/components/inputnumber/inputnumber.ts @@ -1379,10 +1379,11 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control onInputBlur(event: Event) { this.focused = false; - let newValue = this.validateValue(this.parseValue(this.input.nativeElement.value)).toString(); - this.input.nativeElement.value = this.formatValue(newValue); - this.input.nativeElement.setAttribute('aria-valuenow', newValue); - this.updateModel(event, newValue); + const newValueNumber = this.validateValue(this.parseValue(this.input.nativeElement.value)); + const newValueString = newValueNumber?.toString(); + this.input.nativeElement.value = this.formatValue(newValueString); + this.input.nativeElement.setAttribute('aria-valuenow', newValueString); + this.updateModel(event, newValueNumber); this.onBlur.emit(event); }