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

TextField: Call onChange when cleared #1102

Merged
merged 5 commits into from
Nov 19, 2019
Merged
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
23 changes: 13 additions & 10 deletions lib/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,20 @@ Use the "inputRef" prop instead to obtain a ref to the input.`);
clearField() {
const { onClearField } = this.props;

this.setState({ value: '' }, () => {
// Fire callback
if (typeof onClearField === 'function') {
onClearField();
}
// Fire callback
if (typeof onClearField === 'function') {
onClearField();
}

// Set focus on input again
if (this.input.current) {
this.input.current.focus();
}
});
// Clear value on input natively, dispatch an event to be picked up by handleChange, and focus on input again
if (this.input.current) {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
nativeInputValueSetter.call(this.input.current, '');

const ev = new Event('change', { bubbles: true });
this.input.current.dispatchEvent(ev);
this.input.current.focus();
}
}

handleChange(event) {
Expand Down