Skip to content

Commit

Permalink
feat: AutoComplete component add prefix and suffix slot
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiaozero committed Dec 14, 2023
1 parent be8e6df commit d8e0dfb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ icondata = { version = "0.1.0", features = [
"AiCheckOutlined",
"AiGithubOutlined",
"AiUserOutlined",
"AiSearchOutlined",
] }
prisms = { git = "https://github.com/luoxiaozero/prisms", rev = "16d4d34b93fc20578ebf03137d54ecc7eafa4d4b" }

Expand Down
20 changes: 18 additions & 2 deletions demo/src/components/site_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ pub fn SiteHeader() -> impl IntoView {
let navigate = use_navigate();
navigate(&path, Default::default());
};
let auto_complete_ref = create_component_ref::<AutoCompleteRef>();
let handle = window_event_listener(ev::keydown, move |event| {
let key = event.key();
if key == *"/" {
if let Some(auto_complete_ref) = auto_complete_ref.get_untracked() {
event.prevent_default();
auto_complete_ref.focus();
}
}
});
on_cleanup(move || handle.remove());
view! {
<LayoutHeader style>
<Space>
Expand All @@ -84,11 +95,16 @@ pub fn SiteHeader() -> impl IntoView {
<Space>
<AutoComplete
value=search_value
placeholder="Search"
placeholder="Type '/' to search"
options=search_options
clear_after_select=true
on_select=on_search_select
/>
comp_ref=auto_complete_ref
>
<AutoCompletePrefix slot>
<Icon icon=icondata::Icon::from(icondata::AiIcon::AiSearchOutlined) style="font-size: 18px; color: var(--thaw-placeholder-color);"/>
</AutoCompletePrefix>
</AutoComplete>
<Button
variant=ButtonVariant::Text
on_click=move |_| {
Expand Down
36 changes: 33 additions & 3 deletions src/auto_complete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use crate::{
components::{Binder, Follower, FollowerPlacement, FollowerWidth},
use_theme,
utils::{mount_style, StoredMaybeSignal},
Input, Theme,
ComponentRef, Input, InputPrefix, InputRef, InputSuffix, Theme,
};
use crate::{ComponentRef, InputRef};
use leptos::*;
pub use theme::AutoCompleteTheme;

Expand All @@ -17,6 +16,16 @@ pub struct AutoCompleteOption {
pub value: String,
}

#[slot]
pub struct AutoCompletePrefix {
children: Children,
}

#[slot]
pub struct AutoCompleteSuffix {
children: Children,
}

#[component]
pub fn AutoComplete(
#[prop(optional, into)] value: RwSignal<String>,
Expand All @@ -27,6 +36,8 @@ pub fn AutoComplete(
#[prop(optional, into)] disabled: MaybeSignal<bool>,
#[prop(optional, into)] invalid: MaybeSignal<bool>,
#[prop(optional, into)] class: MaybeSignal<String>,
#[prop(optional)] auto_complete_prefix: Option<AutoCompletePrefix>,
#[prop(optional)] auto_complete_suffix: Option<AutoCompleteSuffix>,
#[prop(optional)] comp_ref: ComponentRef<AutoCompleteRef>,
) -> impl IntoView {
mount_style("auto-complete", include_str!("./auto-complete.css"));
Expand Down Expand Up @@ -128,7 +139,26 @@ pub fn AutoComplete(
on_blur=move |_| is_show_menu.set(false)
allow_value
comp_ref=input_ref
/>
>
<InputPrefix if_=auto_complete_prefix.is_some() slot>
{
if let Some(auto_complete_prefix) = auto_complete_prefix {
Some((auto_complete_prefix.children)())
} else {
None
}
}
</InputPrefix>
<InputSuffix if_=auto_complete_suffix.is_some() slot>
{
if let Some(auto_complete_suffix) = auto_complete_suffix {
Some((auto_complete_suffix.children)())
} else {
None
}
}
</InputSuffix>
</Input>
</div>
<Follower
slot
Expand Down
8 changes: 6 additions & 2 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ impl InputVariant {

#[slot]
pub struct InputPrefix {
#[prop(default = true)]
if_: bool,
children: Children,
}

#[slot]
pub struct InputSuffix {
#[prop(default = true)]
if_: bool,
children: Children,
}

Expand Down Expand Up @@ -127,7 +131,7 @@ pub fn Input(
class=("thaw-input--invalid", move || invalid.get())
style=move || css_vars.get()
>
{if let Some(prefix) = input_prefix {
{if let Some(prefix) = input_prefix.map(|prefix| prefix.if_.then(|| prefix)).flatten() {
view! { <div class="thaw-input__prefix">{(prefix.children)()}</div> }.into()
} else {
None
Expand All @@ -149,7 +153,7 @@ pub fn Input(
ref=input_ref
/>

{if let Some(suffix) = input_suffix {
{if let Some(suffix) = input_suffix.map(|suffix| suffix.if_.then(|| suffix)).flatten() {
view! { <div class="thaw-input__suffix">{(suffix.children)()}</div> }.into()
} else {
None
Expand Down

0 comments on commit d8e0dfb

Please sign in to comment.