Skip to content

Commit cfefa2a

Browse files
authored
ENG-6543: Toggle group component (#21)
1 parent 761e031 commit cfefa2a

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
fail_fast: true
22

33
repos:
4-
- repo: https://github.com/charliermarsh/ruff-pre-commit
5-
rev: v0.11.9
4+
- repo: https://github.com/astral-sh/ruff-pre-commit
5+
rev: v0.12.2
66
hooks:
7-
- id: ruff-format
8-
files: ^reflex_ui/
9-
- id: ruff
7+
- id: ruff-check
108
files: ^reflex_ui/
119
args: ["--fix", "--exit-non-zero-on-fix", "--no-unsafe-fixes"]
10+
- id: ruff-format
11+
files: ^reflex_ui/
1212

1313
- repo: https://github.com/codespell-project/codespell
1414
rev: v2.4.1
@@ -17,7 +17,7 @@ repos:
1717
files: ^reflex_ui/
1818

1919
- repo: https://github.com/RobertCraigie/pyright-python
20-
rev: v1.1.402
20+
rev: v1.1.403
2121
hooks:
2222
- id: pyright
2323
files: ^reflex_ui/

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bump = true
2020
metadata = false
2121

2222
[dependency-groups]
23-
dev = ["pyright==1.1.402", "pre-commit"]
23+
dev = ["pyright==1.1.403", "pre-commit"]
2424

2525
[tool.codespell]
2626
skip = "*.pyi, uv.lock"

reflex_ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"components.base.switch": ["switch"],
2020
"components.base.tabs": ["tabs"],
2121
"components.base.theme_switcher": ["theme_switcher"],
22+
"components.base.toggle_group": ["toggle_group"],
2223
"components.base.toggle": ["toggle"],
2324
"components.base.tooltip": ["tooltip"],
2425
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)