From 26f1d820aca5cc5093521aca19c046387481b3c0 Mon Sep 17 00:00:00 2001 From: Marcus Lim <42759889+marcuslimdw@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:22:00 +0800 Subject: [PATCH] Support radio button in ElementFilter (#3789) * Support radio button in ElementFilter * add support for `ui.toggle` --------- Co-authored-by: Falko Schindler --- nicegui/element_filter.py | 6 ++++-- tests/test_element_filter.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/nicegui/element_filter.py b/nicegui/element_filter.py index b2e2e8206..9a8f588bb 100644 --- a/nicegui/element_filter.py +++ b/nicegui/element_filter.py @@ -10,7 +10,9 @@ from .elements.mixins.source_element import SourceElement from .elements.mixins.text_element import TextElement from .elements.notification import Notification +from .elements.radio import Radio from .elements.select import Select +from .elements.toggle import Toggle T = TypeVar('T', bound=Element) @@ -116,10 +118,10 @@ def __iter__(self) -> Iterator[T]: ) if content] if isinstance(element, Notification): element_contents.append(element.message) - if isinstance(element, Select): + if isinstance(element, (Select, Radio, Toggle)): options = {option['value']: option['label'] for option in element.props.get('options', [])} element_contents.append(options.get(element.value, '')) - if element.is_showing_popup: + if not isinstance(element, Select) or element.is_showing_popup: element_contents.extend(options.values()) if any(all(needle not in str(haystack) for haystack in element_contents) for needle in self._contents): continue diff --git a/tests/test_element_filter.py b/tests/test_element_filter.py index decf8c644..d722d56c8 100644 --- a/tests/test_element_filter.py +++ b/tests/test_element_filter.py @@ -55,6 +55,33 @@ def test_find_content(): assert texts(ElementFilter(content=['A', 'butt'])) == ['button A'] +def test_find_radio(): + radio_list = ui.radio(['radio 1', 'radio 2']) + radio_dict = ui.radio({'radio 1': 'Radio A', 'radio 2': 'Radio B'}) + + assert next(iter(ElementFilter(content=['radio 1']))) is radio_list + assert next(iter(ElementFilter(content=['Radio A']))) is radio_dict + + +def test_find_toggle(): + toggle_list = ui.toggle(['toggle 1', 'toggle 2']) + toggle_dict = ui.toggle({'toggle 1': 'Toggle A', 'toggle 2': 'Toggle B'}) + + assert next(iter(ElementFilter(content=['toggle 1']))) is toggle_list + assert next(iter(ElementFilter(content=['Toggle A']))) is toggle_dict + + +def test_find_select(): + select_list = ui.select(['select 1', 'select 2']) + select_dict = ui.select({'select 1': 'Select A', 'select 2': 'Select B'}) + + select_list._is_showing_popup = True # pylint: disable=protected-access + select_dict._is_showing_popup = True # pylint: disable=protected-access + + assert next(iter(ElementFilter(content=['select 1']))) is select_list + assert next(iter(ElementFilter(content=['Select A']))) is select_dict + + def test_find_marker(): ui.button('button A') ui.button('button B').mark('important')