Commit d352c57
committed
fix(android): preserve cursor position when centered TextInput is cleared
On Android, clearing a TextInput with `textAlign: "center"` caused the
cursor to jump to the right edge of the field instead of staying
centered. The cause was `setText(null)` in `ReactEditText.maybeSetText`,
which replaces the underlying `Editable` buffer; recreating the buffer
loses the gravity-based caret positioning that `Gravity.CENTER_HORIZONTAL`
relies on.
Instead of swapping the buffer, remove any composing spans from the
existing `Editable` (preserving the original "buggy keyboards don't
clear composing text" intent) and clear its contents in place via
`Editable.replace(...)`. This mirrors the approach already used by the
non-empty branch a few lines below and keeps gravity-based positioning
intact, so the cursor stays centered (or right-aligned for RTL/right
gravity) after clearing.
Adds a Robolectric regression test in `ReactTextInputPropertyTest`
that asserts the underlying `Editable` instance is preserved across a
clear (the discriminating behavioral difference vs. `setText(null)`).
The test was verified to fail against the original buggy code and pass
against the fix.
Fixes #55457.
Made-with: Cursor
Made-with:1 parent ccff70b commit d352c57
2 files changed
Lines changed: 44 additions & 2 deletions
File tree
- packages/react-native/ReactAndroid/src
- main/java/com/facebook/react/views/textinput
- test/java/com/facebook/react/views/textinput
Lines changed: 13 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
679 | 680 | | |
680 | 681 | | |
681 | 682 | | |
682 | | - | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
683 | 686 | | |
684 | | - | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
685 | 696 | | |
686 | 697 | | |
687 | 698 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
473 | 475 | | |
474 | 476 | | |
475 | 477 | | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
476 | 507 | | |
477 | 508 | | |
478 | 509 | | |
| |||
0 commit comments