1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! `Autocomplete`: a search input tied to a `<datalist>` of suggestions.
use dioxus::prelude::*;
use super::SelectOption;
use crate::class::compose;
/// Props for [`Autocomplete`].
#[derive(Props, Clone, PartialEq)]
pub struct AutocompleteProps {
/// Id of the `<datalist>` that holds the suggestions.
#[props(into)]
pub list: String,
/// The native input type. Defaults to `search`.
#[props(default = "search".to_string(), into)]
pub r#type: String,
/// Suggestions to render as a `<datalist>` with the `list` id, right after the input. Leave
/// empty (the default) to point `list` at a datalist you render yourself, like lumen-react.
#[props(default)]
pub options: Vec<SelectOption>,
/// Extra classes appended after the Lumen classes.
#[props(into)]
pub class: Option<String>,
/// Fires on every edit.
pub oninput: Option<EventHandler<FormEvent>>,
/// Fires when the value is committed.
pub onchange: Option<EventHandler<FormEvent>>,
/// Any other `<input>` or global attribute, such as `value`, `name`, or `placeholder`.
#[props(extends = GlobalAttributes, extends = input)]
pub attributes: Vec<Attribute>,
}
/// A search input with `role="combobox"` that suggests values from a `<datalist>`.
///
/// Browsers and desktop webviews draw the datalist's suggestion popup. Dioxus Native has no
/// datalist popup, so the input works there as a plain search field; use
/// [`Combobox`](crate::Combobox) when the suggestions must show on every renderer.
#[component]
pub fn Autocomplete(props: AutocompleteProps) -> Element {
let class = compose([Some("ui-input ui-autocomplete"), props.class.as_deref()]);
let (oninput, onchange) = (props.oninput, props.onchange);
let list = props.list.clone();
rsx! {
input {
aria_autocomplete: "list",
class,
list: list.clone(),
r#type: props.r#type,
oninput: move |event| {
if let Some(handler) = oninput {
handler.call(event);
}
},
onchange: move |event| {
if let Some(handler) = onchange {
handler.call(event);
}
},
..props.attributes,
}
if !props.options.is_empty() {
datalist { id: props.list,
for (index, option) in props.options.into_iter().enumerate() {
option {
key: "{index}",
disabled: option.disabled,
value: option.value.clone(),
"{option.label}"
}
}
}
}
}
}