Skip to content

Commit 1ee368d

Browse files
authored
improve logic for icon name handling (#4988)
1 parent ff0e698 commit 1ee368d

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

reflex/components/lucide/icon.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,35 @@ def create(cls, *children, **props) -> Component:
5353
if "tag" not in props:
5454
raise AttributeError("Missing 'tag' keyword-argument for Icon")
5555

56-
tag: str | Var | LiteralVar = Var.create(props.pop("tag"))
57-
if isinstance(tag, LiteralVar):
58-
if isinstance(tag, LiteralStringVar):
59-
tag = tag._var_value
56+
tag_var: Var | LiteralVar = Var.create(props.pop("tag"))
57+
if isinstance(tag_var, LiteralVar):
58+
if isinstance(tag_var, LiteralStringVar):
59+
tag = format.to_snake_case(tag_var._var_value.lower())
6060
else:
61-
raise TypeError(f"Icon name must be a string, got {type(tag)}")
62-
elif isinstance(tag, Var):
63-
tag_stringified = tag.guess_type()
61+
raise TypeError(f"Icon name must be a string, got {type(tag_var)}")
62+
elif isinstance(tag_var, Var):
63+
tag_stringified = tag_var.guess_type()
6464
if not isinstance(tag_stringified, StringVar):
65-
raise TypeError(f"Icon name must be a string, got {tag._var_type}")
65+
raise TypeError(f"Icon name must be a string, got {tag_var._var_type}")
6666
return DynamicIcon.create(name=tag_stringified.replace("_", "-"), **props)
6767

68-
if (
69-
not isinstance(tag, str)
70-
or format.to_snake_case(tag) not in LUCIDE_ICON_LIST
71-
):
72-
if isinstance(tag, str):
73-
icons_sorted = sorted(
74-
LUCIDE_ICON_LIST,
75-
key=lambda s, tag=tag: format.length_of_largest_common_substring(
76-
tag, s
77-
),
78-
reverse=True,
79-
)
80-
else:
81-
icons_sorted = LUCIDE_ICON_LIST
68+
if tag not in LUCIDE_ICON_LIST:
69+
icons_sorted = sorted(
70+
LUCIDE_ICON_LIST,
71+
key=lambda s, tag=tag: format.length_of_largest_common_substring(
72+
tag, s
73+
),
74+
reverse=True,
75+
)
8276
console.warn(
8377
f"Invalid icon tag: {tag}. Please use one of the following: {', '.join(icons_sorted[0:10])}, ..."
8478
"\nSee full list at https://reflex.dev/docs/library/data-display/icon/#icons-list. Using 'circle-help' icon instead."
8579
)
86-
tag = "circle-help"
80+
tag = "circle_help"
8781

88-
if tag in LUCIDE_ICON_MAPPING_OVERRIDE:
89-
props["tag"] = LUCIDE_ICON_MAPPING_OVERRIDE[tag]
90-
else:
91-
props["tag"] = format.to_title_case(format.to_snake_case(tag)) + "Icon"
82+
props["tag"] = LUCIDE_ICON_MAPPING_OVERRIDE.get(
83+
tag, format.to_title_case(tag) + "Icon"
84+
)
9285
props["alias"] = f"Lucide{props['tag']}"
9386
props.setdefault("color", "var(--current-color)")
9487
return super().create(**props)

0 commit comments

Comments
 (0)