Skip to main content

lumen_dioxus/inputs/
search_field.rs

1//! `SearchField`: a search input.
2
3use dioxus::prelude::*;
4
5use crate::class::compose;
6
7/// Props for [`SearchField`].
8#[derive(Props, Clone, PartialEq)]
9pub struct SearchFieldProps {
10    /// The native input type. Defaults to `search`.
11    #[props(default = "search".to_string(), into)]
12    pub r#type: String,
13    /// Extra classes appended after the Lumen classes.
14    #[props(into)]
15    pub class: Option<String>,
16    /// Fires on every edit.
17    pub oninput: Option<EventHandler<FormEvent>>,
18    /// Fires when the value is committed.
19    pub onchange: Option<EventHandler<FormEvent>>,
20    /// Fires on every key press, for example to clear the query on Escape.
21    pub onkeydown: Option<EventHandler<KeyboardEvent>>,
22    /// Any other `<input>` or global attribute, such as `value`, `placeholder`, or `aria_label`.
23    #[props(extends = GlobalAttributes, extends = input)]
24    pub attributes: Vec<Attribute>,
25}
26
27/// A text input for search queries. Name it with a `Label`, `aria_label`, or `aria_labelledby`.
28#[component]
29pub fn SearchField(props: SearchFieldProps) -> Element {
30    let class = compose([Some("ui-input ui-search-field"), props.class.as_deref()]);
31    let (oninput, onchange, onkeydown) = (props.oninput, props.onchange, props.onkeydown);
32
33    rsx! {
34        input {
35            class,
36            r#type: props.r#type,
37            oninput: move |event| {
38                if let Some(handler) = oninput {
39                    handler.call(event);
40                }
41            },
42            onchange: move |event| {
43                if let Some(handler) = onchange {
44                    handler.call(event);
45                }
46            },
47            onkeydown: move |event| {
48                if let Some(handler) = onkeydown {
49                    handler.call(event);
50                }
51            },
52            ..props.attributes,
53        }
54    }
55}