Skip to content

Commit 62bda23

Browse files
committed
compose box: Handle every message and topic update directly
This establishes a clear one-directional flow of the data, from secondary prop/state updates to the component via a new function `setInputs`. The primary props of `message` and `topic` are used to track the current value, but we never update the current input values from them.
1 parent 0a14816 commit 62bda23

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/compose/ComposeBox.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ export const updateTextInput = (textInput: TextInput, text: string): void => {
7474
return;
7575
}
7676

77-
if (text) {
78-
textInput.setNativeProps({ text });
79-
} else {
80-
textInput.clear();
77+
textInput.setNativeProps({ text });
78+
79+
if (text.length === 0) {
8180
if (TextInputReset) {
8281
TextInputReset.resetKeyboardInput(findNodeHandle(textInput));
8382
}
@@ -106,6 +105,16 @@ class ComposeBox extends PureComponent<Props, State> {
106105
selection: { start: 0, end: 0 },
107106
};
108107

108+
setMessageInputValue = (message: string) => {
109+
updateTextInput(this.messageInput, message);
110+
this.handleMessageChange(message);
111+
};
112+
113+
setTopicInputValue = (topic: string) => {
114+
updateTextInput(this.topicInput, topic);
115+
this.handleTopicChange(topic);
116+
};
117+
109118
handleComposeMenuToggle = () => {
110119
this.setState(({ isMenuExpanded }) => ({
111120
isMenuExpanded: !isMenuExpanded,
@@ -122,9 +131,8 @@ class ComposeBox extends PureComponent<Props, State> {
122131
this.setState({ topic, isMenuExpanded: false });
123132
};
124133

125-
handleTopicAutocomplete = (message: string) => {
126-
this.handleTopicChange(message);
127-
updateTextInput(this.topicInput, message);
134+
handleTopicAutocomplete = (topic: string) => {
135+
this.setTopicInputValue(topic);
128136
};
129137

130138
handleMessageChange = (message: string) => {
@@ -134,8 +142,7 @@ class ComposeBox extends PureComponent<Props, State> {
134142
};
135143

136144
handleMessageAutocomplete = (message: string) => {
137-
this.handleMessageChange(message);
138-
updateTextInput(this.messageInput, message);
145+
this.setMessageInputValue(message);
139146
};
140147

141148
handleMessageSelectionChange = (event: Object) => {
@@ -144,12 +151,15 @@ class ComposeBox extends PureComponent<Props, State> {
144151
};
145152

146153
handleMessageFocus = () => {
154+
const { topic } = this.state;
147155
const { lastMessageTopic } = this.props;
148-
this.setState(({ topic }) => ({
156+
this.setState({
149157
isMessageFocused: true,
150158
isMenuExpanded: false,
151-
topic: topic || lastMessageTopic,
152-
}));
159+
});
160+
setTimeout(() => {
161+
this.setTopicInputValue(topic || lastMessageTopic);
162+
}, 200);
153163
};
154164

155165
handleMessageBlur = () => {
@@ -193,6 +203,7 @@ class ComposeBox extends PureComponent<Props, State> {
193203

194204
dispatch(addToOutbox(destinationNarrow, message));
195205
dispatch(draftRemove(narrow));
206+
this.setMessageInputValue('');
196207
};
197208

198209
handleEdit = () => {
@@ -227,22 +238,28 @@ class ComposeBox extends PureComponent<Props, State> {
227238
this.tryUpdateDraft();
228239
}
229240

241+
componentDidMount() {
242+
const { message, topic } = this.state;
243+
244+
updateTextInput(this.messageInput, message);
245+
updateTextInput(this.topicInput, topic);
246+
}
247+
230248
componentWillReceiveProps(nextProps: Props) {
231249
if (nextProps.editMessage !== this.props.editMessage) {
232250
const topic =
233251
isStreamNarrow(nextProps.narrow) && nextProps.editMessage
234252
? nextProps.editMessage.topic
235253
: '';
236-
this.setState({
237-
message: nextProps.editMessage ? nextProps.editMessage.content : '',
238-
topic,
239-
});
254+
const message = nextProps.editMessage ? nextProps.editMessage.content : '';
255+
this.setMessageInputValue(message);
256+
this.setTopicInputValue(topic);
240257
if (this.messageInput) {
241258
this.messageInput.focus();
242259
}
243260
} else if (!isEqual(nextProps.narrow, this.props.narrow)) {
244261
this.tryUpdateDraft();
245-
this.setState({ message: nextProps.draft });
262+
this.setMessageInputValue(nextProps.draft);
246263
}
247264
}
248265

0 commit comments

Comments
 (0)