Skip to content

Commit

Permalink
Ensure ListView.index is correct in all post-mount situations
Browse files Browse the repository at this point in the history
This seeks to fix Textualize#1588 by ensuring that the index property is set to an
acceptable value after any items have been mounted within the ListView, thus
ensuring that an inherited ListView, which uses compose to add items rather
than having them passed via __init__, works as expected.
  • Loading branch information
davep committed Jan 18, 2023
1 parent 76272bc commit b62d14c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/textual/widgets/_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def __init__(
super().__init__(*children, name=name, id=id, classes=classes)
self.index = initial_index

def on_mount(self) -> None:
"""Ensure the ListView is fully-settled after mounting."""
# If someone inherits from ListView, they might be populating it
# from Widget.compose rather than from passing the ListItems to the
# initialisation call. Given that watch_index does all the work,
# let's just tickle it again after the DOM has been spun up and all
# children of this widget will have been mounted.
self.index = self.index or 0

@property
def highlighted_child(self) -> ListItem | None:
"""ListItem | None: The currently highlighted ListItem,
Expand Down

0 comments on commit b62d14c

Please sign in to comment.