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

Add Blur message to Input widget #5414

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Added `from_app_focus` to `Focus` event to indicate if a widget is being focused because the app itself has regained focus or not https://github.com/Textualize/textual/pull/5379
- - Added `Select.type_to_search` which allows you to type to move the cursor to a matching option https://github.com/Textualize/textual/pull/5403
- Added `Blur` message to `Input` widget (matching `Submitted` and `Changed`) to make it easier to synchronize with `validate_on` parameter when set to 'blur'.

### Changed

- The content of an `Input` will now only be automatically selected when the widget is focused by the user, not when the app itself has regained focus (similar to web browsers). https://github.com/Textualize/textual/pull/5379
- Updated `TextArea` and `Input` behavior when there is a selection and the user presses left or right https://github.com/Textualize/textual/pull/5400
- Footer can now be scrolled horizontally without holding `shift` https://github.com/Textualize/textual/pull/5404
- Modified _on_blur method in `Input` to post a `Blur` message


## [1.0.0] - 2024-12-12
Expand Down
29 changes: 27 additions & 2 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ def control(self) -> Input:
"""Alias for self.input."""
return self.input

@dataclass
class Blur(Message):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's call it Blurred, which is more consistent with existing messages, and avoids confusion with the existing Blur message.

"""Posted when the widget is blurred (loses focus).

Can be handled using `on_input_blur` in a subclass of `Input` or in a parent
widget in the DOM.
"""

input: Input
"""The `Input` widget that was changed."""

value: str
"""The value that the input was changed to."""

validation_result: ValidationResult | None = None
"""The result of validating the value (formed by combining the results from each validator), or None
if validation was not performed (for example when no validators are specified in the `Input`s init)"""

@property
def control(self) -> Input:
"""Alias for self.input."""
return self.input

def __init__(
self,
value: str | None = None,
Expand Down Expand Up @@ -636,8 +659,10 @@ def _on_mount(self, event: Mount) -> None:

def _on_blur(self, event: Blur) -> None:
self._pause_blink()
if "blur" in self.validate_on:
self.validate(self.value)
validation_result = (
self.validate(self.value) if "blur" in self.validate_on else None
)
self.post_message(self.Blur(self, self.value, validation_result))

def _on_focus(self, event: Focus) -> None:
self._restart_blink()
Expand Down
Loading