radio.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """A radio component."""
  2. from typing import Any, Dict, List, Optional, Union
  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.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 = "RadioGroup"
  13. # State var to bind the the input.
  14. value: Optional[Var[Any]] = None
  15. # The default value.
  16. default_value: Optional[Var[Any]] = None
  17. # The name of the form field
  18. name: Optional[Var[str]] = None
  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 = "Radio"
  49. # Value of radio.
  50. value: Optional[Var[Any]] = None
  51. # The default value.
  52. default_value: Optional[Var[Any]] = None
  53. # The color scheme.
  54. color_scheme: Optional[Var[str]] = None
  55. # If true, the radio will be initially checked.
  56. default_checked: Optional[Var[bool]] = None
  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: Optional[Var[bool]] = None
  59. # If true, the radio will be disabled.
  60. is_disabled: Optional[Var[bool]] = None
  61. # If true, the radio button will be invalid. This also sets `aria-invalid` to true.
  62. is_invalid: Optional[Var[bool]] = None
  63. # If true, the radio will be read-only
  64. is_read_only: Optional[Var[bool]] = None
  65. # If true, the radio button will be required. This also sets `aria-required` to true.
  66. is_required: Optional[Var[bool]] = None
  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)