form.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use std::{collections::HashMap, fmt::Debug, sync::Arc};
  2. use dioxus_core::Event;
  3. pub type FormEvent = Event<FormData>;
  4. /* DOMEvent: Send + SyncTarget relatedTarget */
  5. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  6. #[derive(Clone)]
  7. pub struct FormData {
  8. pub value: String,
  9. pub values: HashMap<String, String>,
  10. #[cfg_attr(feature = "serialize", serde(skip))]
  11. pub files: Option<Arc<dyn FileEngine>>,
  12. }
  13. impl PartialEq for FormData {
  14. fn eq(&self, other: &Self) -> bool {
  15. self.value == other.value && self.values == other.values
  16. }
  17. }
  18. impl Debug for FormData {
  19. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  20. f.debug_struct("FormEvent")
  21. .field("value", &self.value)
  22. .field("values", &self.values)
  23. .finish()
  24. }
  25. }
  26. #[async_trait::async_trait(?Send)]
  27. pub trait FileEngine {
  28. // get a list of file names
  29. fn files(&self) -> Vec<String>;
  30. // read a file to bytes
  31. async fn read_file(&self, file: &str) -> Option<Vec<u8>>;
  32. // read a file to string
  33. async fn read_file_to_string(&self, file: &str) -> Option<String>;
  34. }
  35. impl_event! {
  36. FormData;
  37. /// onchange
  38. onchange
  39. /// oninput handler
  40. oninput
  41. /// oninvalid
  42. oninvalid
  43. /// onreset
  44. onreset
  45. /// onsubmit
  46. onsubmit
  47. }