From 3c5eac8f53006f009b9ce0056704433f19f7d360 Mon Sep 17 00:00:00 2001 From: joseph rosenthal Date: Mon, 11 Mar 2024 15:48:47 -0400 Subject: [PATCH] fixed onInputBlur to call call updateModel with a number new value --- .../inputnumber/inputnumber.spec.ts | 30 +++++++++++++++++++ src/app/components/inputnumber/inputnumber.ts | 9 +++--- 2 files changed, 35 insertions(+), 4 deletions(-) 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); }