From b44983e8d4815e86b4ec664951f4e5a04307a52f Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Sun, 7 May 2023 17:56:02 +0100 Subject: [PATCH] Add an add_options method to the OptionList In doing so, pretty much make the add_option code into the add_options code, and then just have add_option call add_options. See #2507. --- src/textual/widgets/_option_list.py | 36 ++++++++++++++------ tests/option_list/test_option_list_create.py | 4 +++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/textual/widgets/_option_list.py b/src/textual/widgets/_option_list.py index 97744971a..d6e6af537 100644 --- a/src/textual/widgets/_option_list.py +++ b/src/textual/widgets/_option_list.py @@ -7,7 +7,7 @@ forms of bounce-bar menu. 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 @@ -508,6 +508,30 @@ class OptionList(ScrollView, can_focus=True): # 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. + """ + # 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. + options = [item for item in content if isinstance(item, Option)] + if options: + self._options.extend(options) + 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. @@ -520,15 +544,7 @@ class OptionList(ScrollView, can_focus=True): 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. diff --git a/tests/option_list/test_option_list_create.py b/tests/option_list/test_option_list_create.py index ed3239122..043e3e584 100644 --- a/tests/option_list/test_option_list_create.py +++ b/tests/option_list/test_option_list_create.py @@ -106,6 +106,10 @@ 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 async def test_create_with_duplicate_id() -> None: