-
Notifications
You must be signed in to change notification settings - Fork 814
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
added insert, pop and remove_items methods to ListView #4384
Merged
willmcgugan
merged 5 commits into
Textualize:main
from
MuongKimhong:extend-listview-functionality
Apr 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,56 @@ def clear(self) -> AwaitRemove: | |
self.index = None | ||
return await_remove | ||
|
||
def insert(self, index: int, items: Iterable[ListItem]) -> AwaitMount: | ||
"""Insert new ListItem(s) to specified index. | ||
|
||
Args: | ||
index: index to insert new ListItem. | ||
items: The ListItems to insert. | ||
|
||
Returns: | ||
An awaitable that yields control to the event loop | ||
until the DOM has been updated with the new child item. | ||
""" | ||
await_mount = self.mount(*items, before=index) | ||
return await_mount | ||
|
||
def pop(self, index: Optional[int]=None) -> AwaitRemove: | ||
"""Remove last ListItem from ListView or | ||
Remove ListItem from ListView by index | ||
|
||
Args: | ||
index: index of ListItem to remove from ListView | ||
|
||
Returns: | ||
An awaitable that yields control to the event loop until | ||
the DOM has been updated to reflect item being removed. | ||
""" | ||
if index is None: | ||
await_remove = self.query("ListItem").last().remove() | ||
else: | ||
await_remove = self.query("ListItem")[index].remove() | ||
return await_remove | ||
|
||
def remove_items(self, indices: Iterable[int]) -> AwaitRemove: | ||
"""Remove ListItems from ListView by indices | ||
|
||
Args: | ||
indices: index(s) of ListItems to remove from ListView | ||
|
||
Returns: | ||
An awaitable object that waits for the direct children to be removed. | ||
""" | ||
if len(self._nodes) == len(indices): | ||
return self.clear() | ||
|
||
items_to_remove = [] | ||
for index in indices: | ||
items_to_remove.append(self.query("ListItem")[index]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could cache that query. Otherwise it would be performed once per loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please have a review |
||
|
||
await_remove = self.app._remove_nodes(items_to_remove, self) | ||
return await_remove | ||
|
||
def action_select_cursor(self) -> None: | ||
"""Select the current item in the list.""" | ||
selected_child = self.highlighted_child | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are two items in the
ListView
and the developer passed[0, 0]
as theindices
this will have unintended consequences.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank for pointing it out! I have removed that block as it's not necessary.