switch.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """A switch component."""
  2. from __future__ import annotations
  3. from typing import Any, Union
  4. from reflex.components.libs.chakra import ChakraComponent, LiteralColorScheme
  5. from reflex.constants import EventTriggers
  6. from reflex.vars import Var
  7. class Switch(ChakraComponent):
  8. """Toggleable switch component."""
  9. tag = "Switch"
  10. # If true, the switch will be checked. You'll need to set an on_change event handler to update its value (since it is now controlled)
  11. is_checked: Var[bool]
  12. # If true, the switch will be disabled
  13. is_disabled: Var[bool]
  14. # If true and is_disabled prop is set, the switch will remain tabbable but not interactive.
  15. is_focusable: Var[bool]
  16. # If true, the switch is marked as invalid. Changes style of unchecked state.
  17. is_invalid: Var[bool]
  18. # If true, the switch will be readonly
  19. is_read_only: Var[bool]
  20. # If true, the switch will be required
  21. is_required: Var[bool]
  22. # The name of the input field in a switch (Useful for form submission).
  23. name: Var[str]
  24. # The spacing between the switch and its label text (0.5rem)
  25. spacing: Var[str]
  26. # The placeholder text.
  27. placeholder: Var[str]
  28. # The color scheme of the switch (e.g. "blue", "green", "red", etc.)
  29. color_scheme: Var[LiteralColorScheme]
  30. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  31. """Get the event triggers that pass the component's value to the handler.
  32. Returns:
  33. A dict mapping the event trigger to the var that is passed to the handler.
  34. """
  35. return {
  36. **super().get_event_triggers(),
  37. EventTriggers.ON_CHANGE: lambda e0: [e0.target.checked],
  38. }