mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Allow zero items to be passed to add_options and do zero work
This commit is contained in:
@@ -520,16 +520,19 @@ class OptionList(ScrollView, can_focus=True):
|
|||||||
Raises:
|
Raises:
|
||||||
DuplicateID: If there is an attempt to use a duplicate ID.
|
DuplicateID: If there is an attempt to use a duplicate ID.
|
||||||
"""
|
"""
|
||||||
# Turn any incoming values into valid content for the list.
|
# Only work if we have items to add; but don't make a fuss out of
|
||||||
content = [self._make_content(item) for item in items]
|
# zero items to add, just carry on like nothing happened.
|
||||||
self._contents.extend(content)
|
if items:
|
||||||
# Pull out the content that is genuine options and add them to the
|
# Turn any incoming values into valid content for the list.
|
||||||
# list of options.
|
content = [self._make_content(item) for item in items]
|
||||||
options = [item for item in content if isinstance(item, Option)]
|
self._contents.extend(content)
|
||||||
if options:
|
# Pull out the content that is genuine options and add them to the
|
||||||
self._options.extend(options)
|
# list of options.
|
||||||
self._refresh_content_tracking(force=True)
|
options = [item for item in content if isinstance(item, Option)]
|
||||||
self.refresh()
|
if options:
|
||||||
|
self._options.extend(options)
|
||||||
|
self._refresh_content_tracking(force=True)
|
||||||
|
self.refresh()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def add_option(self, item: NewOptionListContent = None) -> Self:
|
def add_option(self, item: NewOptionListContent = None) -> Self:
|
||||||
|
|||||||
@@ -110,6 +110,10 @@ async def test_add_later() -> None:
|
|||||||
[Option("more still"), "Yet more options", "so many options!"]
|
[Option("more still"), "Yet more options", "so many options!"]
|
||||||
)
|
)
|
||||||
assert option_list.option_count == 10
|
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:
|
async def test_create_with_duplicate_id() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user