Skip to content

Commit

Permalink
Add a public read-only parent property to TreeNode
Browse files Browse the repository at this point in the history
See #1397.
  • Loading branch information
davep authored and rodrigogiraoserrao committed Jan 7, 2023
1 parent 2867b09 commit af56d88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Added `TreeNode.parent` -- a read-only property for accessing a node's parent https://github.com/Textualize/textual/issues/1397
- Added public `TreeNode` label access via `TreeNode.label` https://github.com/Textualize/textual/issues/1396

### Changed
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def id(self) -> NodeID:
"""NodeID: Get the node ID."""
return self._id

@property
def parent(self) -> TreeNode[TreeDataType] | None:
"""TreeNode[TreeDataType] | None: The parent of the node."""
return self._parent

@property
def is_expanded(self) -> bool:
"""bool: Check if the node is expanded."""
Expand Down
10 changes: 10 additions & 0 deletions tests/tree/test_tree_node_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from textual.widgets import TreeNode, Tree

def test_tree_node_parent() -> None:
"""It should be possible to access a TreeNode's parent."""
tree = Tree[None]("Anakin")
child = tree.root.add("Leia")
grandchild = child.add("Ben")
assert tree.root.parent is None
assert grandchild.parent == child
assert child.parent == tree.root

0 comments on commit af56d88

Please sign in to comment.