Skip to content

Commit ff0e698

Browse files
authored
allow cond to mix components and props (#4980)
1 parent 1b509c6 commit ff0e698

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

reflex/components/core/client_side_routing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def wait_for_client_redirect(component: Component) -> Component:
5353
The conditionally rendered component.
5454
"""
5555
return cond(
56-
condition=route_not_found,
57-
c1=component,
58-
c2=ClientSideRouting.create(),
56+
route_not_found,
57+
component,
58+
ClientSideRouting.create(),
5959
)
6060

6161

reflex/components/core/cond.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def create(
3131
cls,
3232
cond: Var,
3333
comp1: BaseComponent,
34-
comp2: BaseComponent | None = None,
34+
comp2: BaseComponent | types.Unset = types.Unset(),
3535
) -> Component:
3636
"""Create a conditional component.
3737
@@ -44,10 +44,14 @@ def create(
4444
The conditional component.
4545
"""
4646
# Wrap everything in fragments.
47-
if type(comp1).__name__ != "Fragment":
47+
if type(comp1) is not Fragment:
4848
comp1 = Fragment.create(comp1)
49-
if comp2 is None or type(comp2).__name__ != "Fragment":
50-
comp2 = Fragment.create(comp2) if comp2 else Fragment.create()
49+
if isinstance(comp2, types.Unset) or type(comp2) is not Fragment:
50+
comp2 = (
51+
Fragment.create(comp2)
52+
if not isinstance(comp2, types.Unset)
53+
else Fragment.create()
54+
)
5155
return Fragment.create(
5256
cls._create(
5357
children=[comp1, comp2],
@@ -96,18 +100,22 @@ def add_imports(self) -> ImportDict:
96100

97101

98102
@overload
99-
def cond(condition: Any, c1: Component, c2: Any) -> Component: ... # pyright: ignore [reportOverlappingOverload]
103+
def cond(condition: Any, c1: Component, c2: Any, /) -> Component: ... # pyright: ignore [reportOverlappingOverload]
104+
105+
106+
@overload
107+
def cond(condition: Any, c1: Component, /) -> Component: ...
100108

101109

102110
@overload
103-
def cond(condition: Any, c1: Component) -> Component: ...
111+
def cond(condition: Any, c1: Any, c2: Component, /) -> Component: ... # pyright: ignore [reportOverlappingOverload]
104112

105113

106114
@overload
107-
def cond(condition: Any, c1: Any, c2: Any) -> Var: ...
115+
def cond(condition: Any, c1: Any, c2: Any, /) -> Var: ...
108116

109117

110-
def cond(condition: Any, c1: Any, c2: Any = None) -> Component | Var:
118+
def cond(condition: Any, c1: Any, c2: Any = types.Unset(), /) -> Component | Var:
111119
"""Create a conditional component or Prop.
112120
113121
Args:
@@ -128,15 +136,15 @@ def cond(condition: Any, c1: Any, c2: Any = None) -> Component | Var:
128136

129137
# If the first component is a component, create a Cond component.
130138
if isinstance(c1, BaseComponent):
131-
if c2 is not None and not isinstance(c2, BaseComponent):
132-
raise ValueError("Both arguments must be components.")
139+
if not isinstance(c2, types.Unset) and not isinstance(c2, BaseComponent):
140+
return Cond.create(cond_var.bool(), c1, Fragment.create(c2))
133141
return Cond.create(cond_var.bool(), c1, c2)
134142

135143
# Otherwise, create a conditional Var.
136144
# Check that the second argument is valid.
137145
if isinstance(c2, BaseComponent):
138-
raise ValueError("Both arguments must be props.")
139-
if c2 is None:
146+
return Cond.create(cond_var.bool(), Fragment.create(c1), c2)
147+
if isinstance(c2, types.Unset):
140148
raise ValueError("For conditional vars, the second argument must be set.")
141149

142150
# convert the truth and false cond parts into vars so the _var_data can be obtained.

tests/units/components/core/test_cond.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ def test_prop_cond(c1: Any, c2: Any):
100100

101101

102102
def test_cond_no_mix():
103-
"""Test if cond can't mix components and props."""
104-
with pytest.raises(ValueError):
105-
cond(True, LiteralVar.create("hello"), Text.create("world"))
103+
"""Test if cond can mix components and props."""
104+
cond(True, LiteralVar.create("hello"), Text.create("world"))
106105

107106

108107
def test_cond_no_else():

0 commit comments

Comments
 (0)