Skip to content

Commit 0ef3834

Browse files
committed
simplify match and fix some bugs in it
1 parent 13d026f commit 0ef3834

File tree

11 files changed

+179
-149
lines changed

11 files changed

+179
-149
lines changed

reflex/compiler/templates.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def render(component: Mapping[str, Any] | str) -> str:
5656
return component
5757
if "iterable" in component:
5858
return _RenderUtils.render_iterable_tag(component)
59-
if component.get("name") == "match":
59+
if "match_cases" in component:
6060
return _RenderUtils.render_match_tag(component)
61-
if "cond" in component:
61+
if "cond_state" in component:
6262
return _RenderUtils.render_condition_tag(component)
6363
if (contents := component.get("contents")) is not None:
6464
return contents
@@ -90,15 +90,15 @@ def render_iterable_tag(component: Any) -> str:
9090
@staticmethod
9191
def render_match_tag(component: Any) -> str:
9292
cases_code = ""
93-
for case in component.get("match_cases", []):
94-
for condition in case[:-1]:
95-
cases_code += f" case JSON.stringify({condition._js_expr}):\n"
96-
cases_code += f""" return {_RenderUtils.render(case[-1])};
93+
for conditions, return_value in component["match_cases"]:
94+
for condition in conditions:
95+
cases_code += f" case JSON.stringify({condition}):\n"
96+
cases_code += f""" return {_RenderUtils.render(return_value)};
9797
break;
9898
"""
9999

100100
return f"""(() => {{
101-
switch (JSON.stringify({component["cond"]._js_expr})) {{
101+
switch (JSON.stringify({component["cond"]})) {{
102102
{cases_code} default:
103103
return {_RenderUtils.render(component["default"])};
104104
break;
@@ -107,16 +107,17 @@ def render_match_tag(component: Any) -> str:
107107

108108
@staticmethod
109109
def get_import(module: _ImportDict) -> str:
110-
if module.get("default") and module.get("rest"):
111-
rest_imports = ",".join(sorted(module["rest"]))
112-
return (
113-
f'import {module["default"]}, {{{rest_imports}}} from "{module["lib"]}"'
114-
)
115-
if module.get("default"):
116-
return f'import {module["default"]} from "{module["lib"]}"'
117-
if module.get("rest"):
118-
rest_imports = ",".join(sorted(module["rest"]))
119-
return f'import {{{rest_imports}}} from "{module["lib"]}"'
110+
default_import = module["default"]
111+
rest_imports = module["rest"]
112+
113+
if default_import and rest_imports:
114+
rest_imports_str = ",".join(sorted(rest_imports))
115+
return f'import {default_import}, {{{rest_imports_str}}} from "{module["lib"]}"'
116+
if default_import:
117+
return f'import {default_import} from "{module["lib"]}"'
118+
if rest_imports:
119+
rest_imports_str = ",".join(sorted(rest_imports))
120+
return f'import {{{rest_imports_str}}} from "{module["lib"]}"'
120121
return f'import "{module["lib"]}"'
121122

122123

reflex/components/component.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,27 +2817,30 @@ def render_dict_to_var(tag: dict | Component | str) -> Var:
28172817
func,
28182818
)
28192819

2820-
if tag["name"] == "match":
2821-
element = tag["cond"]
2820+
if "match_cases" in tag:
2821+
element = Var(tag["cond"])
28222822

28232823
conditionals = render_dict_to_var(tag["default"])
28242824

28252825
for case in tag["match_cases"][::-1]:
2826-
condition = case[0].to_string() == element.to_string()
2827-
for pattern in case[1:-1]:
2828-
condition = condition | (pattern.to_string() == element.to_string())
2826+
conditions, return_value = case
2827+
condition = Var.create(False)
2828+
for pattern in conditions:
2829+
condition = condition | (
2830+
Var(pattern).to_string() == element.to_string()
2831+
)
28292832

28302833
conditionals = ternary_operation(
28312834
condition,
2832-
render_dict_to_var(case[-1]),
2835+
render_dict_to_var(return_value),
28332836
conditionals,
28342837
)
28352838

28362839
return conditionals
28372840

2838-
if "cond" in tag:
2841+
if "cond_state" in tag:
28392842
return ternary_operation(
2840-
tag["cond"],
2843+
Var(tag["cond_state"]),
28412844
render_dict_to_var(tag["true_value"]),
28422845
render_dict_to_var(tag["false_value"])
28432846
if tag["false_value"] is not None

reflex/components/core/cond.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def create(
6161

6262
def _render(self) -> Tag:
6363
return CondTag(
64-
cond=self.cond,
64+
cond_state=str(self.cond),
6565
true_value=self.children[0].render(),
6666
false_value=self.children[1].render(),
6767
)
@@ -72,17 +72,11 @@ def render(self) -> dict:
7272
Returns:
7373
The dictionary for template of component.
7474
"""
75-
tag = self._render()
76-
return dict(
77-
tag.add_props(
78-
**self.event_triggers,
79-
key=self.key,
80-
sx=self.style,
81-
id=self.id,
82-
class_name=self.class_name,
83-
),
84-
cond_state=str(self.cond),
85-
)
75+
return {
76+
"cond_state": str(self.cond),
77+
"true_value": self.children[0].render(),
78+
"false_value": self.children[1].render(),
79+
}
8680

8781
def add_imports(self) -> ImportDict:
8882
"""Add imports for the Cond component.

0 commit comments

Comments
 (0)