Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/input comp ref #47

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions demo/src/pages/auto_complete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,32 @@ pub fn AutoCompletePage() -> impl IntoView {
</tr>
</tbody>
</Table>
<h3>"AutoComplete Ref"</h3>
<Table single_column=true>
<thead>
<tr>
<th>"Name"</th>
<th>"Type"</th>
<th>"Description"</th>
</tr>
</thead>
<tbody>
<tr>
<td>"focus"</td>
<td>
<Text code=true>"Fn(&self)"</Text>
</td>
<td>"Focus the input element."</td>
</tr>
<tr>
<td>"blur"</td>
<td>
<Text code=true>"Fn(&self)"</Text>
</td>
<td>"Blur the input element."</td>
</tr>
</tbody>
</Table>
</div>
}
}
68 changes: 67 additions & 1 deletion demo/src/pages/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use thaw::*;
#[component]
pub fn InputPage() -> impl IntoView {
let value = create_rw_signal(String::from("o"));
let input_ref = create_component_ref::<InputRef>();
view! {
<div style="width: 896px; margin: 0 auto;">
<h1>"Input"</h1>
Expand Down Expand Up @@ -64,7 +65,46 @@ pub fn InputPage() -> impl IntoView {

</DemoCode>
</Demo>
<h1>"Prefix & Suffix"</h1>
<h3>"Imperative handle"</h3>
<Demo>
<Space vertical=true>
<Space>
<Button on_click=move |_| input_ref.get_untracked().unwrap().focus()>
"Focus"
</Button>
<Button on_click=move |_| input_ref.get_untracked().unwrap().blur()>
"Blur"
</Button>
</Space>
<Input value comp_ref=input_ref/>
</Space>
<DemoCode slot>

{highlight_str!(
r#"
let value = create_rw_signal(String::from("o"));
let input_ref = create_component_ref::<InputRef>();

view! {
<Space vertical=true>
<Space>
<Button on_click=move |_| input_ref.get_untracked().unwrap().focus()>
"Focus"
</Button>
<Button on_click=move |_| input_ref.get_untracked().unwrap().blur()>
"Blur"
</Button>
</Space>
<Input value comp_ref=input_ref/>
</Space>
}
"#,
"rust"
)}

</DemoCode>
</Demo>
<h2>"Prefix & Suffix"</h2>
<Demo>
<Space vertical=true>
<Input value>
Expand Down Expand Up @@ -197,6 +237,32 @@ pub fn InputPage() -> impl IntoView {
</tr>
</tbody>
</Table>
<h3>"Input Ref"</h3>
<Table single_column=true>
<thead>
<tr>
<th>"Name"</th>
<th>"Type"</th>
<th>"Description"</th>
</tr>
</thead>
<tbody>
<tr>
<td>"focus"</td>
<td>
<Text code=true>"Fn(&self)"</Text>
</td>
<td>"Focus the input element."</td>
</tr>
<tr>
<td>"blur"</td>
<td>
<Text code=true>"Fn(&self)"</Text>
</td>
<td>"Blur the input element."</td>
</tr>
</tbody>
</Table>
</div>
}
}
60 changes: 58 additions & 2 deletions src/auto_complete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
components::{Binder, Follower, FollowerPlacement, FollowerWidth},
use_theme,
utils::{mount_style, StoredMaybeSignal},
Input, Theme,
ComponentRef, Input, InputPrefix, InputRef, InputSuffix, Theme,
};
use leptos::*;
pub use theme::AutoCompleteTheme;
Expand All @@ -16,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 @@ -26,6 +36,9 @@ 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"));
let theme = use_theme(Theme::light);
Expand Down Expand Up @@ -108,6 +121,10 @@ pub fn AutoComplete(
}
}
};
let input_ref = ComponentRef::<InputRef>::new();
input_ref.on_load(move |_| {
comp_ref.load(AutoCompleteRef { input_ref });
});

let ssr_class = ssr_class(&class);
view! {
Expand All @@ -121,7 +138,27 @@ pub fn AutoComplete(
on_focus=move |_| open_menu()
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 Expand Up @@ -196,3 +233,22 @@ pub fn AutoComplete(
</Binder>
}
}

#[derive(Clone)]
pub struct AutoCompleteRef {
input_ref: ComponentRef<InputRef>,
}

impl AutoCompleteRef {
pub fn focus(&self) {
if let Some(input_ref) = self.input_ref.get_untracked() {
input_ref.focus();
}
}

pub fn blur(&self) {
if let Some(input_ref) = self.input_ref.get_untracked() {
input_ref.blur();
}
}
}
35 changes: 32 additions & 3 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod theme;

use crate::{
theme::{use_theme, Theme},
utils::mount_style,
utils::{mount_style, ComponentRef},
};
use leptos::*;
pub use theme::InputTheme;
Expand All @@ -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 All @@ -45,6 +49,7 @@ pub fn Input(
#[prop(optional, into)] invalid: MaybeSignal<bool>,
#[prop(optional)] input_prefix: Option<InputPrefix>,
#[prop(optional)] input_suffix: Option<InputSuffix>,
#[prop(optional)] comp_ref: ComponentRef<InputRef>,
) -> impl IntoView {
let theme = use_theme(Theme::light);
mount_style("input", include_str!("./input.css"));
Expand Down Expand Up @@ -114,6 +119,10 @@ pub fn Input(
});
css_vars
});
let input_ref = create_node_ref::<html::Input>();
input_ref.on_load(move |_| {
comp_ref.load(InputRef { input_ref });
});
view! {
<div
class="thaw-input"
Expand All @@ -122,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 @@ -141,9 +150,10 @@ pub fn Input(
class="thaw-input__input-el"
disabled=move || disabled.get()
placeholder=move || placeholder.get()
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 All @@ -152,3 +162,22 @@ pub fn Input(
</div>
}
}

#[derive(Clone)]
pub struct InputRef {
input_ref: NodeRef<html::Input>,
}

impl InputRef {
pub fn focus(&self) {
if let Some(input_el) = self.input_ref.get_untracked() {
_ = input_el.focus();
}
}

pub fn blur(&self) {
if let Some(input_el) = self.input_ref.get_untracked() {
_ = input_el.blur();
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ pub use theme::*;
pub use time_picker::*;
pub use typography::*;
pub use upload::*;
pub use utils::SignalWatch;
pub use utils::{create_component_ref, ComponentRef, SignalWatch};
Loading
Loading