Skip to content

Commit

Permalink
composebox: Improve text resetting code, circumvent iOS bug
Browse files Browse the repository at this point in the history
Fixes: #2434

There is a simple hack/fix for the iOS-specific bug that was a
blocker for us migrating the iOS version.

If we set the text via `setNativeProps` twice in a row, it will
indeed reset the contents, so the first time we use a space `' '`
that looks empty but is different than the second time when we
set it to an actually empty string '``'. We use `setTimeout` in
order to give React Native a chance to update the native value
before running the second time.

For Android we don't need to call `setNativeProps` as the
`TextInputReset.resetKeyboardInput` call does that. Also compared
to the previous version we don't need to check if `TextInputReset`
is defined as it isn't only for iOS and we `if` for that.
  • Loading branch information
borisyankov committed Sep 5, 2018
1 parent cc87383 commit 5742d57
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/compose/ComposeBox.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */
import React, { PureComponent } from 'react';
import { View, TextInput, findNodeHandle } from 'react-native';
import { View, Platform, TextInput, findNodeHandle } from 'react-native';
import { connect } from 'react-redux';
import TextInputReset from 'react-native-text-input-reset';
import isEqual from 'lodash.isequal';
Expand Down Expand Up @@ -76,20 +76,35 @@ type State = {
selection: InputSelectionType,
};

const clearTextInput = (textInput: TextInput): void => {
if (Platform.OS === 'ios') {
// Calling `setNativeProps` twice works around the currently present iOS bug
// We need to call it with different values, the ' ' looks like no text
textInput.setNativeProps({ text: ' ' });
setTimeout(() => {
textInput.setNativeProps({ text: '' });
}, 0);
} else {
// Force reset to fix an issue with some Android custom keyboards
TextInputReset.resetKeyboardInput(findNodeHandle(textInput));
}
};

export const updateTextInput = (textInput: TextInput, text: string): void => {
if (!textInput) {
// Depending on the lifecycle events this function is called from,
// this might not be set yet.
return;
}

textInput.setNativeProps({ text });

if (text.length === 0 && TextInputReset) {
// React Native has problems with some custom keyboards when clearing
// the input's contents. Force reset to make sure it works.
TextInputReset.resetKeyboardInput(findNodeHandle(textInput));
// Both iOS and Android have bugs related to clearing Input's contents
if (text.length === 0) {
clearTextInput(textInput);
}

setTimeout(() => {
textInput.setNativeProps({ text });
}, 0);
};

class ComposeBox extends PureComponent<Props, State> {
Expand Down

0 comments on commit 5742d57

Please sign in to comment.