lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! `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}"
                    }
                }
            }
        }
    }
}