rust_fel/props.rs
1use crate::element::Element;
2use std::fmt;
3
4/// A type commonly used for construction of a [wasm-bindgen Closure](https://docs.rs/wasm-bindgen/0.2.67/wasm_bindgen/closure/struct.Closure.html) for use with [DOM Element Event Handlers](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers).
5/// # Examples
6/// In this example ```&self``` is a [tuple struct](https://doc.rust-lang.org/1.9.0/book/structs.html#tuple-structs) which implements [rust_fel::Component](../rust_fel/trait.Component.html)
7///```ignore
8///fn render(&self) -> rust_fel::Element {
9/// let borrow = self.0.borrow_mut();
10/// let state = borrow.state.clone();
11/// let mut new_clone = self.clone();
12///
13/// let (theme_onclick, theme_class) = match state.action {
14/// Actions::LightMode => (
15/// Box::new(move || new_clone.reduce_state(Actions::LightMode))
16/// as rust_fel::ClosureProp,
17/// "Light-Mode".to_owned(),
18/// ),
19/// Actions::DarkMode => (
20/// Box::new(move || new_clone.reduce_state(Actions::DarkMode))
21/// as rust_fel::ClosureProp,
22/// "Dark-Mode".to_owned(),
23/// ),
24/// _ => (Box::new(|| ()) as rust_fel::ClosureProp, "".to_owned()),
25/// };
26///
27/// rust_fel::Element::new(
28/// "main".to_owned(),
29/// rust_fel::Props {
30/// id: Some(borrow.id.clone()),
31/// onclick: Some(theme_onclick),
32/// class_name: Some(format!("main {}", theme_class)),
33/// ..Default::default()
34/// },
35/// )
36/// }
37///```
38pub type ClosureProp = Box<dyn FnMut()>;
39
40/// A [struct](https://doc.rust-lang.org/std/keyword.struct.html) holding attributes for a Virtual [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) [rust_fel::Element](../rust_fel/struct.Element.html).
41/// ```Elements``` can have ```children``` stored inside a [std::vec::Vec](https://doc.rust-lang.org/std/vec/struct.Vec.html).
42pub struct Props {
43 pub children: Option<Vec<Element>>,
44 pub text: Option<String>,
45 pub on_click: Option<ClosureProp>,
46 pub mouse: Option<ClosureProp>,
47 pub class_name: Option<String>,
48 pub id: Option<String>,
49 pub href: Option<String>,
50 pub src: Option<String>,
51 pub type_attr: Option<String>,
52 pub role: Option<String>,
53 pub data_cy: Option<String>,
54}
55
56impl fmt::Debug for Props {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 write!(
59 f,
60 "{:#?} props.children, {:#?} props.text, {:#?} props.class_name {:#?} props.id",
61 self.children, self.text, self.class_name, self.id
62 )
63 }
64}
65
66impl Default for Props {
67 fn default() -> Self {
68 Props {
69 children: None,
70 text: None,
71 on_click: None,
72 class_name: None,
73 id: None,
74 mouse: None,
75 href: None,
76 src: None,
77 type_attr: None,
78 role: None,
79 data_cy: None,
80 }
81 }
82}