Skip to content

Commit

Permalink
fix(number-field): blur the element on pressing enter
Browse files Browse the repository at this point in the history
  • Loading branch information
TarunAdobe committed Sep 13, 2023
1 parent f592d64 commit 5a426e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/number-field/src/NumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,12 @@ export class NumberField extends TextfieldBase {
@property({ type: Number })
public override set value(rawValue: number) {
const value = this.validateInput(rawValue);
if (
value === this.value &&
this.__trackingValue === this._trackingValue
) {
if (value === this.value) {
return;
}
const oldValue = this._value;
this._value = value;
this.requestUpdate('value', this.__trackingValue);
this.requestUpdate('value', oldValue);
}

public override get value(): number {
Expand All @@ -176,7 +174,6 @@ export class NumberField extends TextfieldBase {
}

public override _value = NaN;
private __trackingValue = '';
private _trackingValue = '';

/**
Expand Down Expand Up @@ -343,6 +340,9 @@ export class NumberField extends TextfieldBase {
new Event('change', { bubbles: true, composed: true })
);
break;
case 'Enter':
event.preventDefault();
this.blur();
}
}

Expand All @@ -368,7 +368,6 @@ export class NumberField extends TextfieldBase {

protected override onFocus(): void {
super.onFocus();
this.__trackingValue = this._trackingValue;
this._trackingValue = this.inputValue;
this.keyboardFocused = !this.readonly && true;
this.addEventListener('wheel', this.onScroll, { passive: false });
Expand Down Expand Up @@ -450,7 +449,6 @@ export class NumberField extends TextfieldBase {
this.indeterminate = false;
this._value = this.validateInput(valueAsNumber);
}
this.__trackingValue = this._trackingValue;
this._trackingValue = value;
this.inputElement.value = value;
this.inputElement.setSelectionRange(selectionStart, selectionStart);
Expand Down
1 change: 1 addition & 0 deletions packages/number-field/test/number-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ describe('NumberField', () => {
await sendKeys({ press: 'Enter' });
expect(inputSpy.calledWith(507), 'input').to.be.true;
expect(changeSpy.calledWith(507), 'change').to.be.true;
el.focus();
await sendKeys({ type: ',00' });
await sendKeys({ press: 'Enter' });
expect(inputSpy.calledWith(5070), 'input').to.be.true;
Expand Down

0 comments on commit 5a426e4

Please sign in to comment.