checkbox.py 2.2 KB

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