fix value/default_value in accordion (#2616)

* fix value/default_value in accordion

* fix for 3.8 compat

* update comment
This commit is contained in:
Thomas Brandého 2024-02-15 02:06:28 +01:00 committed by GitHub
parent a91987c051
commit 39486386f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 11 deletions

View File

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
from types import SimpleNamespace from types import SimpleNamespace
from typing import Any, Dict, List, Literal, Optional from typing import Any, Dict, List, Literal, Optional, Union
from reflex.components.component import Component from reflex.components.component import Component
from reflex.components.core.match import Match from reflex.components.core.match import Match
@ -16,7 +16,7 @@ from reflex.style import (
format_as_emotion, format_as_emotion,
) )
from reflex.utils import imports from reflex.utils import imports
from reflex.vars import BaseVar, Var, VarData from reflex.vars import BaseVar, Var, VarData, get_unique_variable_name
LiteralAccordionType = Literal["single", "multiple"] LiteralAccordionType = Literal["single", "multiple"]
LiteralAccordionDir = Literal["ltr", "rtl"] LiteralAccordionDir = Literal["ltr", "rtl"]
@ -315,10 +315,10 @@ class AccordionRoot(AccordionComponent):
type_: Var[LiteralAccordionType] type_: Var[LiteralAccordionType]
# The value of the item to expand. # The value of the item to expand.
value: Var[str] value: Var[Optional[Union[str, List[str]]]]
# The default value of the item to expand. # The default value of the item to expand.
default_value: Var[str] default_value: Var[Optional[Union[str, List[str]]]]
# Whether or not the accordion is collapsible. # Whether or not the accordion is collapsible.
collapsible: Var[bool] collapsible: Var[bool]
@ -490,15 +490,19 @@ class AccordionItem(AccordionComponent):
Returns: Returns:
The accordion item. The accordion item.
""" """
# The item requires a value to toggle (use the header as the default value). # The item requires a value to toggle (use a random unique name if not provided).
value = props.pop("value", header if isinstance(header, Var) else str(header)) value = props.pop("value", get_unique_variable_name())
if (header is not None) and (content is not None): if (header is not None) and (content is not None):
children = [ children = [
AccordionHeader.create( AccordionHeader.create(
AccordionTrigger.create( AccordionTrigger.create(
header, header,
Icon.create(tag="chevron_down", class_name="AccordionChevron"), Icon.create(
tag="chevron_down",
class_name="AccordionChevron",
display="inline-block",
),
class_name="AccordionTrigger", class_name="AccordionTrigger",
), ),
), ),

View File

@ -8,7 +8,7 @@ from reflex.vars import Var, BaseVar, ComputedVar
from reflex.event import EventChain, EventHandler, EventSpec from reflex.event import EventChain, EventHandler, EventSpec
from reflex.style import Style from reflex.style import Style
from types import SimpleNamespace from types import SimpleNamespace
from typing import Any, Dict, List, Literal, Optional from typing import Any, Dict, List, Literal, Optional, Union
from reflex.components.component import Component from reflex.components.component import Component
from reflex.components.core.match import Match from reflex.components.core.match import Match
from reflex.components.lucide.icon import Icon from reflex.components.lucide.icon import Icon
@ -20,7 +20,7 @@ from reflex.style import (
format_as_emotion, format_as_emotion,
) )
from reflex.utils import imports from reflex.utils import imports
from reflex.vars import BaseVar, Var, VarData from reflex.vars import BaseVar, Var, VarData, get_unique_variable_name
LiteralAccordionType = Literal["single", "multiple"] LiteralAccordionType = Literal["single", "multiple"]
LiteralAccordionDir = Literal["ltr", "rtl"] LiteralAccordionDir = Literal["ltr", "rtl"]
@ -129,8 +129,12 @@ class AccordionRoot(AccordionComponent):
type_: Optional[ type_: Optional[
Union[Var[Literal["single", "multiple"]], Literal["single", "multiple"]] Union[Var[Literal["single", "multiple"]], Literal["single", "multiple"]]
] = None, ] = None,
value: Optional[Union[Var[str], str]] = None, value: Optional[
default_value: Optional[Union[Var[str], str]] = None, Union[Var[Union[str, List[str]]], Union[str, List[str]]]
] = None,
default_value: Optional[
Union[Var[Union[str, List[str]]], Union[str, List[str]]]
] = None,
collapsible: Optional[Union[Var[bool], bool]] = None, collapsible: Optional[Union[Var[bool], bool]] = None,
disabled: Optional[Union[Var[bool], bool]] = None, disabled: Optional[Union[Var[bool], bool]] = None,
dir: Optional[Union[Var[Literal["ltr", "rtl"]], Literal["ltr", "rtl"]]] = None, dir: Optional[Union[Var[Literal["ltr", "rtl"]], Literal["ltr", "rtl"]]] = None,