Skip to content

Commit

Permalink
Merge pull request #15015 from rosenthalj/inputNumberLooseFocusIssue
Browse files Browse the repository at this point in the history
fixes #15014: fixed issue with input number loosing focus
  • Loading branch information
cetincakiroglu committed Mar 13, 2024
2 parents e76ddb1 + 3c5eac8 commit 9b0518e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
30 changes: 30 additions & 0 deletions src/app/components/inputnumber/inputnumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
9 changes: 5 additions & 4 deletions src/app/components/inputnumber/inputnumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 9b0518e

Please sign in to comment.