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: AutoComplete adds blur_after_select prop #140

Merged
merged 1 commit into from
Mar 15, 2024
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/src/components/site_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub fn SiteHeader() -> impl IntoView {
placeholder="Type '/' to search"
options=search_options
clear_after_select=true
blur_after_select=true
on_select=on_search_select
comp_ref=auto_complete_ref
>
Expand Down
1 change: 1 addition & 0 deletions demo_markdown/docs/auto_complete/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ view! {
| allow_free_input | `bool` | `false` | Whether free text input is allowed. |
| invalid | `MaybeSignal<bool>` | `false` | Whether the input is invalid. |
| clear_after_select | `MaybeSignal<bool>` | `false` | Whether to clear after selection. |
| blur_after_select | `MaybeSignal<bool>` | `false` | Whether to blur after selection. |
| on_select | `Option<Callback<String>>` | `None` | On select callback function. |
| attr: | `Vec<(&'static str, Attribute)>` | `Default::default()` | The dom attrs of the input element inside the component. |

Expand Down
9 changes: 8 additions & 1 deletion thaw/src/auto_complete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn AutoComplete(
#[prop(optional, into)] placeholder: OptionalProp<MaybeSignal<String>>,
#[prop(optional, into)] options: MaybeSignal<Vec<AutoCompleteOption>>,
#[prop(optional, into)] clear_after_select: MaybeSignal<bool>,
#[prop(optional, into)] blur_after_select: MaybeSignal<bool>,
#[prop(optional, into)] on_select: Option<Callback<String>>,
#[prop(optional, into)] disabled: MaybeSignal<bool>,
#[prop(optional, into)] allow_free_input: bool,
Expand Down Expand Up @@ -58,6 +59,8 @@ pub fn AutoComplete(
css_vars
});

let input_ref = ComponentRef::<InputRef>::new();

let default_index = if allow_free_input { None } else { Some(0) };

let select_option_index = create_rw_signal::<Option<usize>>(default_index);
Expand Down Expand Up @@ -89,6 +92,11 @@ pub fn AutoComplete(
select_option_index.set(None);
}
is_show_menu.set(false);
if blur_after_select.get_untracked() {
if let Some(input_ref) = input_ref.get_untracked() {
input_ref.blur();
}
}
};

// we unset selection index whenever options get changed
Expand Down Expand Up @@ -147,7 +155,6 @@ pub fn AutoComplete(
}
}
};
let input_ref = ComponentRef::<InputRef>::new();
input_ref.on_load(move |_| {
comp_ref.load(AutoCompleteRef { input_ref });
});
Expand Down
Loading