switch.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """A switch component."""
  2. from typing import Dict
  3. from pynecone.components.component import EVENT_ARG
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.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 pass onChange 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 isDisabled is passed, 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 spacing between the switch and its label text (0.5rem)
  24. spacing: Var[str]
  25. # The placeholder text.
  26. placeholder: Var[str]
  27. # The color scheme of the switch (e.g. "blue", "green", "red", etc.)
  28. color_scheme: Var[str]
  29. def get_controlled_triggers(self) -> Dict[str, Var]:
  30. """Get the event triggers that pass the component's value to the handler.
  31. Returns:
  32. A dict mapping the event trigger to the var that is passed to the handler.
  33. """
  34. return {
  35. "on_change": EVENT_ARG.target.checked,
  36. }