macro_rules! Form {
(
on_submit = |$bound:pat_param| $on_submit:block
$(
)*
) => {{
use leptos::{
view,
prelude::{
Get,
NodeRef,
ElementChild,
ClassAttribute,
OnAttribute,
signal,
Set,
Show,
},
html::Input,
web_sys::SubmitEvent
};
use log::info;
$(
let ($signal_name_get, $signal_name_set) = signal(None);
let $name: NodeRef = NodeRef::new();
)*
let on_submit = move |ev: SubmitEvent| {
struct Inputs {
$(
$name: $rust_type
),*
}
// stop the page from reloading!
ev.prevent_default();
$(
let value = {
let output = $name
.get()
// event handlers can only fire after the view
// is mounted to the DOM, so the `NodeRef` will be `Some`
.expect(" to exist")
.value();
let fin: Result<$rust_type, leptos::error::Error> = output
.parse()
.map_err(Into::::into);
fin
};
let $name = match value {
Ok(ok) => {
// Reset the signal
$signal_name_set.set(None);
ok
} ,
Err(err) => {
$signal_name_set.set(Some(err));
// Skip running the real `on_submit`
return
}
};
)*
let real_on_submit = |$bound| $on_submit;
real_on_submit(Inputs {
$(
$name
),*
})
};
view! {
}
}};
}
pub(crate) use Form;