nova_forms/components/
select_button.rs

1use std::{fmt::Debug, hash::Hash, str::FromStr};
2
3use leptos::*;
4
5use crate::Icon;
6
7#[component]
8pub fn SelectButton<V, F, G>(
9    #[prop(into)] label: TextProp,
10    #[prop(into, optional)] icon: Option<String>,
11    #[prop(into)] id: String,
12    #[prop(into)] values: Vec<(V, TextProp)>,
13    value: G,
14    on_change: F,
15) -> impl IntoView
16where
17    V: FromStr + ToString + Eq + Hash + Clone + 'static,
18    <V as FromStr>::Err: Debug,
19    F: Fn(V) + Copy + 'static,
20    G: Fn() -> V + Copy + 'static,
21{
22    let options = view! {
23        <For
24            each=move || values.clone()
25            key=|(value, _)| value.clone()
26            children=move |(v, d)| {
27                view! { <option value=v.to_string()>{d}</option> }
28            }
29        />
30    };
31
32    view! {
33        <label class="overlay icon-select button" for=id.clone()>
34            {
35                if let Some(icon) = icon {
36                    view! { <Icon label=label icon=icon /> }.into_view()
37                } else {
38                    view! { <span>{label}</span> }.into_view()
39                }
40            }
41            <select
42                id=id
43                on:change=move |ev| {
44                    let value = event_target_value(&ev);
45                    let value = V::from_str(&value).unwrap();
46                    on_change(value)
47                }
48                prop:value=move || value().to_string()
49                name="language"
50            >
51                {options}
52            </select>
53        </label>
54    }
55}