toggle.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. from typing import Any, Callable, Dict, List, Optional, Union
  2. from ..events import GenericEventArguments
  3. from .choice_element import ChoiceElement
  4. from .mixins.disableable_element import DisableableElement
  5. class Toggle(ChoiceElement, DisableableElement):
  6. def __init__(self,
  7. options: Union[List, Dict], *,
  8. value: Any = None,
  9. on_change: Optional[Callable[..., Any]] = None,
  10. ) -> None:
  11. """Toggle
  12. The options can be specified as a list of values, or as a dictionary mapping values to labels.
  13. After manipulating the options, call `update()` to update the options in the UI.
  14. :param options: a list ['value1', ...] or dictionary `{'value1':'label1', ...}` specifying the options
  15. :param value: the initial value
  16. :param on_change: callback to execute when selection changes
  17. """
  18. super().__init__(tag='q-btn-toggle', options=options, value=value, on_change=on_change)
  19. def _event_args_to_value(self, e: GenericEventArguments) -> Any:
  20. return self._values[e.args]
  21. def _value_to_model_value(self, value: Any) -> Any:
  22. return self._values.index(value) if value in self._values else None