form.rs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. use crate::file_data::HasFileData;
  2. use std::{collections::HashMap, fmt::Debug, ops::Deref};
  3. use dioxus_core::Event;
  4. pub type FormEvent = Event<FormData>;
  5. /// A form value that may either be a list of values or a single value
  6. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  7. #[derive(Debug, Default, Clone, PartialEq)]
  8. pub struct FormValue(pub Vec<String>);
  9. impl Deref for FormValue {
  10. type Target = [String];
  11. fn deref(&self) -> &Self::Target {
  12. self.as_slice()
  13. }
  14. }
  15. impl FormValue {
  16. /// Convenient way to represent Value as slice
  17. pub fn as_slice(&self) -> &[String] {
  18. &self.0
  19. }
  20. /// Return the first value, panicking if there are none
  21. pub fn as_value(&self) -> String {
  22. self.0.first().unwrap().clone()
  23. }
  24. /// Convert into [`Vec<String>`]
  25. pub fn to_vec(self) -> Vec<String> {
  26. self.0.clone()
  27. }
  28. }
  29. impl PartialEq<str> for FormValue {
  30. fn eq(&self, other: &str) -> bool {
  31. self.0.len() == 1 && self.0.first().map(|s| s.as_str()) == Some(other)
  32. }
  33. }
  34. /* DOMEvent: Send + SyncTarget relatedTarget */
  35. pub struct FormData {
  36. inner: Box<dyn HasFormData>,
  37. }
  38. impl<E: HasFormData> From<E> for FormData {
  39. fn from(e: E) -> Self {
  40. Self { inner: Box::new(e) }
  41. }
  42. }
  43. impl PartialEq for FormData {
  44. fn eq(&self, other: &Self) -> bool {
  45. self.value() == other.value() && self.values() == other.values()
  46. }
  47. }
  48. impl Debug for FormData {
  49. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  50. f.debug_struct("FormEvent")
  51. .field("value", &self.value())
  52. .field("values", &self.values())
  53. .field("valid", &self.valid())
  54. .finish()
  55. }
  56. }
  57. impl FormData {
  58. /// Create a new form event
  59. pub fn new(event: impl HasFormData + 'static) -> Self {
  60. Self {
  61. inner: Box::new(event),
  62. }
  63. }
  64. /// Get the value of the form event
  65. pub fn value(&self) -> String {
  66. self.inner.value()
  67. }
  68. /// Get the value of the form event as a parsed type
  69. pub fn parsed<T>(&self) -> Result<T, T::Err>
  70. where
  71. T: std::str::FromStr,
  72. {
  73. self.value().parse()
  74. }
  75. /// Try to parse the value as a boolean
  76. ///
  77. /// Returns false if the value is not a boolean, or if it is false!
  78. /// Does not verify anything about the event itself, use with caution
  79. pub fn checked(&self) -> bool {
  80. self.value().parse().unwrap_or(false)
  81. }
  82. /// Collect all the named form values from the containing form.
  83. ///
  84. /// Every input must be named!
  85. pub fn values(&self) -> HashMap<String, FormValue> {
  86. self.inner.values()
  87. }
  88. /// Get the files of the form event
  89. #[cfg(feature = "file-engine")]
  90. pub fn files(&self) -> Option<std::sync::Arc<dyn crate::file_data::FileEngine>> {
  91. self.inner.files()
  92. }
  93. /// Downcast this event to a concrete event type
  94. #[inline(always)]
  95. pub fn downcast<T: 'static>(&self) -> Option<&T> {
  96. self.inner.as_any().downcast_ref::<T>()
  97. }
  98. /// Did this form pass its own validation?
  99. pub fn valid(&self) -> bool {
  100. self.inner.value().is_empty()
  101. }
  102. }
  103. /// An object that has all the data for a form event
  104. pub trait HasFormData: HasFileData + std::any::Any {
  105. fn value(&self) -> String {
  106. Default::default()
  107. }
  108. fn valid(&self) -> bool {
  109. true
  110. }
  111. fn values(&self) -> HashMap<String, FormValue> {
  112. Default::default()
  113. }
  114. /// return self as Any
  115. fn as_any(&self) -> &dyn std::any::Any;
  116. }
  117. impl FormData {
  118. #[cfg(feature = "serialize")]
  119. /// Parse the values into a struct with one field per value
  120. pub fn parsed_values<T>(&self) -> Result<T, serde_json::Error>
  121. where
  122. T: serde::de::DeserializeOwned,
  123. {
  124. use serde::Serialize;
  125. fn convert_hashmap_to_json<K, V>(hashmap: &HashMap<K, V>) -> serde_json::Result<String>
  126. where
  127. K: Serialize + std::hash::Hash + Eq,
  128. V: Serialize,
  129. {
  130. serde_json::to_string(hashmap)
  131. }
  132. let parsed_json =
  133. convert_hashmap_to_json(&self.values()).expect("Failed to parse values to JSON");
  134. serde_json::from_str(&parsed_json)
  135. }
  136. }
  137. #[cfg(feature = "serialize")]
  138. /// A serialized form data object
  139. #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
  140. pub struct SerializedFormData {
  141. #[serde(default)]
  142. value: String,
  143. #[serde(default)]
  144. values: HashMap<String, FormValue>,
  145. #[serde(default)]
  146. valid: bool,
  147. #[cfg(feature = "file-engine")]
  148. #[serde(default)]
  149. files: Option<crate::file_data::SerializedFileEngine>,
  150. }
  151. #[cfg(feature = "serialize")]
  152. impl SerializedFormData {
  153. /// Create a new serialized form data object
  154. pub fn new(value: String, values: HashMap<String, FormValue>) -> Self {
  155. Self {
  156. value,
  157. values,
  158. valid: true,
  159. #[cfg(feature = "file-engine")]
  160. files: None,
  161. }
  162. }
  163. #[cfg(feature = "file-engine")]
  164. /// Add files to the serialized form data object
  165. pub fn with_files(mut self, files: crate::file_data::SerializedFileEngine) -> Self {
  166. self.files = Some(files);
  167. self
  168. }
  169. /// Create a new serialized form data object from a traditional form data object
  170. pub async fn async_from(data: &FormData) -> Self {
  171. Self {
  172. value: data.value(),
  173. values: data.values(),
  174. valid: data.valid(),
  175. #[cfg(feature = "file-engine")]
  176. files: {
  177. match data.files() {
  178. Some(files) => {
  179. let mut resolved_files = HashMap::new();
  180. for file in files.files() {
  181. let bytes = files.read_file(&file).await;
  182. resolved_files.insert(file, bytes.unwrap_or_default());
  183. }
  184. Some(crate::file_data::SerializedFileEngine {
  185. files: resolved_files,
  186. })
  187. }
  188. None => None,
  189. }
  190. },
  191. }
  192. }
  193. fn from_lossy(data: &FormData) -> Self {
  194. Self {
  195. value: data.value(),
  196. values: data.values(),
  197. valid: data.valid(),
  198. #[cfg(feature = "file-engine")]
  199. files: None,
  200. }
  201. }
  202. }
  203. #[cfg(feature = "serialize")]
  204. impl HasFormData for SerializedFormData {
  205. fn value(&self) -> String {
  206. self.value.clone()
  207. }
  208. fn values(&self) -> HashMap<String, FormValue> {
  209. self.values.clone()
  210. }
  211. fn valid(&self) -> bool {
  212. self.valid
  213. }
  214. fn as_any(&self) -> &dyn std::any::Any {
  215. self
  216. }
  217. }
  218. #[cfg(feature = "serialize")]
  219. impl HasFileData for SerializedFormData {
  220. #[cfg(feature = "file-engine")]
  221. fn files(&self) -> Option<std::sync::Arc<dyn crate::FileEngine>> {
  222. self.files
  223. .as_ref()
  224. .map(|files| std::sync::Arc::new(files.clone()) as _)
  225. }
  226. }
  227. #[cfg(feature = "file-engine")]
  228. impl HasFileData for FormData {
  229. fn files(&self) -> Option<std::sync::Arc<dyn crate::FileEngine>> {
  230. self.inner.files()
  231. }
  232. }
  233. #[cfg(feature = "serialize")]
  234. impl serde::Serialize for FormData {
  235. fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
  236. SerializedFormData::from_lossy(self).serialize(serializer)
  237. }
  238. }
  239. #[cfg(feature = "serialize")]
  240. impl<'de> serde::Deserialize<'de> for FormData {
  241. fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
  242. let data = SerializedFormData::deserialize(deserializer)?;
  243. Ok(Self {
  244. inner: Box::new(data),
  245. })
  246. }
  247. }
  248. impl_event! {
  249. FormData;
  250. /// onchange
  251. onchange
  252. /// The `oninput` event is fired when the value of a `<input>`, `<select>`, or `<textarea>` element is changed.
  253. ///
  254. /// There are two main approaches to updating your input element:
  255. /// 1) Controlled inputs directly update the value of the input element as the user interacts with the element
  256. ///
  257. /// ```rust
  258. /// use dioxus::prelude::*;
  259. ///
  260. /// fn App() -> Element {
  261. /// let mut value = use_signal(|| "hello world".to_string());
  262. ///
  263. /// rsx! {
  264. /// input {
  265. /// // We directly set the value of the input element to our value signal
  266. /// value: "{value}",
  267. /// // The `oninput` event handler will run every time the user changes the value of the input element
  268. /// // We can set the `value` signal to the new value of the input element
  269. /// oninput: move |event| value.set(event.value())
  270. /// }
  271. /// // Since this is a controlled input, we can also update the value of the input element directly
  272. /// button {
  273. /// onclick: move |_| value.write().clear(),
  274. /// "Clear"
  275. /// }
  276. /// }
  277. /// }
  278. /// ```
  279. ///
  280. /// 2) Uncontrolled inputs just read the value of the input element as it changes
  281. ///
  282. /// ```rust
  283. /// use dioxus::prelude::*;
  284. ///
  285. /// fn App() -> Element {
  286. /// rsx! {
  287. /// input {
  288. /// // In uncontrolled inputs, we don't set the value of the input element directly
  289. /// // But you can still read the value of the input element
  290. /// oninput: move |event| println!("{}", event.value()),
  291. /// }
  292. /// // Since we don't directly control the value of the input element, we can't easily modify it
  293. /// }
  294. /// }
  295. /// ```
  296. oninput
  297. /// oninvalid
  298. oninvalid
  299. /// onreset
  300. onreset
  301. /// onsubmit
  302. onsubmit
  303. }