Skip to content

Commit

Permalink
Merge pull request #4707 from Textualize/fix-prevent-default
Browse files Browse the repository at this point in the history
fix prevent default
  • Loading branch information
willmcgugan committed Jul 5, 2024
2 parents 55e54af + c403d76 commit 37f0f26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/textual/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,5 @@ def _bubble_to(self, widget: MessagePump) -> None:
Args:
widget: Target of bubble.
"""
self._no_default_action = False
widget.post_message(self)
27 changes: 26 additions & 1 deletion tests/test_message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from textual.events import Key
from textual.message import Message
from textual.widget import Widget
from textual.widgets import Input
from textual.widgets import Button, Input, Label


class ValidWidget(Widget):
Expand Down Expand Up @@ -143,3 +143,28 @@ def on_input_changed(self) -> None:
app.call_next(app.change_input)
await pilot.pause()
assert hits == 2


async def test_prevent_default():
"""Test that prevent_default doesn't apply when a message is bubbled."""

app_button_pressed = False

class MyButton(Button):
def _on_button_pressed(self, event: Button.Pressed) -> None:
event.prevent_default()

class PreventApp(App[None]):
def compose(self) -> ComposeResult:
yield MyButton("Press me")
yield Label("No pressure")

def on_button_pressed(self, event: Button.Pressed) -> None:
nonlocal app_button_pressed
app_button_pressed = True
self.query_one(Label).update("Ouch!")

app = PreventApp()
async with app.run_test() as pilot:
await pilot.click(MyButton)
assert app_button_pressed

0 comments on commit 37f0f26

Please sign in to comment.