瀏覽代碼

first hacky fix for #102

Rodja Trappe 2 年之前
父節點
當前提交
9e46bae091
共有 2 個文件被更改,包括 50 次插入5 次删除
  1. 10 0
      nicegui/elements/select.py
  2. 40 5
      tests/test_bindings.py

+ 10 - 0
nicegui/elements/select.py

@@ -1,3 +1,4 @@
+from dataclasses import dataclass
 from typing import Any, Callable, Dict, List, Optional, Union
 
 import justpy as jp
@@ -5,6 +6,11 @@ import justpy as jp
 from .choice_element import ChoiceElement
 
 
+@dataclass(frozen=True, eq=True)
+class Wrapper:
+    value: tuple
+
+
 class Select(ChoiceElement):
 
     def __init__(self, options: Union[List, Dict], *,
@@ -20,6 +26,8 @@ class Select(ChoiceElement):
         super().__init__(view, options, value=value, on_change=on_change)
 
     def value_to_view(self, value: Any):
+        if isinstance(value, list):
+            value = tuple(value)
         matches = [o for o in self.view.options if o['value'] == value]
         if any(matches):
             return matches[0]['label']
@@ -29,4 +37,6 @@ class Select(ChoiceElement):
     def handle_change(self, msg: Dict):
         msg['label'] = msg['value']['label']
         msg['value'] = msg['value']['value']
+        if isinstance(self.view.options[0]['value'], tuple) and isinstance(msg['value'], list):
+            msg['value'] = tuple(msg['value'])
         return super().handle_change(msg)

+ 40 - 5
tests/test_bindings.py

@@ -1,18 +1,15 @@
 
-import pytest
 from nicegui import ui
-from selenium.webdriver.common.action_chains import ActionChains
-from selenium.webdriver.common.by import By
 
 from .screen import Screen
 
 
-def test_binding_ui_select_with_tuple_as_key(screen: Screen):
+def test_ui_select_with_tuple_as_key(screen: Screen):
     class Model():
         selection = None
     data = Model()
     options = {
-        (1, 1): 'option A',
+        (2, 1): 'option A',
         (1, 2): 'option B',
     }
     data.selection = list(options.keys())[0]
@@ -24,3 +21,41 @@ def test_binding_ui_select_with_tuple_as_key(screen: Screen):
     screen.click_at_position(element, x=20, y=100)
     screen.wait(0.3)
     screen.should_contain('option B')
+    screen.should_not_contain('option A')
+    assert data.selection == (1, 2)
+
+
+def test_ui_select_with_list_of_tuples(screen: Screen):
+    class Model():
+        selection = None
+    data = Model()
+    options = [(1, 1), (2, 2), (3, 3)]
+    data.selection = options[0]
+    ui.select(options).bind_value(data, 'selection')
+
+    screen.open('/')
+    screen.should_not_contain('2,2')
+    element = screen.click('1,1')
+    screen.click_at_position(element, x=20, y=100)
+    screen.wait(0.3)
+    screen.should_contain('2,2')
+    screen.should_not_contain('1,1')
+    assert data.selection == (2, 2)
+
+
+def test_ui_select_with_list_of_lists(screen: Screen):
+    class Model():
+        selection = None
+    data = Model()
+    options = [[1, 1], [2, 2], [3, 3]]
+    data.selection = options[0]
+    ui.select(options).bind_value(data, 'selection')
+
+    screen.open('/')
+    screen.should_not_contain('2,2')
+    element = screen.click('1,1')
+    screen.click_at_position(element, x=20, y=100)
+    screen.wait(0.3)
+    screen.should_contain('2,2')
+    screen.should_not_contain('1,1')
+    assert data.selection == [2, 2]