lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! Dashboard parts: `ChangeSummary` and `FilterBar`.

use dioxus::prelude::*;

use crate::class::compose;
use crate::hooks::use_controllable;
use crate::icons::{Icon, lucide};
use crate::{Button, ButtonSize, ButtonVariant};

/// One compared field of a [`ChangeSummary`].
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ChangeSummaryItem {
    /// Stable, unique id.
    pub id: String,
    /// Field name.
    pub label: String,
    /// Current value.
    pub before: String,
    /// Proposed value.
    pub after: String,
    /// Whether the application considers the field changed.
    pub changed: bool,
}

impl ChangeSummaryItem {
    /// A compared field; `changed` is decided by the application.
    pub fn new(
        id: impl Into<String>,
        label: impl Into<String>,
        before: impl Into<String>,
        after: impl Into<String>,
        changed: bool,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            before: before.into(),
            after: after.into(),
            changed,
        }
    }
}

/// Props for [`ChangeSummary`].
#[derive(Props, Clone, PartialEq)]
pub struct ChangeSummaryProps {
    /// The compared fields.
    #[props(default)]
    pub items: Vec<ChangeSummaryItem>,
    /// Heading and accessible name. Defaults to `Changes`.
    #[props(default = "Changes".to_string(), into)]
    pub label: String,
    /// Label of the current value. Defaults to `Before`.
    #[props(default = "Before".to_string(), into)]
    pub before_label: String,
    /// Label of the proposed value. Defaults to `After`.
    #[props(default = "After".to_string(), into)]
    pub after_label: String,
    /// State text of a changed field. Defaults to `Changed`.
    #[props(default = "Changed".to_string(), into)]
    pub changed_label: String,
    /// State text of an unchanged field. Defaults to `Unchanged`.
    #[props(default = "Unchanged".to_string(), into)]
    pub unchanged_label: String,
    /// Localized count text supplied by the application, such as `1 of 2 fields changed`.
    #[props(into)]
    pub summary: Option<String>,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<section>` or global attribute.
    #[props(extends = GlobalAttributes, extends = section)]
    pub attributes: Vec<Attribute>,
    /// Content after the list, such as confirm and discard actions.
    pub children: Element,
}

/// Compares current and proposed values with a visible changed state per field.
#[component]
pub fn ChangeSummary(props: ChangeSummaryProps) -> Element {
    let class = compose([Some("ui-change-summary"), props.class.as_deref()]);
    let label = props.label;

    rsx! {
        section { aria_label: "{label}", class, ..props.attributes,
            h3 { class: "ui-change-summary__heading", "{label}" }
            if let Some(summary) = props.summary.filter(|summary| !summary.is_empty()) {
                p { class: "ui-change-summary__summary", "{summary}" }
            }
            dl { class: "ui-change-summary__list",
                for item in props.items {
                    div {
                        key: "{item.id}",
                        class: "ui-change-summary__item",
                        "data-changed": if item.changed { "true" } else { "false" },
                        dt {
                            "{item.label}"
                            span { class: "ui-change-summary__state",
                                if item.changed {
                                    "{props.changed_label}"
                                } else {
                                    "{props.unchanged_label}"
                                }
                            }
                        }
                        dd {
                            span { class: "ui-change-summary__value-label", "{props.before_label}" }
                            "{item.before}"
                        }
                        dd {
                            span { class: "ui-change-summary__value-label", "{props.after_label}" }
                            "{item.after}"
                        }
                    }
                }
            }
            {props.children}
        }
    }
}

/// One active criterion of a [`FilterBar`].
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FilterBarFilter {
    /// Stable, unique id reported by `on_remove_filter`.
    pub id: String,
    /// Criterion name, such as `Status`.
    pub label: String,
    /// Criterion value, such as `Open`.
    pub value: String,
}

impl FilterBarFilter {
    /// An active criterion.
    pub fn new(id: impl Into<String>, label: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            value: value.into(),
        }
    }
}

/// The default accessible name of a filter's remove button: `Remove {label}: {value}`.
pub fn filter_bar_remove_label(filter: &FilterBarFilter) -> String {
    format!("Remove {}: {}", filter.label, filter.value)
}

/// Props for [`FilterBar`].
#[derive(Props, Clone, PartialEq)]
pub struct FilterBarProps {
    /// Accessible name and disclosure summary. Defaults to `Filters`.
    #[props(default = "Filters".to_string(), into)]
    pub label: String,
    /// Active criteria, shown as chips.
    #[props(default)]
    pub filters: Vec<FilterBarFilter>,
    /// Makes each chip a remove button that reports the filter id.
    pub on_remove_filter: Option<EventHandler<String>>,
    /// Shows a reset button while filters are active.
    pub on_reset: Option<EventHandler<()>>,
    /// Text of the reset button. Defaults to `Reset filters`.
    #[props(default = "Reset filters".to_string(), into)]
    pub reset_label: String,
    /// Accessible name of a remove button. Defaults to [`filter_bar_remove_label`].
    pub remove_label: Option<Callback<FilterBarFilter, String>>,
    /// Complete localized result message, announced politely. Zero and unknown are different.
    #[props(into)]
    pub result_label: Option<String>,
    /// Marks the results as updating and disables the chip buttons.
    #[props(default)]
    pub pending: bool,
    /// Controlled disclosure state of the filter controls.
    pub open: Option<bool>,
    /// Initial disclosure state when uncontrolled. Defaults to open.
    #[props(default = true)]
    pub default_open: bool,
    /// Called with the new disclosure state.
    pub on_open_change: Option<EventHandler<bool>>,
    /// Extra content after the chips, such as a save-view button.
    pub actions: Option<Element>,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<section>` or global attribute.
    #[props(extends = GlobalAttributes, extends = section)]
    pub attributes: Vec<Attribute>,
    /// The filter controls inside the disclosure.
    pub children: Element,
}

