use crate::{
hooks::{use_tmp_ref, JsRefContainer},
Callback, KeyType,
};
use js_sys::{Object, Reflect};
use wasm_bindgen::{
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi},
intern, 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<impl KeyType>) -> Self {
self.insert(
"key",
&value.map(|x| x.into()).unwrap_or(JsValue::UNDEFINED),
)
}
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: &Callback<Option<T>>) -> Self
where
T: OptionFromWasmAbi + 'static,
{
self.insert_callback("ref", ref_callback)
}
pub fn insert(self, key: &str, value: &JsValue) -> Self {
self.ref_insert(key, value);
self
}
fn ref_insert(&self, key: &str, value: &JsValue) {
Reflect::set(&self.0, &intern(key).into(), value)
.expect_throw("cannot write into props object");
}
pub fn insert_callback<T, U>(self, key: &str, f: &Callback<T, U>) -> Self
where
T: FromWasmAbi + 'static,
U: IntoWasmAbi + 'static,
{
use_tmp_ref(f.clone(), |f| {
self.ref_insert(key, &f.as_js());
});
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>()?))
}
}