checkbox.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """A checkbox 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 Checkbox(ChakraComponent):
  7. """The Checkbox component is used in forms when a user needs to select multiple values from several options."""
  8. tag = "Checkbox"
  9. # Color scheme for checkbox.
  10. # Options:
  11. # "whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan"
  12. # | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
  13. color_scheme: Var[str]
  14. # "sm" | "md" | "lg"
  15. size: Var[str]
  16. # If true, the checkbox will be checked.
  17. is_checked: Var[bool]
  18. # If true, the checkbox will be disabled
  19. is_disabled: Var[bool]
  20. # If true and is_disabled is passed, the checkbox will remain tabbable but not interactive
  21. is_focusable: Var[bool]
  22. # If true, the checkbox will be indeterminate. This only affects the icon shown inside checkbox and does not modify the is_checked var.
  23. is_indeterminate: Var[bool]
  24. # If true, the checkbox is marked as invalid. Changes style of unchecked state.
  25. is_invalid: Var[bool]
  26. # If true, the checkbox will be readonly
  27. is_read_only: Var[bool]
  28. # If true, the checkbox input is marked as required, and required attribute will be added
  29. is_required: Var[bool]
  30. # The name of the input field in a checkbox (Useful for form submission).
  31. name: Var[str]
  32. # The spacing between the checkbox and its label text (0.5rem)
  33. spacing: Var[str]
  34. @classmethod
  35. def get_controlled_triggers(cls) -> Dict[str, Var]:
  36. """Get the event triggers that pass the component's value to the handler.
  37. Returns:
  38. A dict mapping the event trigger to the var that is passed to the handler.
  39. """
  40. return {
  41. "on_change": EVENT_ARG.target.checked,
  42. }
  43. class CheckboxGroup(ChakraComponent):
  44. """A group of checkboxes."""
  45. tag = "CheckboxGroup"
  46. # The value of the checkbox group
  47. value: Var[str]
  48. # The initial value of the checkbox group
  49. default_value: Var[str]
  50. # If true, all wrapped checkbox inputs will be disabled
  51. is_disabled: Var[bool]
  52. # If true, input elements will receive checked attribute instead of isChecked. This assumes, you're using native radio inputs
  53. is_native: Var[bool]