use dioxus::prelude::*;
use crate::class::compose;
use crate::hooks::use_controllable;
use crate::icons::{Icon, lucide};
use crate::{Button, ButtonSize, ButtonVariant};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ChangeSummaryItem {
pub id: String,
pub label: String,
pub before: String,
pub after: String,
pub changed: bool,
}
impl ChangeSummaryItem {
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,
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ChangeSummaryProps {
#[props(default)]
pub items: Vec<ChangeSummaryItem>,
#[props(default = "Changes".to_string(), into)]
pub label: String,
#[props(default = "Before".to_string(), into)]
pub before_label: String,
#[props(default = "After".to_string(), into)]
pub after_label: String,
#[props(default = "Changed".to_string(), into)]
pub changed_label: String,
#[props(default = "Unchanged".to_string(), into)]
pub unchanged_label: String,
#[props(into)]
pub summary: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = section)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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}
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FilterBarFilter {
pub id: String,
pub label: String,
pub value: String,
}
impl FilterBarFilter {
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(),
}
}
}
pub fn filter_bar_remove_label(filter: &FilterBarFilter) -> String {
format!("Remove {}: {}", filter.label, filter.value)
}
#[derive(Props, Clone, PartialEq)]
pub struct FilterBarProps {
#[props(default = "Filters".to_string(), into)]
pub label: String,
#[props(default)]
pub filters: Vec<FilterBarFilter>,
pub on_remove_filter: Option<EventHandler<String>>,
pub on_reset: Option<EventHandler<()>>,
#[props(default = "Reset filters".to_string(), into)]
pub reset_label: String,
pub remove_label: Option<Callback<FilterBarFilter, String>>,
#[props(into)]
pub result_label: Option<String>,
#[props(default)]
pub pending: bool,
pub open: Option<bool>,
#[props(default = true)]
pub default_open: bool,
pub on_open_change: Option<EventHandler<bool>>,
pub actions: Option<Element>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = section)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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}"
}
}
}
}
}