input.py 3.2 KB

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