App for testing event_actions.
()
| 17 | |
| 18 | |
| 19 | def TestEventAction(): |
| 20 | """App for testing event_actions.""" |
| 21 | from typing import Any |
| 22 | |
| 23 | import reflex as rx |
| 24 | |
| 25 | class EventActionState(rx.State): |
| 26 | order: list[str] |
| 27 | |
| 28 | def on_click(self, ev): |
| 29 | self.order.append(f"on_click:{ev}") |
| 30 | |
| 31 | @rx.event |
| 32 | def on_click2(self): |
| 33 | self.order.append("on_click2") |
| 34 | |
| 35 | def on_click_throttle(self): |
| 36 | self.order.append("on_click_throttle") |
| 37 | |
| 38 | def on_click_debounce(self): |
| 39 | self.order.append("on_click_debounce") |
| 40 | |
| 41 | @rx.event |
| 42 | def on_submit(self, form_data: dict[str, Any]): |
| 43 | self.order.append("on_submit") |
| 44 | |
| 45 | class EventFiringComponent(rx.Component): |
| 46 | """A component that fires onClick event without passing DOM event.""" |
| 47 | |
| 48 | tag = "EventFiringComponent" |
| 49 | |
| 50 | def _get_custom_code(self) -> str | None: |
| 51 | return """ |
| 52 | function EventFiringComponent(props) { |
| 53 | return jsx( |
| 54 | "div", |
| 55 | {"id":props.id,"onClick":(e) => props.onClick("foo")}, |
| 56 | "Event Firing Component", |
| 57 | ) |
| 58 | }""" |
| 59 | |
| 60 | @classmethod |
| 61 | def get_event_triggers(cls): |
| 62 | return {"on_click": rx.event.no_args_event_spec} |
| 63 | |
| 64 | def index(): |
| 65 | return rx.vstack( |
| 66 | rx.input( |
| 67 | value=EventActionState.router.session.client_token, |
| 68 | is_read_only=True, |
| 69 | id="token", |
| 70 | ), |
| 71 | rx.button("No events", id="btn-no-events"), |
| 72 | rx.button( |
| 73 | "Stop Prop Only", |
| 74 | id="btn-stop-prop-only", |
| 75 | on_click=rx.stop_propagation, |
| 76 | ), |