checkbox.py 2.7 KB

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