Skip to content

Commit

Permalink
feat(collapsible): make title a reactive attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJGooding committed Dec 7, 2023
1 parent 82d6e3e commit c85d152
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/textual/widgets/_collapsible.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CollapsibleTitle(Widget, can_focus=True):
"""

collapsed = reactive(True)
label = reactive("Toggle")

def __init__(
self,
Expand Down Expand Up @@ -83,6 +84,7 @@ class Collapsible(Widget):
"""A collapsible container."""

collapsed = reactive(True)
title = reactive("Toggle")

DEFAULT_CSS = """
Collapsible {
Expand Down Expand Up @@ -169,14 +171,15 @@ def __init__(
classes: The CSS classes of the collapsible.
disabled: Whether the collapsible is disabled or not.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self.title = title
self._title = CollapsibleTitle(
label=title,
collapsed_symbol=collapsed_symbol,
expanded_symbol=expanded_symbol,
collapsed=collapsed,
)
self._contents_list: list[Widget] = list(children)
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self.collapsed = collapsed

def _on_collapsible_title_toggle(self, event: CollapsibleTitle.Toggle) -> None:
Expand Down Expand Up @@ -214,3 +217,8 @@ def compose_add_child(self, widget: Widget) -> None:
widget: A Widget to add.
"""
self._contents_list.append(widget)

def _watch_title(self, title: str) -> None:
if not self.is_mounted:
return
self._title.label = title
12 changes: 12 additions & 0 deletions tests/test_collapsible.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,15 @@ def on_collapsible_collapsed(self) -> None:

assert pilot.app.query_one(Collapsible).collapsed
assert len(hits) == 1


async def test_collapsible_title_reactive_change():
class CollapsibleApp(App[None]):
def compose(self) -> ComposeResult:
yield Collapsible(title="Old title")

async with CollapsibleApp().run_test() as pilot:
collapsible = pilot.app.query_one(Collapsible)
assert get_title(collapsible).label == "Old title"
collapsible.title = "New title"
assert get_title(collapsible).label == "New title"

0 comments on commit c85d152

Please sign in to comment.