2023-11-01 14:04:12 +08:00
|
|
|
mod theme;
|
|
|
|
|
2023-11-11 00:38:31 +08:00
|
|
|
use crate::{
|
2023-11-12 02:34:40 +08:00
|
|
|
components::{Binder, Follower, FollowerPlacement, FollowerWidth},
|
2023-11-13 16:17:45 +08:00
|
|
|
use_theme,
|
|
|
|
utils::{mount_style, StoredMaybeSignal},
|
2023-11-11 00:38:31 +08:00
|
|
|
Input, Theme,
|
|
|
|
};
|
2023-10-15 12:23:26 +08:00
|
|
|
use leptos::*;
|
2023-11-01 14:04:12 +08:00
|
|
|
pub use theme::AutoCompleteTheme;
|
2023-10-15 12:23:26 +08:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
pub struct AutoCompleteOption {
|
|
|
|
pub label: String,
|
|
|
|
pub value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn AutoComplete(
|
2023-11-09 14:43:30 +08:00
|
|
|
#[prop(optional, into)] value: RwSignal<String>,
|
2023-10-15 12:23:26 +08:00
|
|
|
#[prop(optional, into)] placeholder: MaybeSignal<String>,
|
|
|
|
#[prop(optional, into)] options: MaybeSignal<Vec<AutoCompleteOption>>,
|
2023-11-08 21:53:49 +08:00
|
|
|
#[prop(optional, into)] clear_after_select: MaybeSignal<bool>,
|
|
|
|
#[prop(optional, into)] on_select: Option<Callback<String>>,
|
2023-10-15 12:23:26 +08:00
|
|
|
) -> impl IntoView {
|
|
|
|
mount_style("auto-complete", include_str!("./auto-complete.css"));
|
2023-11-01 14:04:12 +08:00
|
|
|
let theme = use_theme(Theme::light);
|
|
|
|
let menu_css_vars = create_memo(move |_| {
|
|
|
|
let mut css_vars = String::new();
|
|
|
|
theme.with(|theme| {
|
|
|
|
css_vars.push_str(&format!(
|
2023-11-05 16:03:58 +08:00
|
|
|
"--thaw-background-color: {};",
|
2023-11-01 14:04:12 +08:00
|
|
|
theme.select.menu_background_color
|
|
|
|
));
|
|
|
|
css_vars.push_str(&format!(
|
2023-11-05 16:03:58 +08:00
|
|
|
"--thaw-background-color-hover: {};",
|
2023-11-01 14:04:12 +08:00
|
|
|
theme.select.menu_background_color_hover
|
|
|
|
));
|
|
|
|
});
|
|
|
|
css_vars
|
|
|
|
});
|
|
|
|
|
2023-10-15 12:23:26 +08:00
|
|
|
let is_show_menu = create_rw_signal(false);
|
|
|
|
let auto_complete_ref = create_node_ref::<html::Div>();
|
2023-11-08 21:53:49 +08:00
|
|
|
let options = StoredMaybeSignal::from(options);
|
2023-10-15 12:23:26 +08:00
|
|
|
let allow_value = move |_| {
|
|
|
|
if !is_show_menu.get_untracked() {
|
2023-11-11 00:38:31 +08:00
|
|
|
is_show_menu.set(true);
|
2023-10-15 12:23:26 +08:00
|
|
|
}
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
view! {
|
2023-11-11 00:38:31 +08:00
|
|
|
<Binder target_ref=auto_complete_ref>
|
|
|
|
<div class="thaw-auto-complete" ref=auto_complete_ref>
|
|
|
|
<Input
|
|
|
|
value
|
|
|
|
placeholder
|
|
|
|
on_focus=move |_| is_show_menu.set(true)
|
|
|
|
on_blur=move |_| is_show_menu.set(false)
|
|
|
|
allow_value
|
|
|
|
/>
|
|
|
|
</div>
|
2023-11-19 14:06:23 +08:00
|
|
|
<Follower
|
|
|
|
slot
|
|
|
|
show=is_show_menu
|
|
|
|
placement=FollowerPlacement::BottomStart
|
|
|
|
width=FollowerWidth::Target
|
|
|
|
>
|
|
|
|
<div class="thaw-auto-complete__menu" style=move || menu_css_vars.get()>
|
2023-10-15 12:23:26 +08:00
|
|
|
|
2023-11-11 00:38:31 +08:00
|
|
|
{move || {
|
|
|
|
options
|
|
|
|
.get()
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
let AutoCompleteOption { value: option_value, label } = v;
|
|
|
|
let on_click = move |_| {
|
|
|
|
if clear_after_select.get_untracked() {
|
|
|
|
value.set(String::new());
|
|
|
|
} else {
|
|
|
|
value.set(option_value.clone());
|
|
|
|
}
|
|
|
|
if let Some(on_select) = on_select {
|
|
|
|
on_select.call(option_value.clone());
|
|
|
|
}
|
|
|
|
is_show_menu.set(false);
|
|
|
|
};
|
|
|
|
let on_mousedown = move |ev: ev::MouseEvent| {
|
|
|
|
ev.prevent_default();
|
|
|
|
};
|
|
|
|
view! {
|
|
|
|
<div
|
|
|
|
class="thaw-auto-complete__menu-item"
|
|
|
|
on:click=on_click
|
|
|
|
on:mousedown=on_mousedown
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</div>
|
2023-11-08 21:53:49 +08:00
|
|
|
}
|
2023-11-11 00:38:31 +08:00
|
|
|
})
|
|
|
|
.collect_view()
|
|
|
|
}}
|
2023-10-15 12:23:26 +08:00
|
|
|
|
2023-11-11 00:38:31 +08:00
|
|
|
</div>
|
|
|
|
</Follower>
|
|
|
|
</Binder>
|
2023-10-15 12:23:26 +08:00
|
|
|
}
|
|
|
|
}
|