1
0

inputs.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. launch_desktop(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() -> Element {
  34. rsx! {
  35. div { margin_left: "30px",
  36. {select_example()},
  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| println!("{evt:?}"),
  42. div {
  43. input {
  44. id: "huey",
  45. r#type: "radio",
  46. value: "huey",
  47. checked: "",
  48. name: "drone",
  49. }
  50. label {
  51. r#for: "huey",
  52. "Huey"
  53. }
  54. }
  55. div {
  56. input {
  57. id: "dewey",
  58. r#type: "radio",
  59. value: "dewey",
  60. name: "drone",
  61. }
  62. label {
  63. r#for: "dewey",
  64. "Dewey"
  65. }
  66. }
  67. div {
  68. input {
  69. id: "louie",
  70. value: "louie",
  71. r#type: "radio",
  72. name: "drone",
  73. }
  74. label {
  75. r#for: "louie",
  76. "Louie"
  77. }
  78. }
  79. div {
  80. input {
  81. id: "groovy",
  82. value: "groovy",
  83. r#type: "checkbox",
  84. name: "drone",
  85. }
  86. label {
  87. r#for: "groovy",
  88. "groovy"
  89. }
  90. }
  91. }
  92. // elements with driven values will preventdefault automatically.
  93. // you can disable this override with preventdefault: false
  94. div {
  95. input {
  96. id: "pdf",
  97. value: "pdf",
  98. name: "pdf",
  99. r#type: "checkbox",
  100. oninput: move |evt| {
  101. println!("{evt:?}");
  102. },
  103. }
  104. label {
  105. r#for: "pdf",
  106. "pdf"
  107. }
  108. }
  109. for (field, value) in FIELDS.iter() {
  110. div {
  111. input {
  112. id: "{field}",
  113. name: "{field}",
  114. r#type: "{field}",
  115. value: "{value}",
  116. oninput: move |evt: FormEvent| {
  117. println!("{evt:?}");
  118. },
  119. }
  120. label {
  121. r#for: "{field}",
  122. "{field} element"
  123. }
  124. br {}
  125. }
  126. }
  127. }
  128. }
  129. }
  130. fn select_example() -> Element {
  131. rsx! {
  132. div {
  133. select {
  134. id: "selection",
  135. name: "selection",
  136. multiple: true,
  137. oninput: move |evt| println!("{evt:?}"),
  138. option {
  139. value: "Option 1",
  140. label: "Option 1",
  141. }
  142. option {
  143. value: "Option 2",
  144. label: "Option 2",
  145. selected: true,
  146. },
  147. option {
  148. value: "Option 3",
  149. label: "Option 3",
  150. }
  151. }
  152. label {
  153. r#for: "selection",
  154. "select element"
  155. }
  156. }
  157. }
  158. }