Skip to content

Commit

Permalink
feat(listview): add method to append multiple items (#3012)
Browse files Browse the repository at this point in the history
* feat(listview): add method to append multiple items

* update changelog

---------

Co-authored-by: Will McGugan <[email protected]>
  • Loading branch information
TomJGooding and willmcgugan authored Aug 21, 2023
1 parent fc0b5cc commit eccb6e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Added App.begin_capture_print, App.end_capture_print, Widget.begin_capture_print, Widget.end_capture_print https://github.com/Textualize/textual/issues/2952
- Added the ability to run async methods as thread workers https://github.com/Textualize/textual/pull/2938
- Added `ListView.extend` method to append multiple items https://github.com/Textualize/textual/pull/3012
- Added `App.stop_animation` https://github.com/Textualize/textual/issues/2786
- Added `Widget.stop_animation` https://github.com/Textualize/textual/issues/2786

Expand Down
22 changes: 17 additions & 5 deletions src/textual/widgets/_list_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import ClassVar, Optional
from typing import ClassVar, Iterable, Optional

from textual.await_remove import AwaitRemove
from textual.binding import Binding, BindingType
Expand Down Expand Up @@ -172,6 +172,21 @@ def watch_index(self, old_index: int, new_index: int) -> None:
self._scroll_highlighted_region()
self.post_message(self.Highlighted(self, new_child))

def extend(self, items: Iterable[ListItem]) -> AwaitMount:
"""Append multiple new ListItems to the end of the ListView.
Args:
items: The ListItems to append.
Returns:
An awaitable that yields control to the event loop
until the DOM has been updated with the new child items.
"""
await_mount = self.mount(*items)
if len(self) == 1:
self.index = 0
return await_mount

def append(self, item: ListItem) -> AwaitMount:
"""Append a new ListItem to the end of the ListView.
Expand All @@ -182,10 +197,7 @@ def append(self, item: ListItem) -> AwaitMount:
An awaitable that yields control to the event loop
until the DOM has been updated with the new child item.
"""
await_mount = self.mount(item)
if len(self) == 1:
self.index = 0
return await_mount
return self.extend([item])

def clear(self) -> AwaitRemove:
"""Clear all items from the ListView.
Expand Down

0 comments on commit eccb6e5

Please sign in to comment.