Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print warning instead of error with invalid icon #4959

Merged
merged 4 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions reflex/components/lucide/icon.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Lucide Icon component."""

from reflex.components.component import Component
from reflex.utils import format
from reflex.utils import console, format
from reflex.utils.imports import ImportVar
from reflex.vars.base import LiteralVar, Var
from reflex.vars.sequence import LiteralStringVar, StringVar
Expand Down Expand Up @@ -33,7 +33,6 @@ def create(cls, *children, **props) -> Component:

Raises:
AttributeError: The errors tied to bad usage of the Icon component.
ValueError: If the icon tag is invalid.
TypeError: If the icon name is not a string.

Returns:
Expand Down Expand Up @@ -73,15 +72,18 @@ def create(cls, *children, **props) -> Component:
if isinstance(tag, str):
icons_sorted = sorted(
LUCIDE_ICON_LIST,
key=lambda s: format.length_of_largest_common_substring(tag, s),
key=lambda s, tag=tag: format.length_of_largest_common_substring(
tag, s
),
reverse=True,
)
else:
icons_sorted = LUCIDE_ICON_LIST
raise ValueError(
f"Invalid icon tag: {tag}. Please use one of the following: {', '.join(icons_sorted[0:25])}, ..."
"\nSee full list at https://reflex.dev/docs/library/data-display/icon/#icons-list."
console.warn(
f"Invalid icon tag: {tag}. Please use one of the following: {', '.join(icons_sorted[0:10])}, ..."
"\nSee full list at https://reflex.dev/docs/library/data-display/icon/#icons-list. Using 'circle-help' icon instead."
)
tag = "circle-help"

if tag in LUCIDE_ICON_MAPPING_OVERRIDE:
props["tag"] = LUCIDE_ICON_MAPPING_OVERRIDE[tag]
Expand Down
1 change: 0 additions & 1 deletion reflex/components/lucide/icon.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class Icon(LucideIconComponent):

Raises:
AttributeError: The errors tied to bad usage of the Icon component.
ValueError: If the icon tag is invalid.
TypeError: If the icon name is not a string.

Returns:
Expand Down
4 changes: 2 additions & 2 deletions tests/units/components/lucide/test_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def test_icon_missing_tag():


def test_icon_invalid_tag():
with pytest.raises(ValueError):
_ = Icon.create("invalid-tag")
invalid = Icon.create("invalid-tag")
assert invalid.tag == "CircleHelpIcon"


def test_icon_multiple_children():
Expand Down
Loading