inputs.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::FormData;
  5. use dioxus::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. fn app(cx: Scope) -> Element {
  36. cx.render(rsx! {
  37. div { margin_left: "30px",
  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. oninput: move |evt: FormEvent| {
  120. println!("{:?}", evt);
  121. },
  122. }
  123. label {
  124. r#for: "{field}",
  125. "{field} element"
  126. }
  127. br {}
  128. }
  129. ))
  130. }
  131. })
  132. }