inputs.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //! This example roughly shows how events are serialized into Rust from JavaScript.
  2. //!
  3. //! There is some conversion happening when input types are checkbox/radio/select/textarea etc.
  4. use std::sync::Arc;
  5. use dioxus::{events::FormEvent, prelude::*};
  6. fn main() {
  7. dioxus::desktop::launch(App);
  8. }
  9. const FIELDS: &[(&str, &str)] = &[
  10. ("button", "Click me!"),
  11. ("checkbox", "CHECKBOX"),
  12. ("color", ""),
  13. ("date", ""),
  14. ("datetime-local", ""),
  15. ("email", ""),
  16. ("file", ""),
  17. ("image", ""),
  18. ("number", ""),
  19. ("password", ""),
  20. ("radio", ""),
  21. ("range", ""),
  22. ("reset", ""),
  23. ("search", ""),
  24. ("submit", ""),
  25. ("tel", ""),
  26. ("text", ""),
  27. ("time", ""),
  28. ("url", ""),
  29. //
  30. // less supported things
  31. ("hidden", ""),
  32. ("month", ""), // degrades to text most of the time, but works properly as "value'"
  33. ("week", ""), // degrades to text most of the time
  34. ];
  35. static App: Component = |cx| {
  36. cx.render(rsx! {
  37. div { margin_left: "30px",
  38. // radio group
  39. div {
  40. // handling inputs on divs will catch all input events below
  41. // so the value of our input event will be either huey, dewey, louie, or true/false (because of the checkboxe)
  42. // be mindful in grouping inputs together, as they will all be handled by the same event handler
  43. oninput: move |evt| {
  44. println!("{:?}", evt);
  45. },
  46. div {
  47. input {
  48. id: "huey",
  49. r#type: "radio",
  50. value: "huey",
  51. checked: "",
  52. name: "drone",
  53. }
  54. label {
  55. r#for: "huey",
  56. "Huey"
  57. }
  58. }
  59. div {
  60. input {
  61. id: "dewey",
  62. r#type: "radio",
  63. value: "dewey",
  64. name: "drone",
  65. }
  66. label {
  67. r#for: "dewey",
  68. "Dewey"
  69. }
  70. }
  71. div {
  72. input {
  73. id: "louie",
  74. value: "louie",
  75. r#type: "radio",
  76. name: "drone",
  77. }
  78. label {
  79. r#for: "louie",
  80. "Louie"
  81. }
  82. }
  83. div {
  84. input {
  85. id: "groovy",
  86. value: "groovy",
  87. r#type: "checkbox",
  88. name: "drone",
  89. }
  90. label {
  91. r#for: "groovy",
  92. "groovy"
  93. }
  94. }
  95. }
  96. // elements with driven values will preventdefault automatically.
  97. // you can disable this override with preventdefault: false
  98. div {
  99. input {
  100. id: "pdf",
  101. value: "pdf",
  102. name: "pdf",
  103. r#type: "checkbox",
  104. oninput: move |evt| {
  105. println!("{:?}", evt);
  106. },
  107. }
  108. label {
  109. r#for: "pdf",
  110. "pdf"
  111. }
  112. }
  113. FIELDS.iter().map(|(field, value)| rsx!(
  114. div {
  115. input {
  116. id: "{field}",
  117. name: "{field}",
  118. r#type: "{field}",
  119. value: "{value}",
  120. // checked: "false",
  121. oninput: move |evt: Arc<FormEvent>| {
  122. println!("{:?}", evt);
  123. },
  124. }
  125. label {
  126. r#for: "{field}",
  127. "{field} element"
  128. }
  129. br {}
  130. }
  131. ))
  132. }
  133. })
  134. };