switch.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.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) -> Dict[str, Var]:
  29. """Get the event triggers that pass the component's value to the handler.
  30. Returns:
  31. A dict mapping the event trigger to the var that is passed to the handler.
  32. """
  33. return {
  34. "on_change": EVENT_ARG.target.checked,
  35. }