Skip to content

Commit

Permalink
Support radio button in ElementFilter (#3789)
Browse files Browse the repository at this point in the history
* Support radio button in ElementFilter

* add support for `ui.toggle`

---------

Co-authored-by: Falko Schindler <[email protected]>
  • Loading branch information
marcuslimdw and falkoschindler authored Sep 27, 2024
1 parent e47ec7a commit 26f1d82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions nicegui/element_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/test_element_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 26f1d82

Please sign in to comment.