segmented_control.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """SegmentedControl from Radix Themes."""
  2. from __future__ import annotations
  3. from collections.abc import Sequence
  4. from types import SimpleNamespace
  5. from typing import ClassVar, Literal
  6. from reflex.components.core.breakpoints import Responsive
  7. from reflex.event import EventHandler
  8. from reflex.vars.base import Var
  9. from ..base import LiteralAccentColor, RadixThemesComponent
  10. def on_value_change(
  11. value: Var[str | list[str]],
  12. ) -> tuple[Var[str | list[str]]]:
  13. """Handle the on_value_change event.
  14. Args:
  15. value: The value of the event.
  16. Returns:
  17. The value of the event.
  18. """
  19. return (value,)
  20. class SegmentedControlRoot(RadixThemesComponent):
  21. """Root element for a SegmentedControl component."""
  22. tag = "SegmentedControl.Root"
  23. # The size of the segmented control: "1" | "2" | "3"
  24. size: Var[Responsive[Literal["1", "2", "3"]]]
  25. # Variant of button: "classic" | "surface"
  26. variant: Var[Literal["classic", "surface"]]
  27. # The type of the segmented control, either "single" for selecting one option or "multiple" for selecting multiple options.
  28. type: Var[Literal["single", "multiple"]]
  29. # Override theme color for button
  30. color_scheme: Var[LiteralAccentColor]
  31. # The radius of the segmented control: "none" | "small" | "medium" | "large" | "full"
  32. radius: Var[Literal["none", "small", "medium", "large", "full"]]
  33. # The default value of the segmented control.
  34. default_value: Var[str | Sequence[str]]
  35. # The current value of the segmented control.
  36. value: Var[str | Sequence[str]]
  37. # Handles the `onChange` event for the SegmentedControl component.
  38. on_change: EventHandler[on_value_change]
  39. _rename_props = {"onChange": "onValueChange"}
  40. class SegmentedControlItem(RadixThemesComponent):
  41. """An item in the SegmentedControl component."""
  42. tag = "SegmentedControl.Item"
  43. # The value of the item.
  44. value: Var[str]
  45. _valid_parents: ClassVar[list[str]] = ["SegmentedControlRoot"]
  46. class SegmentedControl(SimpleNamespace):
  47. """SegmentedControl components namespace."""
  48. root = staticmethod(SegmentedControlRoot.create)
  49. item = staticmethod(SegmentedControlItem.create)
  50. segmented_control = SegmentedControl()