-
-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create closure type; allow async event handlers in props; allow short…
… hand event handlers (#2437) * create closure type; allow async event handlers in props; allow shorthand event handlers * test forwarding event handlers with the shorthand syntax * fix clippy * fix imports in spawn async doctest
- Loading branch information
Showing
11 changed files
with
294 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use dioxus::prelude::*; | ||
|
||
// This test just checks that event handlers compile without explicit type annotations | ||
// It will not actually run any code | ||
#[test] | ||
#[allow(unused)] | ||
fn event_handlers_compile() { | ||
fn app() -> Element { | ||
let mut todos = use_signal(String::new); | ||
rsx! { | ||
input { | ||
// Normal event handlers work without explicit type annotations | ||
oninput: move |evt| todos.set(evt.value()), | ||
} | ||
button { | ||
// async event handlers work without explicit type annotations | ||
onclick: |event| async move { | ||
println!("{event:?}"); | ||
}, | ||
} | ||
|
||
// New! You can now use async closures for custom event handlers! | ||
// This shouldn't require an explicit type annotation | ||
TakesEventHandler { onclick: |event| async move { | ||
println!("{event:?}"); | ||
} } | ||
// Or you can accept a callback that returns a value | ||
// This shouldn't require an explicit type annotation | ||
TakesEventHandlerWithArg { double: move |value| (value * 2) as i32 } | ||
} | ||
} | ||
|
||
#[component] | ||
fn TakesEventHandler(onclick: EventHandler<MouseEvent>) -> Element { | ||
rsx! { | ||
button { | ||
// You can pass in EventHandlers directly to events | ||
onclick: onclick, | ||
"Click!" | ||
} | ||
button { | ||
// Or use the shorthand syntax | ||
onclick, | ||
"Click!" | ||
} | ||
|
||
// You should also be able to forward event handlers to other components with the shorthand syntax | ||
TakesEventHandler { | ||
onclick | ||
} | ||
} | ||
} | ||
|
||
#[component] | ||
fn TakesEventHandlerWithArg(double: Callback<u32, i32>) -> Element { | ||
let mut count = use_signal(|| 2); | ||
rsx! { | ||
button { | ||
// Callbacks let you easily inject custom logic into your components | ||
onclick: move |_| count.set(double(count()) as u32), | ||
"{count}" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.