Skip to content

Commit de279e5

Browse files
authored
Support partial as EventHandlerFunction (#1337)
1 parent b68a38a commit de279e5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Don't forget to remove deprecated code on each major release!
2121
- Added support for inline JavaScript as event handlers or other attributes that expect a callable via `reactpy.types.InlineJavaScript`
2222
- Event functions can now call `event.preventDefault()` and `event.stopPropagation()` methods directly on the event data object, rather than using the `@event` decorator.
2323
- Event data now supports accessing properties via dot notation (ex. `event.target.value`).
24+
- Added support for partial functions in EventHandler
2425
- Added `reactpy.types.Event` to provide type hints for the standard `data` function argument (for example `def on_click(event: Event): ...`).
2526
- Added `asgi` and `jinja` installation extras (for example `pip install reactpy[asgi, jinja]`).
2627
- Added `reactpy.executors.asgi.ReactPy` that can be used to run ReactPy in standalone mode via ASGI.

src/reactpy/core/events.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import dis
44
import inspect
55
from collections.abc import Callable, Sequence
6-
from functools import lru_cache
6+
from functools import lru_cache, partial
77
from types import CodeType
88
from typing import Any, Literal, cast, overload
99

@@ -107,6 +107,9 @@ def __init__(
107107
while hasattr(func_to_inspect, "__wrapped__"):
108108
func_to_inspect = func_to_inspect.__wrapped__
109109

110+
if isinstance(func_to_inspect, partial):
111+
func_to_inspect = func_to_inspect.func
112+
110113
found_prevent_default, found_stop_propagation = _inspect_event_handler_code(
111114
func_to_inspect.__code__
112115
)

tests/test_core/test_events.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
import reactpy
4+
from functools import partial
45
from reactpy import component, html
56
from reactpy.core.events import (
67
EventHandler,
@@ -348,6 +349,16 @@ def handler(event: Event):
348349
assert eh.stop_propagation is True
349350

350351

352+
def test_detect_both_when_handler_is_partial():
353+
def handler(event: Event, *, extra_param):
354+
event.preventDefault()
355+
event.stopPropagation()
356+
357+
eh = EventHandler(partial(handler, extra_param="extra_value"))
358+
assert eh.prevent_default is True
359+
assert eh.stop_propagation is True
360+
361+
351362
def test_no_detect():
352363
def handler(event: Event):
353364
pass

0 commit comments

Comments
 (0)