Fix TabbedContent.active not changing actual content

Ensures that the actual content gets changed when TabbedContent.active is
updated via code. Also adds more testing for TabbedContent.

See #2352 for details.
This commit is contained in:
Dave Pearson
2023-04-22 08:28:12 +01:00
parent cd5d9d1e4f
commit c022d9de2f
2 changed files with 41 additions and 2 deletions

View File

@@ -202,3 +202,4 @@ class TabbedContent(Widget):
"""Switch tabs when the active attributes changes.""" """Switch tabs when the active attributes changes."""
with self.prevent(Tabs.TabActivated): with self.prevent(Tabs.TabActivated):
self.get_child_by_type(Tabs).active = active self.get_child_by_type(Tabs).active = active
self.get_child_by_type(ContentSwitcher).current = active

View File

@@ -4,8 +4,8 @@ from textual.app import App, ComposeResult
from textual.widgets import Label, TabbedContent, TabPane from textual.widgets import Label, TabbedContent, TabPane
async def test_tabbed_content_switch(): async def test_tabbed_content_switch_via_ui():
"""Check tab navigation.""" """Check tab navigation via the user interface."""
class TabbedApp(App): class TabbedApp(App):
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
@@ -54,6 +54,44 @@ async def test_tabbed_content_switch():
assert not app.query_one("#bar-label").region assert not app.query_one("#bar-label").region
assert app.query_one("#baz-label").region assert app.query_one("#baz-label").region
async def test_tabbed_content_switch_via_code():
"""Check tab navigation via code."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("foo", id="foo"):
yield Label("Foo", id="foo-label")
with TabPane("bar", id="bar"):
yield Label("Bar", id="bar-label")
with TabPane("baz", id="baz"):
yield Label("Baz", id="baz-label")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
# Check first tab
assert tabbed_content.active == "foo"
assert app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Click second tab
tabbed_content.active = "bar"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Click third tab
tabbed_content.active = "baz"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert app.query_one("#baz-label").region
# Check fail with non existent tab # Check fail with non existent tab
with pytest.raises(ValueError): with pytest.raises(ValueError):
tabbed_content.active = "X" tabbed_content.active = "X"