@@ -83,6 +83,9 @@ class TabbedContent(Widget):
8383 }
8484 """
8585
86+ active : reactive [str ] = reactive ("" , init = False )
87+ """The ID of the active tab, or empty string if none are active."""
88+
8689 class TabActivated (Message ):
8790 """Posted when the active tab changes."""
8891
@@ -113,16 +116,21 @@ def __init__(self, *titles: TextType, initial: str = "") -> None:
113116 self ._initial = initial
114117 super ().__init__ ()
115118
116- @property
117- def active (self ) -> str :
118- """The ID of the active tab, or empty string if none are active."""
119- return self .get_child_by_type (Tabs ).active
119+ def validate_active (self , active : str ) -> str :
120+ """It doesn't make sense for `active` to be an empty string.
121+
122+ Args:
123+ active: Attribute to be validated.
124+
125+ Returns:
126+ Value of `active`.
120127
121- @active .setter
122- def active (self , active : str ) -> None :
128+ Raises:
129+ ValueError: If the active attribute is set to empty string.
130+ """
123131 if not active :
124132 raise ValueError ("'active' tab must not be empty string." )
125- self . get_child_by_type ( Tabs ). active = active
133+ return active
126134
127135 def compose (self ) -> ComposeResult :
128136 """Compose the tabbed content."""
@@ -178,6 +186,7 @@ def _on_tabs_tab_activated(self, event: Tabs.TabActivated) -> None:
178186 switcher = self .get_child_by_type (ContentSwitcher )
179187 assert isinstance (event .tab , ContentTab )
180188 switcher .current = event .tab .id
189+ self .active = event .tab .id
181190 self .post_message (
182191 TabbedContent .TabActivated (
183192 tabbed_content = self ,
@@ -188,3 +197,8 @@ def _on_tabs_tab_activated(self, event: Tabs.TabActivated) -> None:
188197 def _on_tabs_cleared (self , event : Tabs .Cleared ) -> None :
189198 """All tabs were removed."""
190199 event .stop ()
200+
201+ def watch_active (self , active : str ) -> None :
202+ """Switch tabs when the active attributes changes."""
203+ with self .prevent (Tabs .TabActivated ):
204+ self .get_child_by_type (Tabs ).active = active
0 commit comments