Skip to content

Commit

Permalink
feat: AutoComplete adds blur_after_select prop (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiaozero authored Mar 15, 2024
1 parent f996a3b commit 12273fc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
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

0 comments on commit 12273fc

Please sign in to comment.