switch.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """A switch component."""
  2. from __future__ import annotations
  3. from reflex.components.chakra import ChakraComponent, LiteralColorScheme
  4. from reflex.event import EventHandler
  5. from reflex.vars import Var
  6. class Switch(ChakraComponent):
  7. """Toggleable switch component."""
  8. tag = "Switch"
  9. # 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)
  10. is_checked: Var[bool]
  11. # If true, the switch will be disabled
  12. is_disabled: Var[bool]
  13. # If true and is_disabled prop is set, the switch will remain tabbable but not interactive.
  14. is_focusable: Var[bool]
  15. # If true, the switch is marked as invalid. Changes style of unchecked state.
  16. is_invalid: Var[bool]
  17. # If true, the switch will be readonly
  18. is_read_only: Var[bool]
  19. # If true, the switch will be required
  20. is_required: Var[bool]
  21. # The name of the input field in a switch (Useful for form submission).
  22. name: Var[str]
  23. # The value of the input field when checked (use is_checked prop for a bool)
  24. value: Var[str] = Var.create(True) # type: ignore
  25. # The spacing between the switch and its label text (0.5rem)
  26. spacing: Var[str]
  27. # The placeholder text.
  28. placeholder: Var[str]
  29. # The color scheme of the switch (e.g. "blue", "green", "red", etc.)
  30. color_scheme: Var[LiteralColorScheme]
  31. # Fired when the switch value changes
  32. on_change: EventHandler[lambda e0: [e0.target.checked]]