Add a public read-only parent property to TreeNode

See #1397.
This commit is contained in:
Dave Pearson
2023-01-05 10:35:25 +00:00
parent aaad1a310a
commit 27a7cfc489
3 changed files with 23 additions and 2 deletions

View File

@@ -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 `TreeNode.parent` -- a read-only property for accessing a node's parent https://github.com/Textualize/textual/issues/1397
## [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

View File

@@ -121,6 +121,11 @@ class TreeNode(Generic[TreeDataType]):
"""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."""

View File

@@ -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