lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! `NativeSelect`: the browser's own `<select>` with Lumen styling.

// cspell:words optgroup

use dioxus::prelude::*;

use crate::class::compose;
use crate::{ControlSize, SelectOption};

/// Props for [`NativeSelect`].
#[derive(Props, Clone, PartialEq)]
pub struct NativeSelectProps {
    /// Generated `<option>` elements, rendered after `children`.
    #[props(default)]
    pub options: Vec<SelectOption>,
    /// Adds a first, disabled `<option value="">` with this text. It starts selected when neither
    /// `value` nor `default_value` is set.
    #[props(into)]
    pub placeholder: Option<String>,
    /// Controlled value: the option with this value is marked `selected`.
    #[props(into)]
    pub value: Option<String>,
    /// Initial value when uncontrolled.
    #[props(into)]
    pub default_value: Option<String>,
    /// Control height.
    #[props(default)]
    pub visual_size: ControlSize,
    /// Fires on every change of the selected option.
    pub oninput: Option<EventHandler<FormEvent>>,
    /// Fires when the selected option changes; `event.value()` is the new value.
    pub onchange: Option<EventHandler<FormEvent>>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<select>` or global attribute, such as `id`, `name`, `disabled`, or `required`.
    #[props(extends = GlobalAttributes, extends = select)]
    pub attributes: Vec<Attribute>,
    /// Extra `<option>` or `<optgroup>` elements, rendered after the placeholder. Mark their
    /// selected state with `selected` yourself.
    pub children: Element,
}

/// A styled native `<select>`. Use it when the platform picker is a requirement; otherwise prefer
/// [`Select`](crate::Select), which also works on Dioxus Native.
#[component]
pub fn NativeSelect(props: NativeSelectProps) -> Element {
    let placeholder = props.placeholder.clone().filter(|text| !text.is_empty());
    let initial = props
        .default_value
        .clone()
        .or_else(|| placeholder.as_ref().map(|_| String::new()));
    let mut current = use_signal(|| initial);
    let selected = props.value.clone().or_else(|| current.cloned());
    let is_selected = |value: &str| selected.as_deref() == Some(value);
    let size = match props.visual_size {
        ControlSize::Default => None,
        ControlSize::Sm => Some("ui-select--sm"),
        ControlSize::Lg => Some("ui-select--lg"),
    };
    let class = compose([Some("ui-select"), size, props.class.as_deref()]);
    let (oninput, onchange) = (props.oninput, props.onchange);

    rsx! {
        select {
            class,
            oninput: move |event| {
                if let Some(handler) = oninput {
                    handler.call(event);
                }
            },
            onchange: move |event: FormEvent| {
                current.set(Some(event.value()));

                if let Some(handler) = onchange {
                    handler.call(event);
                }
            },
            ..props.attributes,
            if let Some(text) = placeholder {
                option { disabled: true, selected: is_selected(""), value: "", "{text}" }
            }
            {props.children}
            for choice in props.options.iter() {
                option {
                    disabled: choice.disabled,
                    selected: is_selected(&choice.value),
                    value: choice.value.clone(),
                    "{choice.label}"
                }
            }
        }
    }
}