mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add public access to a TreeNode's label
This adds public support to reading a TreeNode's label, and also setting it too. See #1396.
This commit is contained in:
10
CHANGELOG.md
10
CHANGELOG.md
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
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
|
||||
|
||||
## [0.9.1] - 2022-12-30
|
||||
|
||||
### Added
|
||||
@@ -23,8 +29,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
|
||||
|
||||
@@ -163,6 +163,17 @@ class TreeNode(Generic[TreeDataType]):
|
||||
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) -> TextType:
|
||||
"""TextType: The label for the node."""
|
||||
self.set_label(new_label)
|
||||
return self.label
|
||||
|
||||
def set_label(self, label: TextType) -> None:
|
||||
"""Set a new label for the node.
|
||||
|
||||
|
||||
17
tests/test_tree.py
Normal file
17
tests/test_tree.py
Normal file
@@ -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")
|
||||
Reference in New Issue
Block a user