/// Groups application-owned dashboard filters, the active criteria, and a result announcement.
///
/// The summary toggles the disclosure in Rust on every renderer instead of relying on the
/// browser's native `<details>` behavior.
#[component]
pub fn FilterBar(props: FilterBarProps) -> Element {
    let open = use_controllable(props.open, || props.default_open, props.on_open_change);
    let class = compose([Some("ui-filter-bar"), props.class.as_deref()]);
    let pending = props.pending;
    let show_reset = props.on_reset.is_some() && !props.filters.is_empty();
    let on_reset = props.on_reset;

    rsx! {
        section {
            aria_busy: if pending { "true" } else { "false" },
            aria_label: "{props.label}",
            class,
            ..props.attributes,
            details { open: open.get(),
                summary {
                    onclick: move |event| {
                        event.prevent_default();
                        open.set(!open.peek());
                    },
                    "{props.label}"
                }
                div { class: "ui-filter-bar__controls", {props.children} }
            }
            div { class: "ui-filter-bar__active",
                for filter in props.filters {
                    if let Some(remove) = props.on_remove_filter {
                        Button {
                            key: "{filter.id}",
                            aria_label: props.remove_label.map_or_else(|| filter_bar_remove_label(&filter), |label| label.call(filter.clone())),
                            disabled: pending,
                            size: ButtonSize::Sm,
                            variant: ButtonVariant::Outline,
                            onclick: {
                                let id = filter.id.clone();
                                move |_| remove.call(id.clone())
                            },
                            "{filter.label}: {filter.value}"
                            Icon { icon: &lucide::X }
                        }
                    } else {
                        span { key: "{filter.id}", "{filter.label}: {filter.value}" }
                    }
                }
                if show_reset {
                    Button {
                        disabled: pending,
                        size: ButtonSize::Sm,
                        variant: ButtonVariant::Ghost,
                        onclick: move |_| {
                            if let Some(handler) = on_reset {
                                handler.call(());
                            }
                        },
                        "{props.reset_label}"
                    }
                }
                if let Some(actions) = props.actions {
                    {actions}
                }
            }
            p {
                aria_atomic: "true",
                aria_live: "polite",
                class: "ui-filter-bar__results",
                role: "status",
                if let Some(result) = props.result_label {
                    "{result}"
                }
            }
        }
    }
}