Skip to content

Commit

Permalink
compose: Make topic text input uncontrolled
Browse files Browse the repository at this point in the history
Fixes #2589

Remove the `value={topic}` from topic text input making it
an uncontrolled component.

To replicate the previous behavior:
 * initialze at the start in `componentDidMount`
 * make sure the topic input text is updated when we focus on a
the message input (it is invisible until then)
 * change the text on picking a topic autocomplete suggestion
  • Loading branch information
borisyankov committed Jun 28, 2018
1 parent 2345414 commit 79007d6
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/compose/ComposeBox.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class ComposeBox extends PureComponent<Props, State> {
this.handleMessageChange(message);
};

setTopicInputValue = (topic: string) => {
updateTextInput(this.topicInput, topic);
this.handleTopicChange(topic);
};

handleComposeMenuToggle = () => {
this.setState(({ isMenuExpanded }) => ({
isMenuExpanded: !isMenuExpanded,
Expand All @@ -136,6 +141,10 @@ class ComposeBox extends PureComponent<Props, State> {
this.setState({ topic, isMenuExpanded: false });
};

handleTopicAutocomplete = (topic: string) => {
this.setTopicInputValue(topic);
};

handleMessageChange = (message: string) => {
this.setState({ message, isMenuExpanded: false });
const { dispatch, narrow } = this.props;
Expand All @@ -152,12 +161,15 @@ class ComposeBox extends PureComponent<Props, State> {
};

handleMessageFocus = () => {
const { topic } = this.state;
const { lastMessageTopic } = this.props;
this.setState(({ topic }) => ({
this.setState({
isMessageFocused: true,
isMenuExpanded: false,
topic: topic || lastMessageTopic,
}));
});
setTimeout(() => {
this.setTopicInputValue(topic || lastMessageTopic);
}, 200);
};

handleMessageBlur = () => {
Expand Down Expand Up @@ -234,9 +246,10 @@ class ComposeBox extends PureComponent<Props, State> {
};

componentDidMount() {
const { message } = this.state;
const { message, topic } = this.state;

updateTextInput(this.messageInput, message);
updateTextInput(this.topicInput, topic);
}

componentWillUnmount() {
Expand All @@ -251,9 +264,7 @@ class ComposeBox extends PureComponent<Props, State> {
: '';
const message = nextProps.editMessage ? nextProps.editMessage.content : '';
this.setMessageInputValue(message);
this.setState({
topic,
});
this.setTopicInputValue(topic);
if (this.messageInput) {
this.messageInput.focus();
}
Expand Down Expand Up @@ -298,7 +309,7 @@ class ComposeBox extends PureComponent<Props, State> {
narrow={narrow}
topicText={topic}
onMessageAutocomplete={this.handleMessageAutocomplete}
onTopicAutocomplete={this.handleTopicChange}
onTopicAutocomplete={this.handleTopicAutocomplete}
/>
<View style={styles.composeBox} onLayout={this.handleLayoutChange}>
<View style={styles.alignBottom}>
Expand All @@ -322,7 +333,6 @@ class ComposeBox extends PureComponent<Props, State> {
onFocus={this.handleTopicFocus}
onBlur={this.handleTopicBlur}
onTouchStart={this.handleInputTouchStart}
value={topic}
/>
)}
<MultilineInput
Expand Down

0 comments on commit 79007d6

Please sign in to comment.