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

[REF-2636]Improve Error message for unsupported event trigger #3147

Merged
merged 6 commits into from
Apr 23, 2024
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
7 changes: 7 additions & 0 deletions reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def __init__(self, *args, **kwargs):

Raises:
TypeError: If an invalid prop is passed.
ValueError: If an event trigger passed is not valid.
"""
# Set the id and children initially.
children = kwargs.get("children", [])
Expand Down Expand Up @@ -284,6 +285,12 @@ def __init__(self, *args, **kwargs):

# Iterate through the kwargs and set the props.
for key, value in kwargs.items():
if key.startswith("on_") and key not in triggers and key not in props:
raise ValueError(
f"The {(comp_name := type(self).__name__)} does not take in an `{key}` event trigger. If {comp_name}"
f" is a third party component make sure to add `{key}` to the component's event triggers. "
f"visit https://reflex.dev/docs/wrapping-react/logic/#event-triggers for more info."
)
if key in triggers:
# Event triggers are bound to event chains.
field_type = EventChain
Expand Down
25 changes: 25 additions & 0 deletions tests/components/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,3 +1569,28 @@ class TestComponent(Component):
parse_args_spec(custom_triggers[trigger_name]),
):
assert v1.equals(v2)


def test_invalid_event_trigger():
class TriggerComponent(Component):
on_push: Var[bool]

def get_event_triggers(self) -> Dict[str, Any]:
"""Test controlled triggers.

Returns:
Test controlled triggers.
"""
return {
**super().get_event_triggers(),
"on_a": lambda: [],
}

trigger_comp = TriggerComponent.create

# test that these do not throw errors.
trigger_comp(on_push=True)
trigger_comp(on_a=rx.console_log("log"))

with pytest.raises(ValueError):
trigger_comp(on_b=rx.console_log("log"))
Loading