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] Fix TextField with Chinese input method #6562

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class TextField extends Component {
* @param {string} newValue The new value of the text field.
*/
onChange: PropTypes.func,
onCompositionEnd: PropTypes.func,
onCompositionStart: PropTypes.func,
onCompositionUpdate: PropTypes.func,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can @ignore these.

/** @ignore */
onFocus: PropTypes.func,
/**
Expand Down Expand Up @@ -259,6 +262,7 @@ class TextField extends Component {
isFocused: false,
errorText: undefined,
hasValue: false,
isTyping: false,
};

componentWillMount() {
Expand Down Expand Up @@ -348,6 +352,7 @@ class TextField extends Component {
};

handleInputChange = (event) => {
if (this.state.isTyping) return;
if (!this.props.hasOwnProperty('value')) {
this.setState({hasValue: isValid(event.target.value)});
}
Expand All @@ -356,6 +361,14 @@ class TextField extends Component {
}
};

handleComposition = (event) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you add a TODO comment explaining why this is needed (reference the FB issue), so we can revisit and remove the workaround if / when Facebook finally fix it (there is a PR pending review since Dec 2016).

switch (event.type) {
case 'compositionupdate':
case 'compositionstart': this.setState({isTyping: true}); break;
case 'compositionend': this.setState({isTyping: false}); break;
}
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to pass the event though to the appropriate composition prop if set. There are a couple of patterns for that, in use, either test if the prop is defined or (my preference) give the props empty arrow function defaults: () = {}.

handleInputFocus = (event) => {
if (this.props.disabled) {
return;
Expand Down Expand Up @@ -399,6 +412,9 @@ class TextField extends Component {
onBlur, // eslint-disable-line no-unused-vars
onChange, // eslint-disable-line no-unused-vars
onFocus, // eslint-disable-line no-unused-vars
onCompositionStart, // eslint-disable-line no-unused-vars
onCompositionUpdate, // eslint-disable-line no-unused-vars
onCompositionEnd, // eslint-disable-line no-unused-vars
style,
type,
underlineDisabledStyle,
Expand Down Expand Up @@ -444,6 +460,9 @@ class TextField extends Component {
disabled: this.props.disabled,
onBlur: this.handleInputBlur,
onChange: this.handleInputChange,
onCompositionStart: this.handleComposition,
onCompositionUpdate: this.handleComposition,
onCompositionEnd: this.handleComposition,
onFocus: this.handleInputFocus,
};

Expand Down