mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Extend @on decorator to filter matchable attributes (#2498)
* Add tests for #2484. * Implement @on extension. [skip ci] Related issues: #2484. * Changelog. * Add missing @on test. * Remove debug prints. * Document changes. * Update tests. Test now fully works, as of #2490. * Cache parsed selectors. * Streamline exit condition. * Fix typing. * More succint wording. * Document 'on' kwargs. * Update src/textual/_on.py Co-authored-by: Will McGugan <willmcgugan@gmail.com> * Update docs/guide/events.md Co-authored-by: Will McGugan <willmcgugan@gmail.com> * Change 'on' API. * Remove example code. * Address feedback. * Update src/textual/_on.py Co-authored-by: Will McGugan <willmcgugan@gmail.com> * Address review feedback. * Fix #2499. * don't require control to be manually specified * update docstring * deleted words --------- Co-authored-by: Will McGugan <willmcgugan@gmail.com>
This commit is contained in:
committed by
GitHub
parent
dd7e768887
commit
855c90d4f0
@@ -5,7 +5,7 @@ from textual._on import OnDecoratorError
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.message import Message
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Button
|
||||
from textual.widgets import Button, TabbedContent, TabPane
|
||||
|
||||
|
||||
async def test_on_button_pressed() -> None:
|
||||
@@ -102,3 +102,44 @@ def test_on_no_control() -> None:
|
||||
@on(CustomMessage, "#foo")
|
||||
def foo():
|
||||
pass
|
||||
|
||||
|
||||
def test_on_attribute_not_listed() -> None:
|
||||
"""Check `on` checks if the attribute is in ALLOW_SELECTOR_MATCH."""
|
||||
|
||||
class CustomMessage(Message):
|
||||
pass
|
||||
|
||||
with pytest.raises(OnDecoratorError):
|
||||
|
||||
@on(CustomMessage, foo="bar")
|
||||
def foo():
|
||||
pass
|
||||
|
||||
|
||||
async def test_on_arbitrary_attributes() -> None:
|
||||
log: list[str] = []
|
||||
|
||||
class OnArbitraryAttributesApp(App[None]):
|
||||
def compose(self) -> ComposeResult:
|
||||
with TabbedContent():
|
||||
yield TabPane("One", id="one")
|
||||
yield TabPane("Two", id="two")
|
||||
yield TabPane("Three", id="three")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.query_one(TabbedContent).add_class("tabs")
|
||||
|
||||
@on(TabbedContent.TabActivated, tab="#one")
|
||||
def one(self) -> None:
|
||||
log.append("one")
|
||||
|
||||
@on(TabbedContent.TabActivated, ".tabs", tab="#two")
|
||||
def two(self) -> None:
|
||||
log.append("two")
|
||||
|
||||
app = OnArbitraryAttributesApp()
|
||||
async with app.run_test() as pilot:
|
||||
await pilot.press("tab", "right", "right")
|
||||
|
||||
assert log == ["one", "two"]
|
||||
|
||||
Reference in New Issue
Block a user