form.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //! Forms
  2. //!
  3. //! Dioxus forms deviate slightly from html, automatically returning all named inputs
  4. //! in the "values" field.
  5. use dioxus::prelude::*;
  6. use std::collections::HashMap;
  7. fn main() {
  8. launch(app);
  9. }
  10. fn app() -> Element {
  11. let mut values = use_signal(|| HashMap::new());
  12. rsx! {
  13. div {
  14. h1 { "Form" }
  15. // The form element is used to create an HTML form for user input
  16. // You can attach regular attributes to it
  17. form {
  18. id: "cool-form",
  19. style: "display: flex; flex-direction: column;",
  20. // You can attach a handler to the entire form
  21. oninput: move |ev| {
  22. println!("Input event: {:#?}", ev);
  23. values.set(ev.values());
  24. },
  25. // On desktop/liveview, the form will not navigate the page - the expectation is that you handle
  26. // The form event.
  27. // Howver, if your form doesn't have a submit handler, it might navigate the page depending on the webview.
  28. // We suggest always attaching a submit handler to the form.
  29. onsubmit: move |ev| {
  30. println!("Submit event: {:#?}", ev);
  31. },
  32. // Regular text inputs with handlers
  33. label { r#for: "username", "Username" }
  34. input {
  35. r#type: "text",
  36. name: "username",
  37. oninput: move |ev| {
  38. println!("setting username");
  39. values.set(ev.values());
  40. }
  41. }
  42. // And then the various inputs that might exist
  43. // Note for a value to be returned in .values(), it must be named!
  44. label { r#for: "full-name", "Full Name" }
  45. input { r#type: "text", name: "full-name" }
  46. label { r#for: "email", "Email" }
  47. input { r#type: "email", pattern: ".+@example\\.com", size: "30", required: "true", id: "email", name: "email" }
  48. label { r#for: "password", "Password" }
  49. input { r#type: "password", name: "password" }
  50. label { r#for: "color", "Color" }
  51. input { r#type: "radio", checked: true, name: "color", value: "red" }
  52. input { r#type: "radio", name: "color", value: "blue" }
  53. input { r#type: "radio", name: "color", value: "green" }
  54. // Select multiple comes in as a comma separated list of selected values
  55. // You should split them on the comma to get the values manually
  56. label { r#for: "country", "Country" }
  57. select {
  58. name: "country",
  59. multiple: true,
  60. oninput: move |ev| {
  61. println!("Input event: {:#?}", ev);
  62. println!("Values: {:#?}", ev.value().split(',').collect::<Vec<_>>());
  63. },
  64. option { value: "usa", "USA" }
  65. option { value: "canada", "Canada" }
  66. option { value: "mexico", "Mexico" }
  67. }
  68. // Safari can be quirky with color inputs on mac.
  69. // We recommend always providing a text input for color as a fallback.
  70. label { r#for: "color", "Color" }
  71. input { r#type: "color", value: "#000002", name: "head", id: "head" }
  72. // Dates!
  73. input {
  74. min: "2018-01-01",
  75. value: "2018-07-22",
  76. r#type: "date",
  77. name: "trip-start",
  78. max: "2025-12-31",
  79. id: "start"
  80. }
  81. // Buttons will submit your form by default.
  82. button { r#type: "submit", value: "Submit", "Submit the form" }
  83. }
  84. }
  85. div {
  86. h1 { "Oninput Values" }
  87. pre { "{values:#?}" }
  88. }
  89. }
  90. }