-
-
Notifications
You must be signed in to change notification settings - Fork 323
Support raw JavaScript events #1289
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
Changes from all commits
13f588a
edf0498
8ae45ce
c11c2c4
db770ac
67cd1e7
36e33e4
61fc1d3
258c4de
5ce5c32
0b14bc8
23a7297
a109a3b
3a159af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -885,6 +885,10 @@ class JsonImportSource(TypedDict): | |
fallback: Any | ||
|
||
|
||
class JavaScript(str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a docstring to explain that this class is a simple way of marking JavaScript code to be executed in by the browser |
||
pass | ||
|
||
|
||
class EventHandlerFunc(Protocol): | ||
"""A coroutine which can handle event data""" | ||
|
||
|
@@ -919,7 +923,7 @@ class EventHandlerType(Protocol): | |
EventHandlerMapping = Mapping[str, EventHandlerType] | ||
"""A generic mapping between event names to their handlers""" | ||
|
||
EventHandlerDict: TypeAlias = dict[str, EventHandlerType] | ||
EventHandlerDict: TypeAlias = dict[str, EventHandlerType | JavaScript] | ||
"""A dict mapping between event names to their handlers""" | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { h, render } from "https://unpkg.com/preact?module"; | ||
import htm from "https://unpkg.com/htm?module"; | ||
|
||
const html = htm.bind(h); | ||
|
||
export function bind(node, config) { | ||
return { | ||
create: (type, props, children) => h(type, props, ...children), | ||
render: (element) => render(element, node), | ||
unmount: () => render(null, node), | ||
}; | ||
} | ||
|
||
// The intention here is that Child components are passed in here so we check that the | ||
// children of "the-parent" are "child-1" through "child-N" | ||
export function Component(props) { | ||
var text = "DEFAULT"; | ||
if (props.setText && typeof props.setText === "function") { | ||
text = props.setText("PREFIX TEXT: "); | ||
} | ||
return html` | ||
<div id="${props.id}"> | ||
${text} | ||
</div> | ||
`; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
assert_reactpy_did_not_log, | ||
poll, | ||
) | ||
from reactpy.types import JavaScript | ||
from reactpy.web.module import NAME_SOURCE, WebModule | ||
|
||
JS_FIXTURES_DIR = Path(__file__).parent / "js_fixtures" | ||
|
@@ -389,6 +390,27 @@ async def test_subcomponent_notation_as_obj_attrs(display: DisplayFixture): | |
assert len(form_label) == 1 | ||
|
||
|
||
async def test_callable_prop_with_javacript(display: DisplayFixture): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another test is needed to see if |
||
module = reactpy.web.module_from_file( | ||
"callable-prop", JS_FIXTURES_DIR / "callable-prop.js" | ||
) | ||
Component = reactpy.web.export(module, "Component") | ||
|
||
@reactpy.component | ||
def App(): | ||
return Component( | ||
{ | ||
"id": "my-div", | ||
"setText": JavaScript('(prefixText) => prefixText + "TEST 123"'), | ||
} | ||
) | ||
|
||
await display.show(lambda: App()) | ||
|
||
my_div = await display.page.wait_for_selector("#my-div", state="attached") | ||
assert await my_div.inner_text() == "PREFIX TEXT: TEST 123" | ||
|
||
|
||
def test_module_from_string(): | ||
reactpy.web.module_from_string("temp", "old") | ||
with assert_reactpy_did_log(r"Existing web module .* will be replaced with"): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might need to change
isinstance(v, str)
totype(v) == str
, since technicallyisinstance(JavaScript(), str)
isTrue
.