Skip to content

Commit

Permalink
Merge pull request Textualize#2981 from seifertm/2366-switch-click-bu…
Browse files Browse the repository at this point in the history
…bbling

Fixes a bug that causes Click events to bubble up from Switch widgets.
  • Loading branch information
rodrigogiraoserrao authored Jul 22, 2023
2 parents 2f055f6 + febe363 commit 42dc3af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Fixed a crash when a `SelectionList` had a prompt wider than itself https://github.com/Textualize/textual/issues/2900
- Fixed a bug where `Click` events were bubbling up from `Switch` widgets https://github.com/Textualize/textual/issues/2366

## [0.30.0] - 2023-07-17

Expand Down
3 changes: 2 additions & 1 deletion src/textual/widgets/_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def get_content_width(self, container: Size, viewport: Size) -> int:
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return 1

async def _on_click(self, _: Click) -> None:
async def _on_click(self, event: Click) -> None:
"""Toggle the state of the switch."""
event.stop()
self.toggle()

def action_toggle(self) -> None:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from textual.app import App, ComposeResult
from textual.widgets import Switch


async def test_switch_click_doesnt_bubble_up():
"""Regression test for https://github.com/Textualize/textual/issues/2366"""

class SwitchApp(App[None]):
def compose(self) -> ComposeResult:
yield Switch()

async def on_click(self) -> None:
raise AssertionError(
"The app should never receive a click event when Switch is clicked."
)

async with SwitchApp().run_test() as pilot:
await pilot.click(Switch)

0 comments on commit 42dc3af

Please sign in to comment.