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: fire input event when clearing on Esc #3408

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/field-base/src/input-control-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ export const InputControlMixin = (superclass) =>
_onClearButtonClick(event) {
event.preventDefault();
this.inputElement.focus();
this.clear();
this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
this.__clear();
}

/**
Expand Down Expand Up @@ -137,10 +135,8 @@ export const InputControlMixin = (superclass) =>
_onKeyDown(event) {
super._onKeyDown(event);

if (event.key === 'Escape' && this.clearButtonVisible) {
const dispatchChange = !!this.value;
this.clear();
dispatchChange && this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
if (event.key === 'Escape' && this.clearButtonVisible && !!this.value) {
this.__clear();
}
}

Expand All @@ -167,4 +163,11 @@ export const InputControlMixin = (superclass) =>
})
);
}

/** @private */
__clear() {
this.clear();
this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
}
};
11 changes: 11 additions & 0 deletions packages/field-base/test/input-control-mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ describe('input-control-mixin', () => {
expect(input.value).to.equal('foo');
});

it('should dispatch input event when clearing value on Esc', () => {
const spy = sinon.spy();
input.addEventListener('input', spy);
element.clearButtonVisible = true;
escKeyDown(button);
expect(spy.calledOnce).to.be.true;
const event = spy.firstCall.args[0];
expect(event.bubbles).to.be.true;
expect(event.composed).to.be.true;
});

it('should dispatch change event when clearing value on Esc', () => {
const spy = sinon.spy();
input.addEventListener('change', spy);
Expand Down