radio.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """A radio component."""
  2. from typing import Any, List
  3. from reflex.components.chakra import ChakraComponent
  4. from reflex.components.chakra.typography.text import Text
  5. from reflex.components.component import Component
  6. from reflex.components.core.foreach import Foreach
  7. from reflex.event import EventHandler
  8. from reflex.utils.types import _issubclass
  9. from reflex.vars import Var
  10. class RadioGroup(ChakraComponent):
  11. """A grouping of individual radio options."""
  12. tag = "RadioGroup"
  13. # State var to bind the input.
  14. value: Var[Any]
  15. # The default value.
  16. default_value: Var[Any]
  17. # The name of the form field
  18. name: Var[str]
  19. # Fired when the radio group value changes.
  20. on_change: EventHandler[lambda e0: [e0]]
  21. @classmethod
  22. def create(cls, *children, **props) -> Component:
  23. """Create a radio group component.
  24. Args:
  25. *children: The children of the component.
  26. **props: The props of the component.
  27. Returns:
  28. The component.
  29. """
  30. if len(children) == 1 and isinstance(children[0], list):
  31. children = [Radio.create(child) for child in children[0]]
  32. if (
  33. len(children) == 1
  34. and isinstance(children[0], Var)
  35. and _issubclass(children[0]._var_type, List)
  36. ):
  37. children = [Foreach.create(children[0], lambda item: Radio.create(item))]
  38. return super().create(*children, **props)
  39. class Radio(Text):
  40. """Radios are used when only one choice may be selected in a series of options."""
  41. tag = "Radio"
  42. # Value of radio.
  43. value: Var[Any]
  44. # The default value.
  45. default_value: Var[Any]
  46. # The color scheme.
  47. color_scheme: Var[str]
  48. # If true, the radio will be initially checked.
  49. default_checked: Var[bool]
  50. # If true, the radio will be checked. You'll need to pass onChange to update its value (since it is now controlled)
  51. is_checked: Var[bool]
  52. # If true, the radio will be disabled.
  53. is_disabled: Var[bool]
  54. # If true, the radio button will be invalid. This also sets `aria-invalid` to true.
  55. is_invalid: Var[bool]
  56. # If true, the radio will be read-only
  57. is_read_only: Var[bool]
  58. # If true, the radio button will be required. This also sets `aria-required` to true.
  59. is_required: Var[bool]
  60. @classmethod
  61. def create(cls, *children, **props) -> Component:
  62. """Create a radio component.
  63. By default, the value is bound to the first child.
  64. Args:
  65. *children: The children of the component.
  66. **props: The props of the component.
  67. Returns:
  68. The radio component.
  69. """
  70. if "value" not in props:
  71. assert len(children) == 1
  72. props["value"] = children[0]
  73. return super().create(*children, **props)