1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::{callback::PersistedCallback, hooks::JsRefContainer};
use js_sys::{Object, Reflect};
use wasm_bindgen::{
convert::{FromWasmAbi, IntoWasmAbi},
JsCast, JsValue, UnwrapThrowExt,
};
#[derive(Debug, Default, Clone)]
pub struct Props(Object);
impl Props {
pub fn new() -> Self {
Self::default()
}
pub fn key(self, value: Option<&str>) -> Self {
self.insert("key", &value.into())
}
pub fn ref_container<T>(self, ref_container: &JsRefContainer<T>) -> Self {
self.insert("ref", ref_container.as_ref())
}
pub fn ref_callback<T>(self, ref_callback: &PersistedCallback<T>) -> Self
where
T: FromWasmAbi + 'static,
{
self.insert_callback("ref", ref_callback)
}
pub fn insert(self, key: &str, value: &JsValue) -> Self {
Reflect::set(&self.0, &key.into(), value)
.expect_throw("cannot write into props object");
self
}
pub fn insert_callback<T, U>(
self,
key: &str,
f: &PersistedCallback<T, U>,
) -> Self
where
T: FromWasmAbi + 'static,
U: IntoWasmAbi + 'static,
{
Reflect::set(&self.0, &key.into(), &f.as_js())
.expect_throw("cannot write into props object");
self
}
}
impl AsRef<JsValue> for Props {
fn as_ref(&self) -> &JsValue {
&self.0
}
}
impl From<Props> for JsValue {
fn from(style: Props) -> Self {
style.0.into()
}
}
impl From<Object> for Props {
fn from(value: Object) -> Self {
Props(value)
}
}
impl TryFrom<JsValue> for Props {
type Error = JsValue;
fn try_from(value: JsValue) -> Result<Self, Self::Error> {
Ok(Props(value.dyn_into::<Object>()?))
}
}