2022-01-02 07:15:04 +00:00
|
|
|
//! This example roughly shows how events are serialized into Rust from JavaScript.
|
|
|
|
//!
|
|
|
|
//! There is some conversion happening when input types are checkbox/radio/select/textarea etc.
|
|
|
|
|
2022-11-16 00:05:22 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-01-02 07:15:04 +00:00
|
|
|
|
|
|
|
fn main() {
|
2024-01-16 17:45:02 +00:00
|
|
|
launch_desktop(app);
|
2022-01-02 07:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const FIELDS: &[(&str, &str)] = &[
|
|
|
|
("button", "Click me!"),
|
|
|
|
("checkbox", "CHECKBOX"),
|
|
|
|
("color", ""),
|
|
|
|
("date", ""),
|
|
|
|
("datetime-local", ""),
|
|
|
|
("email", ""),
|
|
|
|
("file", ""),
|
|
|
|
("image", ""),
|
|
|
|
("number", ""),
|
|
|
|
("password", ""),
|
|
|
|
("radio", ""),
|
|
|
|
("range", ""),
|
|
|
|
("reset", ""),
|
|
|
|
("search", ""),
|
|
|
|
("submit", ""),
|
|
|
|
("tel", ""),
|
|
|
|
("text", ""),
|
|
|
|
("time", ""),
|
|
|
|
("url", ""),
|
|
|
|
// less supported things
|
|
|
|
("hidden", ""),
|
|
|
|
("month", ""), // degrades to text most of the time, but works properly as "value'"
|
|
|
|
("week", ""), // degrades to text most of the time
|
|
|
|
];
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2022-01-02 07:15:04 +00:00
|
|
|
div { margin_left: "30px",
|
2024-01-14 05:12:21 +00:00
|
|
|
{select_example()},
|
2022-01-02 07:15:04 +00:00
|
|
|
div {
|
|
|
|
// handling inputs on divs will catch all input events below
|
|
|
|
// so the value of our input event will be either huey, dewey, louie, or true/false (because of the checkboxe)
|
|
|
|
// be mindful in grouping inputs together, as they will all be handled by the same event handler
|
2024-01-20 08:11:55 +00:00
|
|
|
oninput: move |evt| println!("{evt:?}"),
|
2022-01-02 07:15:04 +00:00
|
|
|
div {
|
|
|
|
input {
|
|
|
|
id: "huey",
|
|
|
|
r#type: "radio",
|
|
|
|
value: "huey",
|
2024-01-31 22:07:00 +00:00
|
|
|
checked: true,
|
2022-01-02 07:15:04 +00:00
|
|
|
name: "drone",
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
r#for: "huey",
|
|
|
|
"Huey"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
div {
|
|
|
|
input {
|
|
|
|
id: "dewey",
|
|
|
|
r#type: "radio",
|
|
|
|
value: "dewey",
|
|
|
|
name: "drone",
|
|
|
|
}
|
2024-01-31 22:07:00 +00:00
|
|
|
label { r#for: "dewey", "Dewey" }
|
2022-01-02 07:15:04 +00:00
|
|
|
}
|
|
|
|
div {
|
|
|
|
input {
|
|
|
|
id: "louie",
|
|
|
|
value: "louie",
|
|
|
|
r#type: "radio",
|
|
|
|
name: "drone",
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
r#for: "louie",
|
|
|
|
"Louie"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
div {
|
|
|
|
input {
|
|
|
|
id: "groovy",
|
|
|
|
value: "groovy",
|
|
|
|
r#type: "checkbox",
|
|
|
|
name: "drone",
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
r#for: "groovy",
|
|
|
|
"groovy"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// elements with driven values will preventdefault automatically.
|
|
|
|
// you can disable this override with preventdefault: false
|
|
|
|
div {
|
|
|
|
input {
|
|
|
|
id: "pdf",
|
|
|
|
value: "pdf",
|
|
|
|
name: "pdf",
|
|
|
|
r#type: "checkbox",
|
|
|
|
oninput: move |evt| {
|
2023-01-28 02:35:46 +00:00
|
|
|
println!("{evt:?}");
|
2022-01-02 07:15:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
r#for: "pdf",
|
|
|
|
"pdf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 03:33:34 +00:00
|
|
|
for (field, value) in FIELDS.iter() {
|
2022-01-02 07:15:04 +00:00
|
|
|
div {
|
|
|
|
input {
|
|
|
|
id: "{field}",
|
|
|
|
name: "{field}",
|
|
|
|
r#type: "{field}",
|
|
|
|
value: "{value}",
|
2022-01-05 22:30:12 +00:00
|
|
|
oninput: move |evt: FormEvent| {
|
2023-01-28 02:35:46 +00:00
|
|
|
println!("{evt:?}");
|
2022-01-02 07:15:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
r#for: "{field}",
|
|
|
|
"{field} element"
|
|
|
|
}
|
|
|
|
br {}
|
|
|
|
}
|
2024-01-11 03:33:34 +00:00
|
|
|
}
|
2022-01-02 07:15:04 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2023-04-23 15:15:06 +00:00
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn select_example() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-01-14 05:12:21 +00:00
|
|
|
div {
|
|
|
|
select {
|
|
|
|
id: "selection",
|
|
|
|
name: "selection",
|
|
|
|
multiple: true,
|
|
|
|
oninput: move |evt| println!("{evt:?}"),
|
|
|
|
option {
|
|
|
|
value: "Option 1",
|
|
|
|
label: "Option 1",
|
|
|
|
}
|
|
|
|
option {
|
|
|
|
value: "Option 2",
|
|
|
|
label: "Option 2",
|
|
|
|
selected: true,
|
|
|
|
},
|
|
|
|
option {
|
|
|
|
value: "Option 3",
|
|
|
|
label: "Option 3",
|
|
|
|
}
|
2023-04-23 15:15:06 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
label {
|
|
|
|
r#for: "selection",
|
|
|
|
"select element"
|
2023-04-23 15:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2023-04-23 15:15:06 +00:00
|
|
|
}
|