Skip to content

Commit

Permalink
Feat: add switch widget (#185)
Browse files Browse the repository at this point in the history
* introduce switch widget

* - add class keyword
- typing style

* add switch to website/pages/init

* add switch to website/pages/init
add switch gif
  • Loading branch information
lp9052 authored Jul 20, 2023
1 parent 5d50522 commit cc7e0cd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions solara/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from .tooltip import Tooltip # noqa: #F401 F403
from .card import Card, CardActions # noqa: #F401 F403
from .spinner import SpinnerSolara # noqa: #F401 F403
from .switch import Switch # noqa: #F401 F403
from .progress import ProgressLinear # noqa: #F401 F403
from .component_vue import _component_vue, component_vue # noqa: #F401 F403
import reacton.core
Expand Down
68 changes: 68 additions & 0 deletions solara/components/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from typing import Callable, Dict, List, Optional, Union

import reacton.ipyvuetify as v

import solara


@solara.value_component(bool)
def Switch(
*,
label: str = None,
value: Union[bool, solara.Reactive[bool]] = True,
on_value: Callable[[bool], None] = None,
disabled: bool = False,
children: list = [],
classes: List[str] = [],
style: Optional[Union[str, Dict[str, str]]] = None,
):
"""A switch component provides users the ability to choose between two distinct values. But aesthetically different from a checkbox.
Basic examples
```solara
import solara
show_message = solara.reactive(True)
disable = solara.reactive(False)
@solara.component
def Page():
with solara.Column():
with solara.Row():
solara.Switch(label="Hide Message", value=show_message, disabled=disable.value)
solara.Switch(label="Disable Message Switch", value=disable)
if show_message.value:
solara.Markdown("## Use Switch to show/hide message")
```
## Arguments
* `label`: The label to display next to the switch.
* `value`: The current value of the switch (True or False).
* `on_value`: A callback that is called when the switch is toggled.
* `disabled`: If True, the switch is disabled and cannot be used.
* `children`: A list of child elements to display on the switch.
* `classes`: Additional CSS classes to apply.
* `style`: CSS style to apply.
"""
reactive_value = solara.use_reactive(value, on_value)
del value, on_value

if label:
children = [label] + children

return v.Switch(
label=label,
v_model=reactive_value.value,
on_v_model=reactive_value.set,
disabled=disabled,
class_=solara.util._combine_classes(classes),
style_=solara.util._flatten_style(style),
children=children,
)
2 changes: 1 addition & 1 deletion solara/website/pages/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"name": "Input",
"icon": "mdi-chevron-left-box",
"pages": ["button", "checkbox", "input", "select", "slider", "togglebuttons", "file_browser", "file_drop"],
"pages": ["button", "checkbox", "input", "select", "slider", "switch", "togglebuttons", "file_browser", "file_drop"],
},
{
"name": "Output",
Expand Down
13 changes: 13 additions & 0 deletions solara/website/pages/api/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""# Switch
"""

import solara
from solara.website.utils import apidoc

from . import NoPage

Page = NoPage


__doc__ += apidoc(solara.Switch.f) # type: ignore
Binary file added solara/website/public/api/switch.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions tests/unit/switch_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from unittest.mock import MagicMock

import ipyvuetify as vw

import solara


def test_switch():
on_value = MagicMock()
el = solara.Switch(label="label", value=True, on_value=on_value)
_, rc = solara.render(el, handle_error=False)
switch = rc.find(vw.Switch)
switch.widget.v_model = False
assert on_value.call_count == 1
switch.widget.v_model = False
assert on_value.call_count == 1
switch.widget.v_model = True
assert on_value.call_count == 2
rc.close()

0 comments on commit cc7e0cd

Please sign in to comment.