radio.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """A radio component."""
  2. from typing import Any, List, Set
  3. from pynecone import utils
  4. from pynecone.components.component import Component
  5. from pynecone.components.layout.foreach import Foreach
  6. from pynecone.components.libs.chakra import ChakraComponent
  7. from pynecone.components.typography.text import Text
  8. from pynecone.var import Var
  9. class RadioGroup(ChakraComponent):
  10. """A grouping of individual radio options."""
  11. tag = "RadioGroup"
  12. # State var to bind the the input.
  13. value: Var[Any]
  14. @classmethod
  15. def get_controlled_triggers(cls) -> Set[str]:
  16. """Get the event triggers that pass the component's value to the handler.
  17. Returns:
  18. The controlled event triggers.
  19. """
  20. return {"on_change"}
  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 utils._issubclass(children[0].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)