feat(input): add clear method (#3430)

* feat(input): add clear method

* update changelog

* fix method case in changelog
This commit is contained in:
TomJGooding 2023-09-29 15:22:57 +01:00 committed by GitHub
parent dee745929f
commit 3f36989004
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View file

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- `OutOfBounds` exception to be raised by `Pilot` https://github.com/Textualize/textual/pull/3360
- Added `Input.clear` method https://github.com/Textualize/textual/pull/3430
### Changed

View file

@ -481,6 +481,10 @@ class Input(Widget, can_focus=True):
self.value = f"{before}{text}{after}"
self.cursor_position += len(text)
def clear(self) -> None:
"""Clear the input."""
self.value = ""
def action_cursor_left(self) -> None:
"""Move the cursor one position to the left."""
self.cursor_position -= 1

View file

@ -0,0 +1,16 @@
from textual.app import App, ComposeResult
from textual.widgets import Input
class InputApp(App):
def compose(self) -> ComposeResult:
yield Input("Hello, World!")
async def test_input_clear():
async with InputApp().run_test() as pilot:
input_widget = pilot.app.query_one(Input)
assert input_widget.value == "Hello, World!"
input_widget.clear()
await pilot.pause()
assert input_widget.value == ""