dioxus/examples/form.rs

116 lines
4.6 KiB
Rust
Raw Normal View History

2022-02-04 07:13:35 +00:00
//! Forms
//!
2022-02-04 07:13:35 +00:00
//! Dioxus forms deviate slightly from html, automatically returning all named inputs
2024-02-14 20:33:07 +00:00
//! in the "values" field.
use dioxus::prelude::*;
use std::collections::HashMap;
fn main() {
launch(app);
}
fn app() -> Element {
let mut values = use_signal(HashMap::new);
let mut submitted_values = use_signal(HashMap::new);
2024-03-05 20:08:33 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2024-03-05 20:08:33 +00:00
div { style: "display: flex",
div { style: "width: 50%",
h1 { "Form" }
if !submitted_values.read().is_empty() {
h2 { "Submitted! ✅" }
}
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// The form element is used to create an HTML form for user input
// You can attach regular attributes to it
form {
id: "cool-form",
style: "display: flex; flex-direction: column;",
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// You can attach a handler to the entire form
oninput: move |ev| {
println!("Input event: {:#?}", ev);
values.set(ev.values());
},
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// On desktop/liveview, the form will not navigate the page - the expectation is that you handle
// The form event.
// Howver, if your form doesn't have a submit handler, it might navigate the page depending on the webview.
// We suggest always attaching a submit handler to the form.
onsubmit: move |ev| {
println!("Submit event: {:#?}", ev);
submitted_values.set(ev.values());
},
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// Regular text inputs with handlers
label { r#for: "username", "Username" }
input {
r#type: "text",
name: "username",
oninput: move |ev| {
println!("setting username");
values.set(ev.values());
}
}
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// And then the various inputs that might exist
// Note for a value to be returned in .values(), it must be named!
label { r#for: "full-name", "Full Name" }
input { r#type: "text", name: "full-name" }
label { r#for: "email", "Email" }
input { r#type: "email", pattern: ".+@example\\.com", size: "30", required: "true", id: "email", name: "email" }
label { r#for: "password", "Password" }
input { r#type: "password", name: "password" }
label { r#for: "color", "Color" }
input { r#type: "radio", checked: true, name: "color", value: "red" }
input { r#type: "radio", name: "color", value: "blue" }
input { r#type: "radio", name: "color", value: "green" }
// Select multiple comes in as a comma separated list of selected values
// You should split them on the comma to get the values manually
label { r#for: "country", "Country" }
select {
name: "country",
multiple: true,
oninput: move |ev| {
println!("Input event: {:#?}", ev);
println!("Values: {:#?}", ev.value().split(',').collect::<Vec<_>>());
},
option { value: "usa", "USA" }
option { value: "canada", "Canada" }
option { value: "mexico", "Mexico" }
}
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// Safari can be quirky with color inputs on mac.
// We recommend always providing a text input for color as a fallback.
label { r#for: "color", "Color" }
input { r#type: "color", value: "#000002", name: "head", id: "head" }
// Dates!
input {
min: "2018-01-01",
value: "2018-07-22",
r#type: "date",
name: "trip-start",
max: "2025-12-31",
id: "start"
}
2024-03-02 07:37:46 +00:00
2024-03-05 20:08:33 +00:00
// Buttons will submit your form by default.
button { r#type: "submit", value: "Submit", "Submit the form" }
2024-03-02 07:37:46 +00:00
}
}
2024-03-05 20:08:33 +00:00
div { style: "width: 50%",
h1 { "Oninput Values" }
pre { "{values:#?}" }
}
}
2024-01-14 05:12:21 +00:00
}
}