Skip to content

Commit

Permalink
feat: add disable keyword to Select and SelectMultiple components (#173)
Browse files Browse the repository at this point in the history
* enhance: add disable keyword to select and select multiple widgets

see #144

* minor changes based on project convention and feedbacks
  • Loading branch information
lp9052 committed Jun 23, 2023
1 parent a6189b0 commit 9cac720
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions solara/components/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def Select(
value: None = ...,
on_value: Optional[Callable[[Optional[T]], None]] = ...,
dense: bool = ...,
disabled: bool = ...,
classes: List[str] = [],
style: Union[str, Dict[str, str], None] = None,
) -> reacton.core.ValueElement[v.Select, T]:
Expand All @@ -30,6 +31,7 @@ def Select(
value: T = ...,
on_value: Optional[Callable[[T], None]] = ...,
dense: bool = ...,
disabled: bool = ...,
classes: List[str] = [],
style: Union[str, Dict[str, str], None] = None,
) -> reacton.core.ValueElement[v.Select, T]:
Expand All @@ -43,6 +45,7 @@ def Select(
value: solara.Reactive[Optional[T]] = ...,
on_value: Optional[Callable[[Optional[T]], None]] = ...,
dense: bool = ...,
disabled: bool = ...,
classes: List[str] = [],
style: Union[str, Dict[str, str], None] = None,
) -> reacton.core.ValueElement[v.Select, T]:
Expand All @@ -56,6 +59,7 @@ def Select(
value: solara.Reactive[T] = ...,
on_value: Optional[Callable[[T], None]] = None,
dense: bool = ...,
disabled: bool = ...,
classes: List[str] = [],
style: Union[str, Dict[str, str], None] = None,
) -> reacton.core.ValueElement[v.Select, T]:
Expand All @@ -69,6 +73,7 @@ def Select(
value: Union[None, T, solara.Reactive[T], solara.Reactive[Optional[T]]] = None,
on_value: Union[None, Callable[[T], None], Callable[[Optional[T]], None]] = None,
dense: bool = False,
disabled: bool = False,
classes: List[str] = [],
style: Union[str, Dict[str, str], None] = None,
) -> reacton.core.ValueElement[v.Select, T]:
Expand Down Expand Up @@ -96,6 +101,7 @@ def Page():
* `values`: List of values to select from.
* `on_value`: Callback to call when the value changes.
* `dense`: Whether to use a denser style.
* `disabled`: Whether the select widget allow user interaction
* `classes`: List of CSS classes to apply to the select.
* `style`: CSS style to apply to the select.
Expand All @@ -114,6 +120,7 @@ def Page():
items=values,
label=label,
dense=dense,
disabled=disabled,
class_=class_,
style_=style_flat,
),
Expand All @@ -127,6 +134,7 @@ def SelectMultiple(
all_values: List[T],
on_value: Callable[[List[T]], None] = None,
dense: bool = False,
disabled: bool = False,
classes: List[str] = [],
style: Union[str, Dict[str, str], None] = None,
) -> reacton.core.ValueElement[v.Select, List[T]]:
Expand Down Expand Up @@ -154,6 +162,7 @@ def Page():
* `all_values`: List of all values to select from.
* `on_value`: Callback to call when the value changes.
* `dense`: Whether to use a denser style.
* `disabled`: Whether the select widget allow user interaction
* `classes`: List of CSS classes to apply to the select.
* `style`: CSS style to apply to the select.
"""
Expand All @@ -170,6 +179,7 @@ def Page():
label=label,
multiple=True,
dense=False,
disabled=disabled,
class_=class_,
style_=style_flat,
),
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/select_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from unittest.mock import MagicMock

import ipyvuetify as vw

import solara


def test_select():
"""
test select widget
"""
disabled = None
on_value = MagicMock()
on_value_multiple = MagicMock()

@solara.component
def Test():
nonlocal disabled
disabled = solara.use_reactive(False)
solara.Select(label="single", values=["test0", "test1", "test2"], on_value=on_value, disabled=disabled.value)
solara.SelectMultiple(label="multiple", values=[], all_values=["test0", "test1", "test2"], on_value=on_value_multiple, disabled=disabled.value)

_, rc = solara.render(Test(), handle_error=False)
select = rc.find(vw.Select, label="single").widget
select_multi = rc.find(vw.Select, label="multiple").widget

# init
assert not select.disabled
assert not select_multi.disabled

assert select.v_model is None
assert select_multi.v_model == []

# set v_model
select.v_model = "test0"
assert on_value.call_count == 1
assert on_value.call_args[0][0] == "test0"

select_multi.v_model = ["test0", "test1"]
assert on_value_multiple.call_count == 1
assert on_value_multiple.call_args[0][0] == ["test0", "test1"]

# test disable
disabled.set(True)
assert len(rc.find(vw.Select, disabled=True).widgets) == 2

rc.close()

0 comments on commit 9cac720

Please sign in to comment.