Skip to content

Commit

Permalink
fix cursor position for overflowed text on Android TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
OlimpiaZurek committed Oct 10, 2023
1 parent b7191cd commit 094e645
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public class ReactEditText extends AppCompatEditText {

private static final KeyListener sKeyListener = QwertyKeyListener.getInstanceForFullKeyboard();
private @Nullable EventDispatcher mEventDispatcher;

public boolean isInitialRender = true;
public ReactEditText(Context context) {
super(context);
setFocusableInTouchMode(false);
Expand Down Expand Up @@ -671,6 +671,12 @@ public void maybeSetText(ReactTextUpdate reactTextUpdate) {
// text so, we have to set text to null, which will clear the currently composing text.
if (reactTextUpdate.getText().length() == 0) {
setText(null);
// When we call Editable#replace() Android moves the cursor to the end of the replaced text.
// To ensure that the cursor position remains at the beginning of the text on the initial render
// we call Editable#setText()
} else if (isInitialRender && !hasFocus()) {
setText(spannableStringBuilder);
isInitialRender = false;
} else {
// When we update text, we trigger onChangeText code that will
// try to update state if the wrapper is available. Temporarily disable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void updateExtraData(ReactEditText view, Object extraData) {
boolean isCurrentSelectionEmpty = view.getSelectionStart() == view.getSelectionEnd();
int selectionStart = UNSET;
int selectionEnd = UNSET;
if (isCurrentSelectionEmpty) {
if (isCurrentSelectionEmpty && !view.isInitialRender) {
// if selection is not set by state, shift current selection to ensure constant gap to
// text end
int textLength = view.getText() == null ? 0 : view.getText().length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,17 @@ module.exports = ([
);
},
},
{
title: 'Long text behavior',
render: function (): React.Node {
return (
<TextInput
style={styles.default}
defaultValue="I wanted to send a shorter text, but my keyboard started a protest for word count equality!"
/>
);
},
},
{
name: 'maxLength',
title: "Live Re-Write (<sp> -> '_') + maxLength",
Expand Down

0 comments on commit 094e645

Please sign in to comment.