nova_forms/hooks/
nova_forms_use.rs

1use leptos::*;
2
3/// Wires multiple input signals to an output signal using the given function.
4#[macro_export]
5macro_rules! wire {
6    ( move | $($input:ident),* | $($t:tt)* ) => {
7        {
8            $(
9                let $input = leptos::create_signal(Default::default());
10            )*
11
12            let output = leptos::Signal::derive(move || {
13                (|$($input),*| {
14                    $($t)*
15                })($($input.0.get()),*)
16            });
17
18            (output, $( $input.1 ),*)
19        }  
20    };
21}
22
23/// Calls the input function only if the returned functions result is ok.
24pub fn on_ok<F, T, E>(f: F) -> impl Fn(Result<T, E>)
25where
26    F: Fn(T) + 'static,
27{
28    move |result| {
29        if let Ok(t) = result {
30            f(t)
31        }
32    }   
33}
34
35/// Calls the input function only if the returned functions result is an error.
36pub fn on_err<F, T, E>(f: F) -> impl Fn(Result<T, E>)
37where
38    F: Fn(E) + 'static,
39{
40    move |result| {
41        if let Err(err) = result {
42            f(err)
43        }
44    }   
45}
46
47/// Returns a function that sets a signal.
48pub fn set<T, S>(signal: S) -> impl Fn(T)
49where
50    S: SignalSet<Value = T>,
51{
52    move |t| {
53        signal.set(t);
54    }   
55}
56