inputs.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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::{events::FormEvent, 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. //
  29. // less supported things
  30. ("hidden", ""),
  31. ("month", ""), // degrades to text most of the time, but works properly as "value'"
  32. ("week", ""), // degrades to text most of the time
  33. ];
  34. fn app(cx: Scope) -> Element {
  35. cx.render(rsx! {
  36. div { margin_left: "30px",
  37. // radio group
  38. div {
  39. // handling inputs on divs will catch all input events below
  40. // so the value of our input event will be either huey, dewey, louie, or true/false (because of the checkboxe)
  41. // be mindful in grouping inputs together, as they will all be handled by the same event handler
  42. oninput: move |evt| {
  43. println!("{:?}", evt);
  44. },
  45. div {
  46. input {
  47. id: "huey",
  48. r#type: "radio",
  49. value: "huey",
  50. checked: "",
  51. name: "drone",
  52. }
  53. label {
  54. r#for: "huey",
  55. "Huey"
  56. }
  57. }
  58. div {
  59. input {
  60. id: "dewey",
  61. r#type: "radio",
  62. value: "dewey",
  63. name: "drone",
  64. }
  65. label {
  66. r#for: "dewey",
  67. "Dewey"
  68. }
  69. }
  70. div {
  71. input {
  72. id: "louie",
  73. value: "louie",
  74. r#type: "radio",
  75. name: "drone",
  76. }
  77. label {
  78. r#for: "louie",
  79. "Louie"
  80. }
  81. }
  82. div {
  83. input {
  84. id: "groovy",
  85. value: "groovy",
  86. r#type: "checkbox",
  87. name: "drone",
  88. }
  89. label {
  90. r#for: "groovy",
  91. "groovy"
  92. }
  93. }
  94. }
  95. // elements with driven values will preventdefault automatically.
  96. // you can disable this override with preventdefault: false
  97. div {
  98. input {
  99. id: "pdf",
  100. value: "pdf",
  101. name: "pdf",
  102. r#type: "checkbox",
  103. oninput: move |evt| {
  104. println!("{:?}", evt);
  105. },
  106. }
  107. label {
  108. r#for: "pdf",
  109. "pdf"
  110. }
  111. }
  112. FIELDS.iter().map(|(field, value)| rsx!(
  113. div {
  114. input {
  115. id: "{field}",
  116. name: "{field}",
  117. r#type: "{field}",
  118. value: "{value}",
  119. // checked: "false",
  120. oninput: move |evt: FormEvent| {
  121. println!("{:?}", evt);
  122. },
  123. }
  124. label {
  125. r#for: "{field}",
  126. "{field} element"
  127. }
  128. br {}
  129. }
  130. ))
  131. }
  132. })
  133. }