Browse Source

Support radio button in ElementFilter (#3789)

* Support radio button in ElementFilter

* add support for `ui.toggle`

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
Marcus Lim 7 months ago
parent
commit
26f1d820ac
2 changed files with 31 additions and 2 deletions
  1. 4 2
      nicegui/element_filter.py
  2. 27 0
      tests/test_element_filter.py

+ 4 - 2
nicegui/element_filter.py

@@ -10,7 +10,9 @@ from .elements.mixins.content_element import ContentElement
 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 @@ class ElementFilter(Generic[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

+ 27 - 0
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')