Skip to content

Commit

Permalink
Implementing rest of the changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Sep 11, 2023
1 parent 2244883 commit d555896
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions solara/components/togglebuttons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, Dict, List, Optional, TypeVar, Union, cast, overload

import ipyvuetify as v # type: ignore
import ipyvuetify as v
import reacton
from typing_extensions import Literal

Expand Down Expand Up @@ -50,36 +50,6 @@ def ToggleButtonsSingle(
...


@overload
@solara.value_component(None)
def ToggleButtonsSingle(
value: Union[T, solara.Reactive[T]],
values: List[T] = ...,
children: List[reacton.core.Element] = ...,
on_value: Optional[Callable[[T], None]] = ...,
dense: bool = ...,
mandatory: Literal[True] = ...,
classes: List[str] = ...,
style: Union[str, Dict[str, str], None] = ...,
) -> reacton.core.ValueElement[v.BtnToggle, T]:
...


@overload
@solara.value_component(None)
def ToggleButtonsSingle(
value: Union[Optional[T], solara.Reactive[Optional[T]]] = None,
values: List[T] = ...,
children: List[reacton.core.Element] = ...,
on_value: Optional[Callable[[T], None]] = ...,
dense: bool = ...,
mandatory: Literal[False] = ...,
classes: List[str] = ...,
style: Union[str, Dict[str, str], None] = ...,
) -> reacton.core.ValueElement[v.BtnToggle, T]:
...


@solara.value_component(None)
def ToggleButtonsSingle(
value: Union[None, T, Optional[T], solara.Reactive[T], solara.Reactive[Optional[T]]] = None,
Expand Down Expand Up @@ -146,17 +116,15 @@ def Page():
class_ = _combine_classes(classes)
style_flat = solara.util._flatten_style(style)
# TODO: make type safe
# typing is ignored below due to an issue with the typing of use_reactive; Typechecker expects a different type than is given. If expected type is
# given instead, a similar error shows up in typing of argument.
reactive_value = solara.use_reactive(value, on_value) # type: ignore
children = [solara.Button(label=str(value)) for value in values] + children
values = values + [_get_button_value(button) for button in children] # type: ignore
# When mandatory = True, index should not be None, but we are letting the front-end take care of setting index to 0 because of a bug
# (see https://github.com/widgetti/solara/issues/282)
# TODO: set index to 0 on python side (after #282 is resolved)
index, set_index = solara.use_state_or_update(values.index(reactive_value.value) if reactive_value.value is not None else None, key="index")
# When mandatory = True, index should not be None, but we are letting the front-end take care of setting index to 0 because of a bug
# (see https://github.com/widgetti/solara/issues/282)
# TODO: set index to 0 on python side (after #282 is resolved)
index, set_index = solara.use_state_or_update(values.index(reactive_value.value) if reactive_value.value is not None else None, key="index")

def on_index(index):
set_index(index)
Expand All @@ -172,7 +140,6 @@ def on_index(index):
)


@overload
@overload
@solara.value_component(None)
def ToggleButtonsMultiple(
Expand Down Expand Up @@ -208,7 +175,7 @@ def ToggleButtonsMultiple(
value: Union[Optional[List[T]], solara.Reactive[List[T]], solara.Reactive[Optional[List[T]]]] = [],
values: List[T] = [],
children: List[reacton.core.Element] = [],
on_value: Union[Callable[[Optional[List[T]]], None], None] = None,
on_value: Union[Callable[[List[T]], None], None] = None,
dense: bool = False,
mandatory: bool = False,
classes: List[str] = [],
Expand Down Expand Up @@ -244,7 +211,7 @@ def Page():
"""
class_ = _combine_classes(classes)
style_flat = solara.util._flatten_style(style)
# Similar approach to select.py
# See comment regarding typing issue in ToggleButtonsSingle
reactive_value = solara.use_reactive(value, on_value) # type: ignore
children = [solara.Button(label=str(value)) for value in values] + children
allvalues = values + [_get_button_value(button) for button in children]
Expand Down

0 comments on commit d555896

Please sign in to comment.