-
Notifications
You must be signed in to change notification settings - Fork 815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populating a ListView
via constructor vs via inherit and compose
#1588
Labels
bug
Something isn't working
Comments
The quickest of fixes, which I'd like to confirm with @darrenburns first, is this: diff --git a/src/textual/widgets/_list_view.py b/src/textual/widgets/_list_view.py
index 062986cb..1ea7100d 100644
--- a/src/textual/widgets/_list_view.py
+++ b/src/textual/widgets/_list_view.py
@@ -52,6 +52,9 @@ class ListView(Vertical, can_focus=True, can_focus_children=False):
super().__init__(*children, name=name, id=id, classes=classes)
self.index = initial_index
+ def on_mount( self ) -> None:
+ self.index = self.index or 0
+
@property
def highlighted_child(self) -> ListItem | None:
"""ListItem | None: The currently highlighted ListItem, |
davep
added a commit
to davep/textual
that referenced
this issue
Jan 18, 2023
Added to test the fail in Textualize#1588. Any fix for this should cause this test to pass.
davep
added a commit
to davep/textual
that referenced
this issue
Jan 18, 2023
For testing permutations of Textualize#1588.
davep
added a commit
to davep/textual
that referenced
this issue
Jan 18, 2023
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.
Don't forget to star the repository! Follow @textualizeio for Textual updates. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There seems to be a subtle difference in the working of a
ListView
if you create one by passing theListItem
s to it, vs if you create a customListView
by inheriting from it and usingcompose
to populate it. Take the following example code, which places both approaches side-by-side:I feel the
CustomListView
would not be an unusual approach for people to take; perhaps wanting to make their own commonly-used selection list of simple values (or even very complex child values that they want easily handled). Side-by-side theListView
s look the same to start with:Note that the non-inherited
ListView
is to the left and that, even though it doesn't have focus, the first item is highlighted. If you tab into it you can cursor around, etc, just fine. On the other hand notice that the rightListView
(which is aCustomListView
) has no obvious highlighted item and if you tab into it nothing gets highlighted.Further to this, if you (via keyboard) focus the inherited (right hand)
ListView
and then attempt to cursor down, the following exception will be raised:The text was updated successfully, but these errors were encountered: