Skip to content

Commit

Permalink
Fix Chat Input Crashes on Android
Browse files Browse the repository at this point in the history
The input was crashing when you sent messages that had modified the
native selection props. It crashed when you cleared the text because it
would set the text to empty, and reused a cached selection that was out
of bounds of an empty string.

hello @foo
@spacedrop
For example, say you type "hello @" and autocomplete to "hello @foo". We
do some text selection magic to insert the @foo and put your cursor in
the correct spot. The Selection start and end indicies are both 11. When
you submit to message, we clear the text. When we clear the text, RN
keeps reusing those selection indices (of 11) and Android complains the
selection is outside of the bounds of the text ("" has a length of 0,
but you want to move the cursor to position 11, outside the bounds of
0).

The workaround here is to set the selection at the same time as the
text.

The RN Issue is: facebook/react-native#25265
  • Loading branch information
MarcoPolo committed Oct 1, 2019
1 parent acaa90d commit 863e0de
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions shared/common-adapters/plain-input.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
styleSheetCreate,
isDarkMode,
} from '../styles'
import {isIOS} from '../constants/platform'
import {isIOS, isAndroid} from '../constants/platform'
import {checkTextInfo} from './input.shared'
import {pick} from 'lodash-es'
import logger from '../logger'
Expand Down Expand Up @@ -67,9 +67,19 @@ class PlainInput extends Component<InternalProps> {
}
const newTextInfo = fn(currentTextInfo)
checkTextInfo(newTextInfo)
this.setNativeProps({text: newTextInfo.text})
this._lastNativeText = newTextInfo.text
this._setSelection(newTextInfo.selection)
// If we're android, let's workaround this Issue: https://github.com/imnapo/react-native-cn-richtext-editor/issues/81
// By setting the text and selection at the same time
if (isAndroid) {
this.setNativeProps({
selection: this._sanityCheckSelection(newTextInfo.selection, newTextInfo.text),
text: newTextInfo.text,
})
this._lastNativeText = newTextInfo.text
} else {
this.setNativeProps({text: newTextInfo.text})
this._lastNativeText = newTextInfo.text
this._setSelection(newTextInfo.selection)
}
if (reflectChange) {
this._onChangeText(newTextInfo.text)
}
Expand All @@ -87,14 +97,17 @@ class PlainInput extends Component<InternalProps> {
this._setSelection(s)
}

// Validate that this selection makes sense with current value
_sanityCheckSelection = (selection: Selection, nativeText: string): Selection => {
let {start, end} = selection
end = Math.max(0, Math.min(end || 0, nativeText.length))
start = Math.min(start || 0, end)
return {end, start}
}

_setSelection = (selection: Selection) => {
this._setTimeout(() => {
// Validate that this selection makes sense with current value
let {start, end} = selection
const text = this._lastNativeText || '' // TODO write a good internal getValue fcn for this
end = Math.max(0, Math.min(end || 0, text.length))
start = Math.min(start || 0, end)
const newSelection = {end, start}
const newSelection = this._sanityCheckSelection(selection, this._lastNativeText || '')
this.setNativeProps({selection: newSelection})
this._lastNativeSelection = selection
}, 0)
Expand Down

0 comments on commit 863e0de

Please sign in to comment.