1use dioxus::prelude::*;
4
5use crate::class::compose;
6use crate::hooks::use_controllable;
7use crate::icons::{Icon, lucide};
8use crate::{Button, ButtonSize, ButtonVariant};
9
10#[derive(Clone, Debug, Default, PartialEq, Eq)]
12pub struct ChangeSummaryItem {
13 pub id: String,
15 pub label: String,
17 pub before: String,
19 pub after: String,
21 pub changed: bool,
23}
24
25impl ChangeSummaryItem {
26 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#[derive(Props, Clone, PartialEq)]
46pub struct ChangeSummaryProps {
47 #[props(default)]
49 pub items: Vec<ChangeSummaryItem>,
50 #[props(default = "Changes".to_string(), into)]
52 pub label: String,
53 #[props(default = "Before".to_string(), into)]
55 pub before_label: String,
56 #[props(default = "After".to_string(), into)]
58 pub after_label: String,
59 #[props(default = "Changed".to_string(), into)]
61 pub changed_label: String,
62 #[props(default = "Unchanged".to_string(), into)]
64 pub unchanged_label: String,
65 #[props(into)]
67 pub summary: Option<String>,
68 #[props(into)]
70 pub class: Option<String>,
71 #[props(extends = GlobalAttributes, extends = section)]
73 pub attributes: Vec<Attribute>,
74 pub children: Element,
76}
77
78#[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#[derive(Clone, Debug, Default, PartialEq, Eq)]
124pub struct FilterBarFilter {
125 pub id: String,
127 pub label: String,
129 pub value: String,
131}
132
133impl FilterBarFilter {
134 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
144pub fn filter_bar_remove_label(filter: &FilterBarFilter) -> String {
146 format!("Remove {}: {}", filter.label, filter.value)
147}
148
149#[derive(Props, Clone, PartialEq)]
151pub struct FilterBarProps {
152 #[props(default = "Filters".to_string(), into)]
154 pub label: String,
155 #[props(default)]
157 pub filters: Vec<FilterBarFilter>,
158 pub on_remove_filter: Option<EventHandler<String>>,
160 pub on_reset: Option<EventHandler<()>>,
162 #[props(default = "Reset filters".to_string(), into)]
164 pub reset_label: String,
165 pub remove_label: Option<Callback<FilterBarFilter, String>>,
167 #[props(into)]
169 pub result_label: Option<String>,
170 #[props(default)]
172 pub pending: bool,
173 pub open: Option<bool>,
175 #[props(default = true)]
177 pub default_open: bool,
178 pub on_open_change: Option<EventHandler<bool>>,
180 pub actions: Option<Element>,
182 #[props(into)]
184 pub class: Option<String>,
185 #[props(extends = GlobalAttributes, extends = section)]
187 pub attributes: Vec<Attribute>,
188 pub children: Element,
190}
191
192#[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}