nova_forms/components/
icon_select.rs

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
use std::{fmt::Debug, hash::Hash, str::FromStr};

use leptos::*;

use crate::Icon;

#[component]
pub fn IconSelect<V, F, G>(
    #[prop(into)] label: TextProp,
    #[prop(into)] icon: String,
    #[prop(into)] id: String,
    #[prop(into)] values: Vec<(V, TextProp)>,
    value: G,
    on_change: F,
) -> impl IntoView
where
    V: FromStr + ToString + Eq + Hash + Clone + 'static,
    <V as FromStr>::Err: Debug,
    F: Fn(V) + Copy + 'static,
    G: Fn() -> V + Copy + 'static,
{
    let options = view! {
        <For
            each=move || values.clone()
            key=|(value, _)| value.clone()
            children=move |(v, d)| {
                view! { <option value=v.to_string()>{d}</option> }
            }
        />
    };

    view! {
        <label class="overlay icon-select button" for=id.clone()>
            <Icon label=label icon=icon />
            <select
                id=id
                on:change=move |ev| {
                    let value = event_target_value(&ev);
                    let value = V::from_str(&value).unwrap();
                    on_change(value)
                }
                prop:value=move || value().to_string()
                name="language"
            >
                {options}
            </select>
        </label>
    }
}