Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(number-field): stepUp/stepDown not fire input event #921

Merged
13 changes: 13 additions & 0 deletions packages/elements/src/number-field/__demo__/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
</style>
</head>

<body>
<script type="module">
import '@refinitiv-ui/elements/number-field';
Expand Down Expand Up @@ -151,6 +152,18 @@
});
</script>
</demo-block>
<demo-block layout="normal" header="Input event" tags="event">
<ef-number-field id="input" placeholder="Input event"></ef-number-field>
<p>Value: <code id="input-text"></code></p>
<script>
var element = document.getElementById('input');
var inputText = document.getElementById('input-text');

element.addEventListener('input', function (e) {
inputText.innerHTML = e.target.value;
});
</script>
</demo-block>

<demo-block layout="normal" header="Error change event" tags="event">
<ef-number-field id="error-changed" min="0" max="10" placeholder="Error change event"></ef-number-field>
Expand Down
28 changes: 28 additions & 0 deletions packages/elements/src/number-field/__test__/number-field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ describe('number-field/NumberField', function () {

expect(el.value).to.equal('100');
});
it("Should fire input event when step up/down value by user's interactions", async function () {
const spinnerUp = el.shadowRoot.querySelector('[part=spinner-up]');
const spinnerDown = el.shadowRoot.querySelector('[part=spinner-down]');

let eventFiredCounter = 0;
el.addEventListener('input', () => {
eventFiredCounter += 1;
});

setTimeout(() => spinnerUp.click());
await oneEvent(el, 'input');
expect(eventFiredCounter).to.equal(1);

setTimeout(() => spinnerDown.click());
await oneEvent(el, 'input');
expect(eventFiredCounter).to.equal(2);
});
it('Should not fire input event when programmatically step up/down value', async function () {
let eventFired = false;
el.addEventListener('input', () => {
eventFired = true;
});

el.stepUp();
el.stepDown();
Sarin-Udompanish marked this conversation as resolved.
Show resolved Hide resolved
await elementUpdated(el);
expect(eventFired).to.be.false;
});
it("Should fire event when value changes by user's interactions", async function () {
const input = el.shadowRoot.querySelector('input');
input.value = '3';
Expand Down
1 change: 1 addition & 0 deletions packages/elements/src/number-field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ export class NumberField extends FormFieldElement {
protected onApplyStep(direction: Direction): void {
try {
this.applyStepDirection(direction);
this.dispatchEvent(new InputEvent('input'));
this.setSilentlyValueAndNotify();
} catch (error) {
// According to specs stepDown/stepUp may fail for some invalid inputs
Expand Down
1 change: 0 additions & 1 deletion packages/elements/src/slider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,6 @@ export class Slider extends ControlElement {
@blur=${this.onNumberFieldBlur}
@keydown=${this.onNumberFieldKeyDown}
@input=${this.onNumberFieldInput}
@value-changed=${this.onNumberFieldInput}
part="input"
name="${name}"
no-spinner
Expand Down