|
| 1 | +"""Custom toggle group component.""" |
| 2 | + |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +from reflex.components.component import Component |
| 6 | +from reflex.event import EventHandler, passthrough_event_spec |
| 7 | +from reflex.utils.imports import ImportVar |
| 8 | +from reflex.vars import Var |
| 9 | + |
| 10 | +from reflex_ui.components.base_ui import PACKAGE_NAME, BaseUIComponent |
| 11 | + |
| 12 | +LiteralOrientation = Literal["horizontal", "vertical"] |
| 13 | + |
| 14 | + |
| 15 | +class ClassNames: |
| 16 | + """Class names for toggle group components.""" |
| 17 | + |
| 18 | + ROOT = "inline-flex items-center gap-1 p-1 rounded-md bg-secondary-3 data-[orientation=vertical]:flex-col data-[disabled]:opacity-50 data-[disabled]:cursor-not-allowed" |
| 19 | + |
| 20 | + |
| 21 | +class ToggleGroupBaseComponent(BaseUIComponent): |
| 22 | + """Base component for toggle group components.""" |
| 23 | + |
| 24 | + library = f"{PACKAGE_NAME}/toggle-group" |
| 25 | + |
| 26 | + @property |
| 27 | + def import_var(self): |
| 28 | + """Return the import variable for the toggle group component.""" |
| 29 | + return ImportVar(tag="ToggleGroup", package_path="", install=False) |
| 30 | + |
| 31 | + |
| 32 | +class ToggleGroupRoot(ToggleGroupBaseComponent): |
| 33 | + """Provides a shared state to a series of toggle buttons.""" |
| 34 | + |
| 35 | + tag = "ToggleGroup" |
| 36 | + |
| 37 | + # The open state of the toggle group represented by an array of the values of all pressed toggle buttons. This is the uncontrolled counterpart of value. |
| 38 | + default_value: Var[list[str | int]] |
| 39 | + |
| 40 | + # The open state of the toggle group represented by an array of the values of all pressed toggle buttons. This is the controlled counterpart of default_value. |
| 41 | + value: Var[list[str | int]] |
| 42 | + |
| 43 | + # Callback fired when the pressed states of the toggle group changes. |
| 44 | + on_value_change: EventHandler[passthrough_event_spec(list[str | int], dict)] |
| 45 | + |
| 46 | + # When false only one item in the group can be pressed. If any item in the group becomes pressed, the others will become unpressed. When true multiple items can be pressed. Defaults to False. |
| 47 | + toggle_multiple: Var[bool] |
| 48 | + |
| 49 | + # Whether the toggle group should ignore user interaction. Defaults to False. |
| 50 | + disabled: Var[bool] |
| 51 | + |
| 52 | + # Whether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys. Defaults to True. |
| 53 | + loop: Var[bool] |
| 54 | + |
| 55 | + # The component orientation (layout flow direction). Defaults to "horizontal". |
| 56 | + orientation: Var[LiteralOrientation] |
| 57 | + |
| 58 | + # The render prop |
| 59 | + render_: Var[Component] |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def create(cls, *children, **props) -> Component: |
| 63 | + """Create the toggle group root component.""" |
| 64 | + props["data-slot"] = "toggle-group" |
| 65 | + cls.set_class_name(ClassNames.ROOT, props) |
| 66 | + return super().create(*children, **props) |
| 67 | + |
| 68 | + |
| 69 | +toggle_group = ToggleGroupRoot.create |
0 commit comments