Skip to content
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

Add add_options to OptionList #2508

Merged
merged 7 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- run_worker exclusive parameter is now `False` by default https://github.com/Textualize/textual/pull/2470
- Added `always_update` as an optional argument for `reactive.var`

### Added

- Added `OptionList.add_options` https://github.com/Textualize/textual/pull/2508

## [0.23.0] - 2023-05-03

### Fixed
Expand Down
1 change: 0 additions & 1 deletion docs/examples/widgets/option_list_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from textual.app import App, ComposeResult
from textual.widgets import Footer, Header, OptionList
from textual.widgets.option_list import Option, Separator

COLONIES: tuple[tuple[str, str, str, str], ...] = (
("Aerilon", "Demeter", "1.2 Billion", "Gaoth"),
Expand Down
37 changes: 27 additions & 10 deletions src/textual/widgets/_option_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from __future__ import annotations

from typing import ClassVar, NamedTuple
from typing import ClassVar, Iterable, NamedTuple

from rich.console import RenderableType
from rich.repr import Result
Expand Down Expand Up @@ -508,6 +508,31 @@ def _refresh_content_tracking(self, force: bool = False) -> None:
# list, set the virtual size.
self.virtual_size = Size(self.scrollable_content_region.width, len(self._lines))

def add_options(self, items: Iterable[NewOptionListContent]) -> Self:
"""Add new options to the end of the option list.

Args:
items: The new items to add.

Returns:
The `OptionList` instance.

Raises:
DuplicateID: If there is an attempt to use a duplicate ID.
"""
# Only work if we have items to add; but don't make a fuss out of
# zero items to add, just carry on like nothing happened.
if items:
# Turn any incoming values into valid content for the list.
content = [self._make_content(item) for item in items]
self._contents.extend(content)
# Pull out the content that is genuine options and add them to the
# list of options.
self._options.extend([item for item in content if isinstance(item, Option)])
self._refresh_content_tracking(force=True)
self.refresh()
return self

def add_option(self, item: NewOptionListContent = None) -> Self:
"""Add a new option to the end of the option list.

Expand All @@ -520,15 +545,7 @@ def add_option(self, item: NewOptionListContent = None) -> Self:
Raises:
DuplicateID: If there is an attempt to use a duplicate ID.
"""
# Turn any incoming value into valid content for the list.
content = self._make_content(item)
self._contents.append(content)
# If the content is a genuine option, add it to the list of options.
if isinstance(content, Option):
self._options.append(content)
self._refresh_content_tracking(force=True)
self.refresh()
return self
return self.add_options([item])

def _remove_option(self, index: int) -> None:
"""Remove an option from the option list.
Expand Down
8 changes: 8 additions & 0 deletions tests/option_list/test_option_list_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ async def test_add_later() -> None:
assert option_list.option_count == 6
option_list.add_option(Option("even more"))
assert option_list.option_count == 7
option_list.add_options(
[Option("more still"), "Yet more options", "so many options!"]
)
assert option_list.option_count == 10
option_list.add_option(None)
assert option_list.option_count == 10
option_list.add_options([])
assert option_list.option_count == 10


async def test_create_with_duplicate_id() -> None:
Expand Down
Loading