Skip to content

Commit

Permalink
Button can only accept str/Text as label.
Browse files Browse the repository at this point in the history
Fixes: #1870.
  • Loading branch information
rodrigogiraoserrao committed Feb 24, 2023
1 parent 336f000 commit a4072c4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Scrolling by page now adds to current position.
- Markdown lists have been polished: a selection of bullets, better alignment of numbers, style tweaks https://github.com/Textualize/textual/pull/1832
- Added alternative method of composing Widgets https://github.com/Textualize/textual/pull/1847
- Buttons no longer accept arbitrary renderables https://github.com/Textualize/textual/issues/1870

### Removed

Expand Down
10 changes: 4 additions & 6 deletions src/textual/widgets/_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import cast

import rich.repr
from rich.console import RenderableType
from rich.text import Text, TextType
from typing_extensions import Literal

Expand Down Expand Up @@ -145,7 +144,7 @@ class Button(Static, can_focus=True):
ACTIVE_EFFECT_DURATION = 0.3
"""When buttons are clicked they get the `-active` class for this duration (in seconds)"""

label: reactive[RenderableType] = reactive[RenderableType]("")
label: reactive[TextType] = reactive[TextType]("")
"""The text label that appears within the button."""

variant = reactive("default")
Expand Down Expand Up @@ -209,15 +208,14 @@ def watch_variant(self, old_variant: str, variant: str):
self.remove_class(f"-{old_variant}")
self.add_class(f"-{variant}")

def validate_label(self, label: RenderableType) -> RenderableType:
def validate_label(self, label: TextType) -> TextType:
"""Parse markup for self.label"""
if isinstance(label, str):
return Text.from_markup(label)
return label

def render(self) -> RenderableType:
label = self.label.copy()
label = Text.assemble(" ", label, " ")
def render(self) -> TextType:
label = Text.assemble(" ", self.label, " ")
label.stylize(self.text_style)
return label

Expand Down

0 comments on commit a4072c4

Please sign in to comment.