Bladeren bron

Merge pull request #1409 from zauberzeug/set_options

Provide method to set options of any choice element (eg. ui.select)
Falko Schindler 1 jaar geleden
bovenliggende
commit
e02ed3ea00
4 gewijzigde bestanden met toevoegingen van 39 en 0 verwijderingen
  1. 11 0
      nicegui/elements/choice_element.py
  2. 6 0
      tests/screen.py
  3. 11 0
      tests/test_select.py
  4. 11 0
      website/more_documentation/select_documentation.py

+ 11 - 0
nicegui/elements/choice_element.py

@@ -33,3 +33,14 @@ class ChoiceElement(ValueElement):
         self._update_values_and_labels()
         self._update_values_and_labels()
         self._update_options()
         self._update_options()
         super().update()
         super().update()
+
+    def set_options(self, options: Union[List, Dict], *, value: Any = None) -> None:
+        """Set the options of this choice element.
+
+        :param options: The new options.
+        :param value: The new value. If not given, the current value is kept.
+        """
+        self.options = options
+        if value is not None:
+            self.value = value
+        self.update()

+ 6 - 0
tests/screen.py

@@ -141,6 +141,12 @@ class Screen:
     def find_element(self, element: ui.element) -> WebElement:
     def find_element(self, element: ui.element) -> WebElement:
         return self.selenium.find_element(By.ID, f'c{element.id}')
         return self.selenium.find_element(By.ID, f'c{element.id}')
 
 
+    def find_by_class(self, name: str) -> WebElement:
+        return self.selenium.find_element(By.CLASS_NAME, name)
+
+    def find_all_by_class(self, name: str) -> WebElement:
+        return self.selenium.find_elements(By.CLASS_NAME, name)
+
     def find_by_tag(self, name: str) -> WebElement:
     def find_by_tag(self, name: str) -> WebElement:
         return self.selenium.find_element(By.TAG_NAME, name)
         return self.selenium.find_element(By.TAG_NAME, name)
 
 

+ 11 - 0
tests/test_select.py

@@ -75,3 +75,14 @@ def test_changing_options(screen: Screen):
     screen.should_contain('value = 10')
     screen.should_contain('value = 10')
     screen.click('clear')
     screen.click('clear')
     screen.should_contain('value = None')
     screen.should_contain('value = None')
+
+
+def test_set_options(screen:  Screen):
+    s = ui.select([1, 2, 3], value=1)
+    ui.button('Set new options', on_click=lambda: s.set_options([4, 5, 6], value=4))
+
+    screen.open('/')
+    screen.click('Set new options')
+    screen.click('4')
+    screen.should_contain('5')
+    screen.should_contain('6')

+ 11 - 0
website/more_documentation/select_documentation.py

@@ -35,3 +35,14 @@ def more() -> None:
             .classes('w-64')
             .classes('w-64')
         ui.select(names, multiple=True, value=names[:2], label='with chips') \
         ui.select(names, multiple=True, value=names[:2], label='with chips') \
             .classes('w-64').props('use-chips')
             .classes('w-64').props('use-chips')
+
+    @text_demo('Update options', '''
+        Options can be changed with the `options` property.
+        But then you also need to call `update()` afterwards to let the change take effect.
+        `set_options` is a shortcut that does both and works well for lambdas.
+    ''')
+    def update_selection():
+        select = ui.select([1, 2, 3], value=1)
+        with ui.row():
+            ui.button('4, 5, 6', on_click=lambda: select.set_options([4, 5, 6], value=4))
+            ui.button('1, 2, 3', on_click=lambda: select.set_options([1, 2, 3], value=1))