radio.py 3.0 KB

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