Skip to content

Commit

Permalink
fix(number-field): added a variable to track last committed value in …
Browse files Browse the repository at this point in the history
…input
  • Loading branch information
TarunAdobe authored and Westbrook Johnson committed Sep 29, 2023
1 parent 8c16cb7 commit 65ac8c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions packages/number-field/src/NumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ export class NumberField extends TextfieldBase {
}
const oldValue = this._value;
this._value = value;
if (!this.managedInput) {
if (!this.managedInput && this.lastCommitedValue != this.value) {
this.dispatchEvent(
new Event('change', { bubbles: true, composed: true })
);
this.lastCommitedValue = this.value;
}
this.requestUpdate('value', oldValue);
}
Expand All @@ -180,6 +181,7 @@ export class NumberField extends TextfieldBase {

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

/**
* Retreive the value of the element parsed to a Number.
Expand Down Expand Up @@ -285,9 +287,12 @@ export class NumberField extends TextfieldBase {
this.buttons.releasePointerCapture(event.pointerId);
cancelAnimationFrame(this.nextChange);
clearTimeout(this.safty);
this.dispatchEvent(
new Event('change', { bubbles: true, composed: true })
);
if (this.lastCommitedValue != this.value) {
this.dispatchEvent(
new Event('change', { bubbles: true, composed: true })
);
this.lastCommitedValue = this.value;
}
this.managedInput = false;
}

Expand Down Expand Up @@ -357,6 +362,7 @@ export class NumberField extends TextfieldBase {
this.dispatchEvent(
new Event('change', { bubbles: true, composed: true })
);
this.lastCommitedValue = this.value;
}, CHANGE_DEBOUNCE_MS) as unknown as number;
}
this.managedInput = false;
Expand Down Expand Up @@ -400,7 +406,10 @@ export class NumberField extends TextfieldBase {
}
this.value = value;
this.inputElement.value = this.formattedValue;
super.handleChange();
if (this.lastCommitedValue != this.value) {
this.lastCommitedValue = this.value;
super.handleChange();
}
}

protected handleCompositionStart(): void {
Expand Down
6 changes: 3 additions & 3 deletions packages/number-field/test/number-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ describe('NumberField', () => {
],
});
expect(inputSpy.callCount).to.equal(4);
expect(changeSpy.callCount).to.equal(1);
expect(changeSpy.callCount).to.equal(0); // the actual value hasn't changed so changespy won't be called
});
});
it('accepts pointer interactions with the stepper UI', async () => {
Expand Down Expand Up @@ -751,7 +751,7 @@ describe('NumberField', () => {
await sendKeys({ press: 'Enter' });
await elementUpdated(el);
expect(lastInputValue, 'last input value').to.equal(10);
expect(lastChangeValue, 'last change value').to.equal(10);
expect(lastChangeValue, 'last change value').to.equal(0); // value wouldn't go beyond max, so it remains unchanged after pressing enter and thus no changeSpy called
expect(el.formattedValue).to.equal('10');
expect(el.valueAsString).to.equal('10');
expect(el.value).to.equal(10);
Expand Down Expand Up @@ -811,7 +811,7 @@ describe('NumberField', () => {
await sendKeys({ press: 'Enter' });
await elementUpdated(el);
expect(lastInputValue, 'last input value').to.equal(10);
expect(lastChangeValue, 'last change value').to.equal(10);
expect(lastChangeValue, 'last change value').to.equal(0);
expect(el.formattedValue).to.equal('10');
expect(el.valueAsString).to.equal('10');
expect(el.value).to.equal(10);
Expand Down

0 comments on commit 65ac8c0

Please sign in to comment.