Skip to content

Commit

Permalink
Set selection to end of text input on accessibility click
Browse files Browse the repository at this point in the history
Summary:
When we render a text input that already has a text value (<TextInput value="123" />), its selection (cursor) is automatically set to the end of the text. However, when you swipe to focus the text input with TalkBack, Android decides it needs to clear the selection, which moves the cursor back to the beginning of the text input. This is probably not what you want if you're editing some text that's already there. Ideally we would just keep the selection at the end, but I don't know how to prevent this from happening - it seems to be part of how TextView handles the accessibility focus event? So instead I'm just explicitly setting the selection to the end of the text in our handler for accessibility click.

Changelog: [Android][Fixed] Move selection to the end of the text input on accessibility click

Reviewed By: mdvacca

Differential Revision: D23441077

fbshipit-source-id: 16964f5b106637e55a98c6b0ef0f0041e8e6215d
  • Loading branch information
Emily Janzer authored and facebook-github-bot committed Sep 2, 2020
1 parent 5fdb558 commit f0e80ae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,37 @@ public void run() {
assertFalse(reactEditText.isFocused());
}

public void testAccessibilityFocus_notEmpty_selectionSetAtEnd() throws Throwable {
String testId = "textInput1";
String text = "Testing";

final ReactEditText reactEditText = getViewByTestId(testId);
reactEditText.setText(text);
runTestOnUiThread(
new Runnable() {
@Override
public void run() {
reactEditText.clearFocus();
}
});
waitForBridgeAndUIIdle();
assertFalse(reactEditText.isFocused());
assertEquals(0, reactEditText.getSelectionStart());

runTestOnUiThread(
new Runnable() {
@Override
public void run() {
reactEditText.performAccessibilityAction(
AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
reactEditText.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null);
}
});
waitForBridgeAndUIIdle();
assertTrue(reactEditText.isFocused());
assertEquals(text.length(), reactEditText.getSelectionStart());
}

private void fireEditorActionAndCheckRecording(
final ReactEditText reactEditText, final int actionId) throws Throwable {
fireEditorActionAndCheckRecording(reactEditText, actionId, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ public ReactEditText(Context context) {
@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
if (action == AccessibilityNodeInfo.ACTION_CLICK) {
int length = getText().length();
if (length > 0) {
// For some reason, when you swipe to focus on a text input that already has text in
// it, it clears the selection and resets the cursor to the beginning of the input.
// Since this is not typically (ever?) what you want, let's just explicitly set the
// selection on accessibility click to undo that.
setSelection(length);
}
return requestFocusInternal();
}
return super.performAccessibilityAction(host, action, args);
Expand Down

0 comments on commit f0e80ae

Please sign in to comment.