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
  • Loading branch information
davep committed Jan 5, 2023
1 parent aaad1a3 commit 27a7cfc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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 27a7cfc

Please sign in to comment.