form.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. dioxus::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. // However, 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. input { r#type: "text", name: "full-name" }
  53. label { r#for: "email", "Email (matching <name>@example.com)" }
  54. input { r#type: "email", pattern: ".+@example\\.com", size: "30", required: "true", id: "email", name: "email" }
  55. label { r#for: "password", "Password" }
  56. input { r#type: "password", name: "password" }
  57. label { r#for: "color", "Color" }
  58. input { r#type: "radio", checked: true, name: "color", value: "red" }
  59. input { r#type: "radio", name: "color", value: "blue" }
  60. input { r#type: "radio", name: "color", value: "green" }
  61. // Select multiple comes in as a comma separated list of selected values
  62. // You should split them on the comma to get the values manually
  63. label { r#for: "country", "Country" }
  64. select {
  65. name: "country",
  66. multiple: true,
  67. oninput: move |ev| {
  68. println!("Input event: {:#?}", ev);
  69. println!("Values: {:#?}", ev.value().split(',').collect::<Vec<_>>());
  70. },
  71. option { value: "usa", "USA" }
  72. option { value: "canada", "Canada" }
  73. option { value: "mexico", "Mexico" }
  74. }
  75. // Safari can be quirky with color inputs on mac.
  76. // We recommend always providing a text input for color as a fallback.
  77. label { r#for: "color", "Color" }
  78. input { r#type: "color", value: "#000002", name: "head", id: "head" }
  79. // Dates!
  80. input {
  81. min: "2018-01-01",
  82. value: "2018-07-22",
  83. r#type: "date",
  84. name: "trip-start",
  85. max: "2025-12-31",
  86. id: "start"
  87. }
  88. // CHekcboxes
  89. label { r#for: "cbox", "Color" }
  90. div {
  91. label { r#for: "cbox-red", "red" }
  92. input { r#type: "checkbox", checked: true, name: "cbox", value: "red", id: "cbox-red" }
  93. }
  94. div {
  95. label { r#for: "cbox-blue", "blue" }
  96. input { r#type: "checkbox", name: "cbox", value: "blue", id: "cbox-blue" }
  97. }
  98. div {
  99. label { r#for: "cbox-green", "green" }
  100. input { r#type: "checkbox", name: "cbox", value: "green", id: "cbox-green" }
  101. }
  102. div {
  103. label { r#for: "cbox-yellow", "yellow" }
  104. input { r#type: "checkbox", name: "cbox", value: "yellow", id: "cbox-yellow" }
  105. }
  106. // Buttons will submit your form by default.
  107. button { r#type: "submit", value: "Submit", "Submit the form" }
  108. }
  109. }
  110. div { style: "width: 50%",
  111. h1 { "Oninput Values" }
  112. pre { "{values:#?}" }
  113. }
  114. }
  115. }
  116. }