From 2789a738c41917d238ca5dccbecc89f89bf59a4e Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Tue, 24 Sep 2024 16:24:19 -0700 Subject: [PATCH] add test case and avoid concat var operation if it's not necessary --- reflex/components/component.py | 3 +++ reflex/vars/sequence.py | 23 +++++++++++++++++++++++ tests/components/test_component.py | 10 ++++++++++ 3 files changed, 36 insertions(+) diff --git a/reflex/components/component.py b/reflex/components/component.py index e4ed1f9230..628ea1ccd3 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -500,6 +500,9 @@ def __init__(self, *args, **kwargs): kwargs["class_name"] = LiteralArrayVar.create( class_name, _var_type=List[str] ).join(" ") + elif isinstance(class_name, str) and class_name: + print("class_name", class_name) + kwargs["class_name"] = LiteralVar.create(class_name) # Construct the component. super().__init__(*args, **kwargs) diff --git a/reflex/vars/sequence.py b/reflex/vars/sequence.py index ddc9ef3c2b..15c7411a6c 100644 --- a/reflex/vars/sequence.py +++ b/reflex/vars/sequence.py @@ -592,6 +592,29 @@ def create( else: return only_string.to(StringVar, only_string._var_type) + if len( + literal_strings := [ + s + for s in filtered_strings_and_vals + if isinstance(s, (str, LiteralStringVar)) + ] + ) == len(filtered_strings_and_vals): + return LiteralStringVar.create( + "".join( + s._var_value if isinstance(s, LiteralStringVar) else s + for s in literal_strings + ), + _var_type=_var_type, + _var_data=VarData.merge( + _var_data, + *( + s._get_all_var_data() + for s in filtered_strings_and_vals + if isinstance(s, Var) + ), + ), + ) + concat_result = ConcatVarOperation.create( *filtered_strings_and_vals, _var_data=_var_data, diff --git a/tests/components/test_component.py b/tests/components/test_component.py index 4dda81896c..73d3f611b2 100644 --- a/tests/components/test_component.py +++ b/tests/components/test_component.py @@ -1288,6 +1288,16 @@ def handler2(self, arg): [FORMATTED_TEST_VAR], id="fstring-class_name", ), + pytest.param( + rx.fragment(class_name=f"foo{TEST_VAR}bar other-class"), + [LiteralVar.create(f"{FORMATTED_TEST_VAR} other-class")], + id="fstring-dual-class_name", + ), + pytest.param( + rx.fragment(class_name=[TEST_VAR, "other-class"]), + [LiteralVar.create([TEST_VAR, "other-class"]).join(" ")], + id="fstring-dual-class_name", + ), pytest.param( rx.fragment(special_props=[TEST_VAR]), [TEST_VAR],