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

* Improve Error message for unsupported event trigger

* typo fix

* fix ci

* add tests

* Update reflex/components/component.py

Co-authored-by: Masen Furer <m_github@0x26.net>

* fix typo

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
This commit is contained in:
Elijah Ahianyo 2024-04-23 14:36:26 -07:00 committed by GitHub
parent 25b84107da
commit 8e4d6a4781
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View File

@ -255,6 +255,7 @@ class Component(BaseComponent, ABC):
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", [])
@ -284,6 +285,12 @@ class Component(BaseComponent, ABC):
# 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

View File

@ -1569,3 +1569,28 @@ def test_custom_component_declare_event_handlers_in_fields():
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"))