radio.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """A radio component."""
  2. from typing import Any, Dict, List, Union
  3. from reflex.components.component import Component
  4. from reflex.components.layout.foreach import Foreach
  5. from reflex.components.libs.chakra import ChakraComponent
  6. from reflex.components.typography.text import Text
  7. from reflex.constants import EventTriggers
  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: str = "RadioGroup"
  13. # State var to bind the 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. def get_event_triggers(self) -> Dict[str, Union[Var, Any]]:
  20. """Get the event triggers that pass the component's value to the handler.
  21. Returns:
  22. A dict mapping the event trigger to the var that is passed to the handler.
  23. """
  24. return {
  25. **super().get_event_triggers(),
  26. EventTriggers.ON_CHANGE: lambda e0: [e0],
  27. }
  28. @classmethod
  29. def create(cls, *children, **props) -> Component:
  30. """Create a radio group component.
  31. Args:
  32. *children: The children of the component.
  33. **props: The props of the component.
  34. Returns:
  35. The component.
  36. """
  37. if len(children) == 1 and isinstance(children[0], list):
  38. children = [Radio.create(child) for child in children[0]]
  39. if (
  40. len(children) == 1
  41. and isinstance(children[0], Var)
  42. and _issubclass(children[0]._var_type, List)
  43. ):
  44. children = [Foreach.create(children[0], lambda item: Radio.create(item))]
  45. return super().create(*children, **props)
  46. class Radio(Text):
  47. """Radios are used when only one choice may be selected in a series of options."""
  48. tag: str = "Radio"
  49. # Value of radio.
  50. value: Var[Any]
  51. # The default value.
  52. default_value: Var[Any]
  53. # The color scheme.
  54. color_scheme: Var[str]
  55. # If true, the radio will be initially checked.
  56. default_checked: Var[bool]
  57. # If true, the radio will be checked. You'll need to pass onChange to update its value (since it is now controlled)
  58. is_checked: Var[bool]
  59. # If true, the radio will be disabled.
  60. is_disabled: Var[bool]
  61. # If true, the radio button will be invalid. This also sets `aria-invalid` to true.
  62. is_invalid: Var[bool]
  63. # If true, the radio will be read-only
  64. is_read_only: Var[bool]
  65. # If true, the radio button will be required. This also sets `aria-required` to true.
  66. is_required: Var[bool]
  67. @classmethod
  68. def create(cls, *children, **props) -> Component:
  69. """Create a radio component.
  70. By default, the value is bound to the first child.
  71. Args:
  72. *children: The children of the component.
  73. **props: The props of the component.
  74. Returns:
  75. The radio component.
  76. """
  77. if "value" not in props:
  78. assert len(children) == 1
  79. props["value"] = children[0]
  80. return super().create(*children, **props)