checkbox.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """A checkbox component."""
  2. from __future__ import annotations
  3. from typing import Any, Union
  4. from reflex.components.libs.chakra import ChakraComponent
  5. from reflex.constants import EventTriggers
  6. from reflex.vars import Var
  7. class Checkbox(ChakraComponent):
  8. """The Checkbox component is used in forms when a user needs to select multiple values from several options."""
  9. tag = "Checkbox"
  10. # Color scheme for checkbox.
  11. # Options:
  12. # "whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan"
  13. # | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
  14. color_scheme: Var[str]
  15. # "sm" | "md" | "lg"
  16. size: Var[str]
  17. # If true, the checkbox will be checked.
  18. is_checked: Var[bool]
  19. # If true, the checkbox will be disabled
  20. is_disabled: Var[bool]
  21. # If true and is_disabled is passed, the checkbox will remain tabbable but not interactive
  22. is_focusable: Var[bool]
  23. # If true, the checkbox will be indeterminate. This only affects the icon shown inside checkbox and does not modify the is_checked var.
  24. is_indeterminate: Var[bool]
  25. # If true, the checkbox is marked as invalid. Changes style of unchecked state.
  26. is_invalid: Var[bool]
  27. # If true, the checkbox will be readonly
  28. is_read_only: Var[bool]
  29. # If true, the checkbox input is marked as required, and required attribute will be added
  30. is_required: Var[bool]
  31. # The name of the input field in a checkbox (Useful for form submission).
  32. name: Var[str]
  33. # The spacing between the checkbox and its label text (0.5rem)
  34. spacing: Var[str]
  35. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  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. **super().get_event_triggers(),
  42. EventTriggers.ON_CHANGE: lambda e0: [e0.target.checked],
  43. }
  44. class CheckboxGroup(ChakraComponent):
  45. """A group of checkboxes."""
  46. tag = "CheckboxGroup"
  47. # The value of the checkbox group
  48. value: Var[str]
  49. # The initial value of the checkbox group
  50. default_value: Var[str]
  51. # If true, all wrapped checkbox inputs will be disabled
  52. is_disabled: Var[bool]
  53. # If true, input elements will receive checked attribute instead of isChecked. This assumes, you're using native radio inputs
  54. is_native: Var[bool]