color_input.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from typing import Callable, Dict, Optional
  2. from nicegui import ui
  3. from ..element import Element
  4. from ..events import ValueChangeEventArguments, handle_event
  5. from .mixins.value_element import ValueElement
  6. class ColorInput(ValueElement):
  7. def __init__(self, label: str = None, *,
  8. placeholder: str = None, value: str = '', on_change: Optional[Callable] = None) -> None:
  9. """Color Input
  10. :param label: displayed label for the color input
  11. :param placeholder: text to show if no color is selected
  12. :param value: the current color value
  13. :param on_change: callback to execute when the input is confirmed by leaving the focus
  14. """
  15. super().__init__(tag='q-input', value=value, on_value_change=on_change)
  16. self._props['label'] = label
  17. self._props['placeholder'] = placeholder
  18. with self, ui.menu() as self.popup:
  19. def handle_change(msg: Dict) -> None:
  20. arguments = ValueChangeEventArguments(sender=self, client=self.client, value=msg['args'])
  21. handle_event(on_change, arguments)
  22. self.color_element = Element('q-color').on('change', handle_change)
  23. with self.add_slot('append'):
  24. self.button = ui.button(on_click=self.popup.open) \
  25. .props('icon=colorize flat round', remove='color').classes('cursor-pointer')
  26. def open(self) -> None:
  27. self.popup.open()
  28. def close(self) -> None:
  29. self.popup.close()