Преглед на файлове

#271 introduce password parameter for ui.input

Falko Schindler преди 2 години
родител
ревизия
268031eed5
променени са 2 файла, в които са добавени 40 реда и са изтрити 2 реда
  1. 8 2
      nicegui/elements/input.py
  2. 32 0
      tests/test_input.py

+ 8 - 2
nicegui/elements/input.py

@@ -5,15 +5,21 @@ from .mixins.value_element import ValueElement
 
 
 class Input(ValueElement):
 class Input(ValueElement):
 
 
-    def __init__(self, label: Optional[str] = None, *,
-                 placeholder: Optional[str] = None, value: str = '', on_change: Optional[Callable] = None) -> None:
+    def __init__(self,
+                 label: Optional[str] = None, *,
+                 placeholder: Optional[str] = None,
+                 value: str = '',
+                 password: bool = False,
+                 on_change: Optional[Callable] = None) -> None:
         """Text Input
         """Text Input
 
 
         :param label: displayed label for the text input
         :param label: displayed label for the text input
         :param placeholder: text to show if no value is entered
         :param placeholder: text to show if no value is entered
         :param value: the current value of the text input
         :param value: the current value of the text input
+        :param password: whether to hide the input
         :param on_change: callback to execute when the input is confirmed by leaving the focus
         :param on_change: callback to execute when the input is confirmed by leaving the focus
         """
         """
         super().__init__(tag='q-input', value=value, on_value_change=on_change)
         super().__init__(tag='q-input', value=value, on_value_change=on_change)
         self._props['label'] = label
         self._props['label'] = label
         self._props['placeholder'] = placeholder
         self._props['placeholder'] = placeholder
+        self._props['type'] = 'password' if password else 'text'

+ 32 - 0
tests/test_input.py

@@ -0,0 +1,32 @@
+from selenium.webdriver.common.by import By
+
+from nicegui import ui
+
+from .screen import Screen
+
+
+def test_input(screen: Screen):
+    ui.input('Your name', value='John Doe')
+
+    screen.open('/')
+    screen.should_contain('Your name')
+    element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Your name"]')
+    assert element.get_attribute('type') == 'text'
+    assert element.get_attribute('value') == 'John Doe'
+
+    element.send_keys(' Jr.')
+    assert element.get_attribute('value') == 'John Doe Jr.'
+
+
+def test_password(screen: Screen):
+    ui.input('Your password', value='123456', password=True)
+
+    screen.open('/')
+    screen.should_contain('Your password')
+
+    element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Your password"]')
+    assert element.get_attribute('type') == 'password'
+    assert element.get_attribute('value') == '123456'
+
+    element.send_keys('789')
+    assert element.get_attribute('value') == '123456789'