Skip to content

fix(input): fix invalid cursor position after updating value #5812

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed `VERTICAL_BREAKPOINTS` doesn't work https://github.com/Textualize/textual/pull/5785
- Fixed `Button` allowing text selection https://github.com/Textualize/textual/pull/5770
- Fixed `Input` invalid cursor position after updating the value https://github.com/Textualize/textual/issues/5811

## [3.2.0] - 2025-05-02

Expand Down
4 changes: 4 additions & 0 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ def _watch_value(self, value: str) -> None:
if self._initial_value:
self.cursor_position = len(self.value)
self._initial_value = False
else:
# Force a re-validation of the selection to ensure it accounts for
# the length of the new value
self.selection = self.selection

def _watch_valid_empty(self) -> None:
"""Repeat validation when valid_empty changes."""
Expand Down
13 changes: 13 additions & 0 deletions tests/input/test_input_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,16 @@ async def test_input_selection_deleted_programmatically():
input_widget.selection = Selection(4, 0)
input_widget.delete_selection()
assert input_widget.value == "o, world!"


async def test_input_selection_is_valid_after_updating_value():
"""Regression test for https://github.com/Textualize/textual/issues/5811"""
app = InputApp()
async with app.run_test() as pilot:
input_widget = pilot.app.query_one(Input)
# Sanity check (by default focusing the input selects all text)
assert input_widget.selection == (0, len(input_widget.value))

input_widget.value = "foo"

assert input_widget.selection == (0, len(input_widget.value))
Loading