input.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from typing import Any, Callable, Dict, Optional
  2. from .icon import Icon
  3. from .mixins.value_element import ValueElement
  4. class Input(ValueElement):
  5. LOOPBACK = False
  6. def __init__(self,
  7. label: Optional[str] = None, *,
  8. placeholder: Optional[str] = None,
  9. value: str = '',
  10. password: bool = False,
  11. password_toggle_button: bool = False,
  12. on_change: Optional[Callable] = None,
  13. validation: Dict[str, Callable] = {}) -> None:
  14. """Text Input
  15. This element is based on Quasar's `QInput <https://quasar.dev/vue-components/input>`_ component.
  16. The `on_change` event is called on every keystroke and the value updates accordingly.
  17. If you want to wait until the user confirms the input, you can register a custom event callback, e.g.
  18. `ui.input(...).on('keydown.enter', ...)` or `ui.input(...).on('blur', ...)`.
  19. You can use the `validation` parameter to define a dictionary of validation rules.
  20. The key of the first rule that fails will be displayed as an error message.
  21. :param label: displayed label for the text input
  22. :param placeholder: text to show if no value is entered
  23. :param value: the current value of the text input
  24. :param password: whether to hide the input (default: False)
  25. :param password_toggle_button: whether to show a button to toggle the password visibility (default: False)
  26. :param on_change: callback to execute when the input is confirmed by leaving the focus
  27. :param validation: dictionary of validation rules, e.g. ``{'Too short!': lambda value: len(value) < 3}``
  28. """
  29. super().__init__(tag='q-input', value=value, on_value_change=on_change)
  30. if label is not None:
  31. self._props['label'] = label
  32. if placeholder is not None:
  33. self._props['placeholder'] = placeholder
  34. self._props['type'] = 'password' if password else 'text'
  35. if password_toggle_button:
  36. with self.add_slot('append'):
  37. def toggle_type(_):
  38. is_hidden = self._props.get('type') == 'password'
  39. icon.props(f'name={"visibility" if is_hidden else "visibility_off"}')
  40. self.props(f'type={"text" if is_hidden else "password"}')
  41. icon = Icon('visibility_off').classes('cursor-pointer').on('click', toggle_type)
  42. self.validation = validation
  43. def on_value_change(self, value: Any) -> None:
  44. super().on_value_change(value)
  45. for message, check in self.validation.items():
  46. if not check(value):
  47. self.props(f'error error-message="{message}"')
  48. break
  49. else:
  50. self.props(remove='error')