Replies: 1 comment
-
Add a class DynamicTabbedContent(TabbedContent):
def __init__(self, *titles: TextType):
super().__init__(*titles)
self.tabs: Optional[Tabs] = None
self.content_switcher: Optional[ContentSwitcher] = None
@classmethod
def _set_id(cls, content: TabPane, new_id: str) -> TabPane:
"""Set an id on the content, if not already present.
Args:
content: a TabPane.
new_id: New `is` attribute, if it is not already set.
Returns:
The same TabPane.
"""
if content.id is None:
content.id = new_id
return content
def append(self, content: TabPane):
"""Add TabPane"""
self.titles.append(content._title)
self._tab_content.append(content)
tab_pane_with_id = self._set_id(content, f"tab-{len(self.content_switcher.children) + 1}")
content_tab = ContentTab(tab_pane_with_id._title, tab_pane_with_id.id or "")
await self.content_switcher.mount(tab_pane_with_id)
self.content_switcher._initial = self.active
self.content_switcher.on_mount()
self.tabs.add_tab(content_tab)
def compose(self) -> ComposeResult:
"""Compose the tabbed content."""
# Wrap content in a `TabPane` if required.
pane_content = [
(
self._set_id(content, f"tab-{index}")
if isinstance(content, TabPane)
else TabPane(
title or self.render_str(f"Tab {index}"), content, id=f"tab-{index}"
)
)
for index, (title, content) in enumerate(
zip_longest(self.titles, self._tab_content), 1
)
]
# Get a tab for each pane
tabs = [
ContentTab(content._title, content.id or "") for content in pane_content
]
# Yield the tabs
self.tabs = Tabs(*tabs, active=self._initial or None)
yield self.tabs
# Yield the content switcher and panes
self.content_switcher = ContentSwitcher(initial=self._initial or None)
with self.content_switcher:
yield from pane_content |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How to add TapPane to a TabbedContent like
OptionList.add_option
TabbedContent.compose_add_child
is only support using in context managerBeta Was this translation helpful? Give feedback.
All reactions