Merge pull request #3150 from Textualize/fix-3145

Allow modifying tabs in nested contexts
This commit is contained in:
Rodrigo Girão Serrão
2023-08-23 14:01:10 +01:00
committed by GitHub
3 changed files with 70 additions and 8 deletions

View File

@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
### Fixed
- Could not hide/show/disable/enable tabs in nested `TabbedContent` https://github.com/Textualize/textual/pull/3150
## [0.34.0] - 2023-08-22
### Added

View File

@@ -379,18 +379,22 @@ class TabbedContent(Widget):
def _on_tabs_tab_disabled(self, event: Tabs.TabDisabled) -> None:
"""Disable the corresponding tab pane."""
event.stop()
tab_id = event.tab.id
tab_id = event.tab.id or ""
try:
self.query_one(f"TabPane#{tab_id}").disabled = True
self.get_child_by_type(ContentSwitcher).get_child_by_id(
tab_id, expect_type=TabPane
).disabled = True
except NoMatches:
return
def _on_tabs_tab_enabled(self, event: Tabs.TabEnabled) -> None:
"""Enable the corresponding tab pane."""
event.stop()
tab_id = event.tab.id
tab_id = event.tab.id or ""
try:
self.query_one(f"TabPane#{tab_id}").disabled = False
self.get_child_by_type(ContentSwitcher).get_child_by_id(
tab_id, expect_type=TabPane
).disabled = False
except NoMatches:
return
@@ -404,7 +408,7 @@ class TabbedContent(Widget):
Tabs.TabError: If there are any issues with the request.
"""
self.query_one(Tabs).disable(tab_id)
self.get_child_by_type(Tabs).disable(tab_id)
def enable_tab(self, tab_id: str) -> None:
"""Enables the tab with the given ID.
@@ -416,7 +420,7 @@ class TabbedContent(Widget):
Tabs.TabError: If there are any issues with the request.
"""
self.query_one(Tabs).enable(tab_id)
self.get_child_by_type(Tabs).enable(tab_id)
def hide_tab(self, tab_id: str) -> None:
"""Hides the tab with the given ID.
@@ -428,7 +432,7 @@ class TabbedContent(Widget):
Tabs.TabError: If there are any issues with the request.
"""
self.query_one(Tabs).hide(tab_id)
self.get_child_by_type(Tabs).hide(tab_id)
def show_tab(self, tab_id: str) -> None:
"""Shows the tab with the given ID.
@@ -440,4 +444,4 @@ class TabbedContent(Widget):
Tabs.TabError: If there are any issues with the request.
"""
self.query_one(Tabs).show(tab_id)
self.get_child_by_type(Tabs).show(tab_id)

View File

@@ -696,3 +696,55 @@ async def test_showing_first_tab_activates_tab(tab_id: str):
tabbed_content.show_tab(tab_id)
await pilot.pause()
assert tabbed_content.active == tab_id
async def test_disabling_nested_tabs():
"""Regression test for https://github.com/Textualize/textual/issues/3145."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent(id="tabbed-content"):
with TabPane("Tab Pane 1"):
yield Label("foo")
with TabPane("Tab Pane 2"):
yield Label("bar")
with TabPane("Tab Pane 3"):
with TabbedContent():
with TabPane("Inner Pane 1"):
yield Label("fizz")
with TabPane("Inner Pane 2"):
yield Label("bang")
app = TabbedApp()
async with app.run_test() as pilot:
tabber = app.query_one("#tabbed-content", expect_type=TabbedContent)
tabber.disable_tab("tab-1")
await pilot.pause()
tabber.enable_tab("tab-1")
await pilot.pause()
async def test_hiding_nested_tabs():
"""Regression test for https://github.com/Textualize/textual/issues/3145."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent(id="tabbed-content"):
with TabPane("Tab Pane 1"):
yield Label("foo")
with TabPane("Tab Pane 2"):
yield Label("bar")
with TabPane("Tab Pane 3"):
with TabbedContent():
with TabPane("Inner Pane 1"):
yield Label("fizz")
with TabPane("Inner Pane 2"):
yield Label("bang")
app = TabbedApp()
async with app.run_test() as pilot:
tabber = app.query_one("#tabbed-content", expect_type=TabbedContent)
tabber.hide_tab("tab-1")
await pilot.pause()
tabber.show_tab("tab-1")
await pilot.pause()