Skip to main content

lumen_dioxus/data/
dashboard.rs

1//! Dashboard parts: `ChangeSummary` and `FilterBar`.
2
3use dioxus::prelude::*;
4
5use crate::class::compose;
6use crate::hooks::use_controllable;
7use crate::icons::{Icon, lucide};
8use crate::{Button, ButtonSize, ButtonVariant};
9
10/// One compared field of a [`ChangeSummary`].
11#[derive(Clone, Debug, Default, PartialEq, Eq)]
12pub struct ChangeSummaryItem {
13    /// Stable, unique id.
14    pub id: String,
15    /// Field name.
16    pub label: String,
17    /// Current value.
18    pub before: String,
19    /// Proposed value.
20    pub after: String,
21    /// Whether the application considers the field changed.
22    pub changed: bool,
23}
24
25impl ChangeSummaryItem {
26    /// A compared field; `changed` is decided by the application.
27    pub fn new(
28        id: impl Into<String>,
29        label: impl Into<String>,
30        before: impl Into<String>,
31        after: impl Into<String>,
32        changed: bool,
33    ) -> Self {
34        Self {
35            id: id.into(),
36            label: label.into(),
37            before: before.into(),
38            after: after.into(),
39            changed,
40        }
41    }
42}
43
44/// Props for [`ChangeSummary`].
45#[derive(Props, Clone, PartialEq)]
46pub struct ChangeSummaryProps {
47    /// The compared fields.
48    #[props(default)]
49    pub items: Vec<ChangeSummaryItem>,
50    /// Heading and accessible name. Defaults to `Changes`.
51    #[props(default = "Changes".to_string(), into)]
52    pub label: String,
53    /// Label of the current value. Defaults to `Before`.
54    #[props(default = "Before".to_string(), into)]
55    pub before_label: String,
56    /// Label of the proposed value. Defaults to `After`.
57    #[props(default = "After".to_string(), into)]
58    pub after_label: String,
59    /// State text of a changed field. Defaults to `Changed`.
60    #[props(default = "Changed".to_string(), into)]
61    pub changed_label: String,
62    /// State text of an unchanged field. Defaults to `Unchanged`.
63    #[props(default = "Unchanged".to_string(), into)]
64    pub unchanged_label: String,
65    /// Localized count text supplied by the application, such as `1 of 2 fields changed`.
66    #[props(into)]
67    pub summary: Option<String>,
68    /// Extra classes appended after the Lumen class.
69    #[props(into)]
70    pub class: Option<String>,
71    /// Any other `<section>` or global attribute.
72    #[props(extends = GlobalAttributes, extends = section)]
73    pub attributes: Vec<Attribute>,
74    /// Content after the list, such as confirm and discard actions.
75    pub children: Element,
76}
77
78/// Compares current and proposed values with a visible changed state per field.
79#[component]
80pub fn ChangeSummary(props: ChangeSummaryProps) -> Element {
81    let class = compose([Some("ui-change-summary"), props.class.as_deref()]);
82    let label = props.label;
83
84    rsx! {
85        section { aria_label: "{label}", class, ..props.attributes,
86            h3 { class: "ui-change-summary__heading", "{label}" }
87            if let Some(summary) = props.summary.filter(|summary| !summary.is_empty()) {
88                p { class: "ui-change-summary__summary", "{summary}" }
89            }
90            dl { class: "ui-change-summary__list",
91                for item in props.items {
92                    div {
93                        key: "{item.id}",
94                        class: "ui-change-summary__item",
95                        "data-changed": if item.changed { "true" } else { "false" },
96                        dt {
97                            "{item.label}"
98                            span { class: "ui-change-summary__state",
99                                if item.changed {
100                                    "{props.changed_label}"
101                                } else {
102                                    "{props.unchanged_label}"
103                                }
104                            }
105                        }
106                        dd {
107                            span { class: "ui-change-summary__value-label", "{props.before_label}" }
108                            "{item.before}"
109                        }
110                        dd {
111                            span { class: "ui-change-summary__value-label", "{props.after_label}" }
112                            "{item.after}"
113                        }
114                    }
115                }
116            }
117            {props.children}
118        }
119    }
120}
121
122/// One active criterion of a [`FilterBar`].
123#[derive(Clone, Debug, Default, PartialEq, Eq)]
124pub struct FilterBarFilter {
125    /// Stable, unique id reported by `on_remove_filter`.
126    pub id: String,
127    /// Criterion name, such as `Status`.
128    pub label: String,
129    /// Criterion value, such as `Open`.
130    pub value: String,
131}
132
133impl FilterBarFilter {
134    /// An active criterion.
135    pub fn new(id: impl Into<String>, label: impl Into<String>, value: impl Into<String>) -> Self {
136        Self {
137            id: id.into(),
138            label: label.into(),
139            value: value.into(),
140        }
141    }
142}
143
144/// The default accessible name of a filter's remove button: `Remove {label}: {value}`.
145pub fn filter_bar_remove_label(filter: &FilterBarFilter) -> String {
146    format!("Remove {}: {}", filter.label, filter.value)
147}
148
149/// Props for [`FilterBar`].
150#[derive(Props, Clone, PartialEq)]
151pub struct FilterBarProps {
152    /// Accessible name and disclosure summary. Defaults to `Filters`.
153    #[props(default = "Filters".to_string(), into)]
154    pub label: String,
155    /// Active criteria, shown as chips.
156    #[props(default)]
157    pub filters: Vec<FilterBarFilter>,
158    /// Makes each chip a remove button that reports the filter id.
159    pub on_remove_filter: Option<EventHandler<String>>,
160    /// Shows a reset button while filters are active.
161    pub on_reset: Option<EventHandler<()>>,
162    /// Text of the reset button. Defaults to `Reset filters`.
163    #[props(default = "Reset filters".to_string(), into)]
164    pub reset_label: String,
165    /// Accessible name of a remove button. Defaults to [`filter_bar_remove_label`].
166    pub remove_label: Option<Callback<FilterBarFilter, String>>,
167    /// Complete localized result message, announced politely. Zero and unknown are different.
168    #[props(into)]
169    pub result_label: Option<String>,
170    /// Marks the results as updating and disables the chip buttons.
171    #[props(default)]
172    pub pending: bool,
173    /// Controlled disclosure state of the filter controls.
174    pub open: Option<bool>,
175    /// Initial disclosure state when uncontrolled. Defaults to open.
176    #[props(default = true)]
177    pub default_open: bool,
178    /// Called with the new disclosure state.
179    pub on_open_change: Option<EventHandler<bool>>,
180    /// Extra content after the chips, such as a save-view button.
181    pub actions: Option<Element>,
182    /// Extra classes appended after the Lumen class.
183    #[props(into)]
184    pub class: Option<String>,
185    /// Any other `<section>` or global attribute.
186    #[props(extends = GlobalAttributes, extends = section)]
187    pub attributes: Vec<Attribute>,
188    /// The filter controls inside the disclosure.
189    pub children: Element,
190}
191
192/// Groups application-owned dashboard filters, the active criteria, and a result announcement.
193///
194/// The summary toggles the disclosure in Rust on every renderer instead of relying on the
195/// browser's native `<details>` behavior.
196#[component]
197pub fn FilterBar(props: FilterBarProps) -> Element {
198    let open = use_controllable(props.open, || props.default_open, props.on_open_change);
199    let class = compose([Some("ui-filter-bar"), props.class.as_deref()]);
200    let pending = props.pending;
201    let show_reset = props.on_reset.is_some() && !props.filters.is_empty();
202    let on_reset = props.on_reset;
203
204    rsx! {
205        section {
206            aria_busy: if pending { "true" } else { "false" },
207            aria_label: "{props.label}",
208            class,
209            ..props.attributes,
210            details { open: open.get(),
211                summary {
212                    onclick: move |event| {
213                        event.prevent_default();
214                        open.set(!open.peek());
215                    },
216                    "{props.label}"
217                }
218                div { class: "ui-filter-bar__controls", {props.children} }
219            }
220            div { class: "ui-filter-bar__active",
221                for filter in props.filters {
222                    if let Some(remove) = props.on_remove_filter {
223                        Button {
224                            key: "{filter.id}",
225                            aria_label: props.remove_label.map_or_else(|| filter_bar_remove_label(&filter), |label| label.call(filter.clone())),
226                            disabled: pending,
227                            size: ButtonSize::Sm,
228                            variant: ButtonVariant::Outline,
229                            onclick: {
230                                let id = filter.id.clone();
231                                move |_| remove.call(id.clone())
232                            },
233                            "{filter.label}: {filter.value}"
234                            Icon { icon: &lucide::X }
235                        }
236                    } else {
237                        span { key: "{filter.id}", "{filter.label}: {filter.value}" }
238                    }
239                }
240                if show_reset {
241                    Button {
242                        disabled: pending,
243                        size: ButtonSize::Sm,
244                        variant: ButtonVariant::Ghost,
245                        onclick: move |_| {
246                            if let Some(handler) = on_reset {
247                                handler.call(());
248                            }
249                        },
250                        "{props.reset_label}"
251                    }
252                }
253                if let Some(actions) = props.actions {
254                    {actions}
255                }
256            }
257            p {
258                aria_atomic: "true",
259                aria_live: "polite",
260                class: "ui-filter-bar__results",
261                role: "status",
262                if let Some(result) = props.result_label {
263                    "{result}"
264                }
265            }
266        }
267    }
268}