Skip to content

Commit 5a6906b

Browse files
authored
Merge pull request #5414 from edward-jazzhands/input_blur_message
Add Blur message to Input widget
2 parents 8fac029 + 65ffbd3 commit 5a6906b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
- 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
2020
- Updated `TextArea` and `Input` behavior when there is a selection and the user presses left or right https://github.com/Textualize/textual/pull/5400
2121
- 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
22+
- Added `Blurred` message to `Input` widget (matching `Submitted` and `Changed`) to make it easier to synchronize with `validate_on` parameter when set to 'blur'.
2223
- Added `Offset.transpose` https://github.com/Textualize/textual/pull/5409
2324
- Added `screen--selection` component class to define style for selection https://github.com/Textualize/textual/pull/5409
2425
- Added `Widget.select_container` property https://github.com/Textualize/textual/pull/5409
@@ -33,6 +34,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3334

3435
### Fixed
3536

37+
- 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
38+
- Updated `TextArea` and `Input` behavior when there is a selection and the user presses left or right https://github.com/Textualize/textual/pull/5400
39+
- Footer can now be scrolled horizontally without holding `shift` https://github.com/Textualize/textual/pull/5404
40+
- Modified _on_blur method in `Input` to post a `Blurred` message
3641
- Fixed `Pilot.click` not working with `times` parameter https://github.com/Textualize/textual/pull/5398
3742
- Fixed select refocusing itself too late https://github.com/Textualize/textual/pull/5420
3843
- Fixed Log widget not refreshing on resize https://github.com/Textualize/textual/pull/5460

src/textual/widgets/_input.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,29 @@ def control(self) -> Input:
284284
"""Alias for self.input."""
285285
return self.input
286286

287+
@dataclass
288+
class Blurred(Message):
289+
"""Posted when the widget is blurred (loses focus).
290+
291+
Can be handled using `on_input_blurred` in a subclass of `Input` or in a parent
292+
widget in the DOM.
293+
"""
294+
295+
input: Input
296+
"""The `Input` widget that was changed."""
297+
298+
value: str
299+
"""The value that the input was changed to."""
300+
301+
validation_result: ValidationResult | None = None
302+
"""The result of validating the value (formed by combining the results from each validator), or None
303+
if validation was not performed (for example when no validators are specified in the `Input`s init)"""
304+
305+
@property
306+
def control(self) -> Input:
307+
"""Alias for self.input."""
308+
return self.input
309+
287310
def __init__(
288311
self,
289312
value: str | None = None,
@@ -636,8 +659,10 @@ def _on_mount(self, event: Mount) -> None:
636659

637660
def _on_blur(self, event: Blur) -> None:
638661
self._pause_blink()
639-
if "blur" in self.validate_on:
640-
self.validate(self.value)
662+
validation_result = (
663+
self.validate(self.value) if "blur" in self.validate_on else None
664+
)
665+
self.post_message(self.Blurred(self, self.value, validation_result))
641666

642667
def _on_focus(self, event: Focus) -> None:
643668
self._restart_blink()

0 commit comments

Comments
 (0)