1
0

switch.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """A switch component."""
  2. from typing import Set
  3. from pynecone.components.component import EVENT_ARG
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.var 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. @classmethod
  28. def get_controlled_triggers(cls) -> Set[str]:
  29. """Get the event triggers that pass the component's value to the handler.
  30. Returns:
  31. The controlled event triggers.
  32. """
  33. return {"on_change"}
  34. @classmethod
  35. def get_controlled_value(cls) -> Var:
  36. """Get the var that is passed to the event handler for controlled triggers.
  37. Returns:
  38. The controlled value.
  39. """
  40. return EVENT_ARG.target.checked