1
0

inputs.rs 4.0 KB

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