inputs.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 dioxus::prelude::*;
  5. fn main() {
  6. dioxus_desktop::launch(app);
  7. }
  8. const FIELDS: &[(&str, &str)] = &[
  9. ("button", "Click me!"),
  10. ("checkbox", "CHECKBOX"),
  11. ("color", ""),
  12. ("date", ""),
  13. ("datetime-local", ""),
  14. ("email", ""),
  15. ("file", ""),
  16. ("image", ""),
  17. ("number", ""),
  18. ("password", ""),
  19. ("radio", ""),
  20. ("range", ""),
  21. ("reset", ""),
  22. ("search", ""),
  23. ("submit", ""),
  24. ("tel", ""),
  25. ("text", ""),
  26. ("time", ""),
  27. ("url", ""),
  28. // less supported things
  29. ("hidden", ""),
  30. ("month", ""), // degrades to text most of the time, but works properly as "value'"
  31. ("week", ""), // degrades to text most of the time
  32. ];
  33. fn app(cx: Scope) -> Element {
  34. cx.render(rsx! {
  35. div { margin_left: "30px",
  36. select_example(cx),
  37. div {
  38. // handling inputs on divs will catch all input events below
  39. // so the value of our input event will be either huey, dewey, louie, or true/false (because of the checkboxe)
  40. // be mindful in grouping inputs together, as they will all be handled by the same event handler
  41. oninput: move |evt| {
  42. println!("{evt:?}");
  43. },
  44. div {
  45. input {
  46. id: "huey",
  47. r#type: "radio",
  48. value: "huey",
  49. checked: "",
  50. name: "drone",
  51. }
  52. label {
  53. r#for: "huey",
  54. "Huey"
  55. }
  56. }
  57. div {
  58. input {
  59. id: "dewey",
  60. r#type: "radio",
  61. value: "dewey",
  62. name: "drone",
  63. }
  64. label {
  65. r#for: "dewey",
  66. "Dewey"
  67. }
  68. }
  69. div {
  70. input {
  71. id: "louie",
  72. value: "louie",
  73. r#type: "radio",
  74. name: "drone",
  75. }
  76. label {
  77. r#for: "louie",
  78. "Louie"
  79. }
  80. }
  81. div {
  82. input {
  83. id: "groovy",
  84. value: "groovy",
  85. r#type: "checkbox",
  86. name: "drone",
  87. }
  88. label {
  89. r#for: "groovy",
  90. "groovy"
  91. }
  92. }
  93. }
  94. // elements with driven values will preventdefault automatically.
  95. // you can disable this override with preventdefault: false
  96. div {
  97. input {
  98. id: "pdf",
  99. value: "pdf",
  100. name: "pdf",
  101. r#type: "checkbox",
  102. oninput: move |evt| {
  103. println!("{evt:?}");
  104. },
  105. }
  106. label {
  107. r#for: "pdf",
  108. "pdf"
  109. }
  110. }
  111. FIELDS.iter().map(|(field, value)| rsx! {
  112. div {
  113. input {
  114. id: "{field}",
  115. name: "{field}",
  116. r#type: "{field}",
  117. value: "{value}",
  118. oninput: move |evt: FormEvent| {
  119. println!("{evt:?}");
  120. },
  121. }
  122. label {
  123. r#for: "{field}",
  124. "{field} element"
  125. }
  126. br {}
  127. }
  128. })
  129. }
  130. })
  131. }
  132. fn select_example(cx: Scope) -> Element {
  133. cx.render(rsx! {
  134. div {
  135. select {
  136. id: "selection",
  137. name: "selection",
  138. multiple: true,
  139. oninput: move |evt| {
  140. println!("{evt:?}");
  141. },
  142. option {
  143. value : "Option 1",
  144. label : "Option 1",
  145. }
  146. option {
  147. value : "Option 2",
  148. label : "Option 2",
  149. selected : true,
  150. },
  151. option {
  152. value : "Option 3",
  153. label : "Option 3",
  154. }
  155. }
  156. label {
  157. r#for: "selection",
  158. "select element"
  159. }
  160. }})
  161. }