Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fix onKeyPress so it provides the correct key when TextInput is at max length #30751

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class ReactEditText extends AppCompatEditText
private @Nullable String mFontFamily = null;
private int mFontWeight = ReactTypefaceUtils.UNSET;
private int mFontStyle = ReactTypefaceUtils.UNSET;
private int lengthFilterValue = UNSET;
osamaqarem marked this conversation as resolved.
Show resolved Hide resolved
private boolean mAutoFocus = false;
private boolean mDidAttachToWindow = false;

Expand Down Expand Up @@ -465,6 +466,14 @@ public void setFontStyle(String fontStyleString) {
}
}

public int getLengthFilterValue() {
return lengthFilterValue;
}

public void setLengthFilterValue(int lengthFilterValue) {
this.lengthFilterValue = lengthFilterValue;
}

public void maybeUpdateTypeface() {
if (!mTypefaceDirty) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,17 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {
boolean noPreviousSelection = previousSelectionStart == previousSelectionEnd;
boolean cursorDidNotMove = currentSelectionStart == previousSelectionStart;
boolean cursorMovedBackwardsOrAtBeginningOfInput =
(currentSelectionStart < previousSelectionStart) || currentSelectionStart <= 0;
if (cursorMovedBackwardsOrAtBeginningOfInput || (!noPreviousSelection && cursorDidNotMove)) {
osamaqarem marked this conversation as resolved.
Show resolved Hide resolved
osamaqarem marked this conversation as resolved.
Show resolved Hide resolved
(currentSelectionStart < previousSelectionStart) || currentSelectionStart <= 0;
int maxLength = mEditText.getLengthFilterValue();
Copy link
Author

@osamaqarem osamaqarem Jan 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way of getting the maxLength would be looping through the InputFilters on the EditText until we find the LengthFilter. Let me know if that approach is preferrable.

int textLength = text.length();
boolean editTextAtMaxLength = mEditText.getText().length() == maxLength;
if (editTextAtMaxLength && textLength != 0) {
key = String.valueOf(text.charAt(textLength - 1));
osamaqarem marked this conversation as resolved.
Show resolved Hide resolved
osamaqarem marked this conversation as resolved.
Show resolved Hide resolved
}
else if (cursorMovedBackwardsOrAtBeginningOfInput || (!noPreviousSelection && cursorDidNotMove)) {
key = BACKSPACE_KEY_VALUE;
} else {
osamaqarem marked this conversation as resolved.
Show resolved Hide resolved
}
else {
key = String.valueOf(mEditText.getText().charAt(currentSelectionStart - 1));
}
dispatchKeyEventOrEnqueue(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ public void setMaxLength(ReactEditText view, @Nullable Integer maxLength) {
newFilters = new InputFilter[1];
newFilters[0] = new InputFilter.LengthFilter(maxLength);
}
view.setLengthFilterValue(maxLength);
}

view.setFilters(newFilters);
Expand Down