Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/textual/widgets/_collapsible.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CollapsibleTitle(Static, can_focus=True):
"""

collapsed = reactive(True)
label: reactive[ContentText] = reactive(Content("Toggle"))
label: reactive[ContentText] = reactive(Content("Toggle"), always_update=True)

def __init__(
self,
Expand All @@ -64,8 +64,11 @@ def __init__(
super().__init__()
self.collapsed_symbol = collapsed_symbol
self.expanded_symbol = expanded_symbol
self.label = Content.from_text(label)
self.collapsed = collapsed
# Both inputs belong to the initial title. Do not render reactive
# defaults or an intermediate collapsed symbol before acquiring them.
self.set_reactive(CollapsibleTitle.label, Content.from_text(label))
self.set_reactive(CollapsibleTitle.collapsed, collapsed)
self._update_label()

class Toggle(Message):
"""Request toggle."""
Expand Down
26 changes: 26 additions & 0 deletions tests/test_collapsible.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@

from textual import on
from textual.app import App, ComposeResult
from textual.content import Content
from textual.widgets import Collapsible, Label
from textual.widgets._collapsible import CollapsibleTitle

COLLAPSED_CLASS = "-collapsed"


async def test_title_initial_content_and_same_text_style():
title = CollapsibleTitle(
label=Content.from_markup("[red]original[/red]"),
collapsed_symbol="+",
expanded_symbol="---",
collapsed=False,
)
assert title.content.is_same(Content.assemble("---", " ", Content.from_markup("[red]original[/red]")))
app = App()
async with app.run_test() as pilot:
await app.mount(title)
await pilot.pause()
revision = title._layout_updates
title.label = Content.from_markup("[blue]original[/blue]")
assert title.content.is_same(
Content.assemble("---", " ", Content.from_markup("[blue]original[/blue]"))
)
assert title._layout_updates == revision
title.collapsed = True
assert title.content.plain == "+ original"
assert title._layout_updates > revision
await pilot.pause()
assert title.region.width == len("+ original") + 2


def get_title(collapsible: Collapsible) -> CollapsibleTitle:
return collapsible.get_child_by_type(CollapsibleTitle)

Expand Down
Loading