1
0

form.rs 4.6 KB

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