Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public access (both get and set) to the label of a TreeNode #1487

Merged
merged 5 commits into from
Jan 6, 2023
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
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.10.0] - Unreleased

### Added

- Added public `TreeNode` label access via `TreeNode.label` https://github.com/Textualize/textual/issues/1396

### Changed

- `MouseScrollUp` and `MouseScrollDown` now inherit from `MouseEvent` and have attached modifier keys. https://github.com/Textualize/textual/pull/1458
Expand All @@ -29,8 +33,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Widget.render_line now returns a Strip
- Fix for slow updates on Windows
- Bumped Rich dependency
- Bumped Rich dependency

## [0.8.2] - 2022-12-28

### Fixed
Expand Down
11 changes: 10 additions & 1 deletion src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
self._tree = tree
self._parent = parent
self._id = id
self._label = label
self._label = tree.process_label(label)
self.data = data
self._expanded = expanded
self._children: list[TreeNode] = []
Expand Down Expand Up @@ -163,6 +163,15 @@ def toggle(self) -> None:
self._updates += 1
self._tree._invalidate()

@property
def label(self) -> TextType:
"""TextType: The label for the node."""
return self._label

@label.setter
def label(self, new_label: TextType) -> None:
self.set_label(new_label)

def set_label(self, label: TextType) -> None:
"""Set a new label for the node.

Expand Down
17 changes: 17 additions & 0 deletions tests/tree/test_tree_node_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from textual.widgets import Tree, TreeNode
from rich.text import Text

def test_tree_node_label() -> None:
"""It should be possible to modify a TreeNode's label."""
node = TreeNode(Tree[None]("Xenomorph Lifecycle"), None, 0, "Facehugger")
assert node.label == Text("Facehugger")
node.label = "Chestbuster"
assert node.label == Text("Chestbuster")

def test_tree_node_label_via_tree() -> None:
"""It should be possible to modify a TreeNode's label when created via a Tree."""
tree = Tree[None]("Xenomorph Lifecycle")
node = tree.root.add("Facehugger")
assert node.label == Text("Facehugger")
node.label = "Chestbuster"
assert node.label == Text("Chestbuster")