Skip to main content

lumen_dioxus/data/
display.rs

1//! `Rating`, `Timeline` and `TimelineItem`, and `Descriptions` with its composable parts.
2
3use dioxus::prelude::*;
4
5use crate::class::compose;
6use crate::hooks::use_controllable;
7use crate::icons::{Icon, lucide};
8
9/// Props for [`Rating`].
10#[derive(Props, Clone, PartialEq)]
11pub struct RatingProps {
12    /// Controlled value. Leave unset to let the rating manage itself.
13    pub value: Option<f64>,
14    /// Initial value when uncontrolled.
15    #[props(default)]
16    pub default_value: f64,
17    /// Number of stars. Defaults to 5.
18    #[props(default = 5)]
19    pub max: u32,
20    /// Disables the stars and shows the rating as read-only.
21    #[props(default)]
22    pub readonly: bool,
23    /// Called with the star the user picked, from 1 to `max`.
24    pub on_value_change: Option<EventHandler<f64>>,
25    /// Extra classes appended after the Lumen classes.
26    #[props(into)]
27    pub class: Option<String>,
28    /// Any other `<div>` or global attribute.
29    #[props(extends = GlobalAttributes, extends = div)]
30    pub attributes: Vec<Attribute>,
31}
32
33/// A row of star buttons. Each star is named `Rate {star} out of {max}`; activating one sets the
34/// rating, which lumen-react leaves to the application.
35#[component]
36pub fn Rating(props: RatingProps) -> Element {
37    let rating = use_controllable(props.value, || props.default_value, props.on_value_change);
38    let value = rating.get();
39    let max = props.max;
40    let readonly = props.readonly;
41    let class = compose([
42        Some("ui-rating"),
43        readonly.then_some("ui-rating--readonly"),
44        props.class.as_deref(),
45    ]);
46
47    rsx! {
48        div {
49            class,
50            "data-max": "{max}",
51            "data-readonly": if readonly { "true" } else { "false" },
52            "data-ui-rating": "true",
53            "data-value": "{value}",
54            ..props.attributes,
55            for star in 1..=max {
56                button {
57                    key: "{star}",
58                    aria_label: "Rate {star} out of {max}",
59                    class: "ui-rating__star",
60                    "data-active": if f64::from(star) <= value { "true" } else { "false" },
61                    disabled: readonly,
62                    r#type: "button",
63                    onclick: move |_| {
64                        if !readonly {
65                            rating.set(f64::from(star));
66                        }
67                    },
68                    Icon { icon: &lucide::STAR }
69                }
70            }
71        }
72    }
73}
74
75/// Props for [`Timeline`].
76#[derive(Props, Clone, PartialEq)]
77pub struct TimelineProps {
78    /// Extra classes appended after the Lumen class.
79    #[props(into)]
80    pub class: Option<String>,
81    /// Any other `<ol>` or global attribute.
82    #[props(extends = GlobalAttributes, extends = ol)]
83    pub attributes: Vec<Attribute>,
84    /// The [`TimelineItem`]s.
85    pub children: Element,
86}
87
88/// An ordered list of events.
89#[component]
90pub fn Timeline(props: TimelineProps) -> Element {
91    let class = compose([Some("ui-timeline"), props.class.as_deref()]);
92
93    rsx! {
94        ol { class, "data-slot": "timeline", ..props.attributes, {props.children} }
95    }
96}
97
98/// Props for [`TimelineItem`].
99#[derive(Props, Clone, PartialEq)]
100pub struct TimelineItemProps {
101    /// Content of the marker, such as a step number or an icon.
102    pub dot: Option<Element>,
103    /// Extra classes appended after the Lumen class.
104    #[props(into)]
105    pub class: Option<String>,
106    /// Any other `<li>` or global attribute.
107    #[props(extends = GlobalAttributes, extends = li)]
108    pub attributes: Vec<Attribute>,
109    /// Event content.
110    pub children: Element,
111}
112
113/// One event in a [`Timeline`]: a tail, a dot, and content.
114#[component]
115pub fn TimelineItem(props: TimelineItemProps) -> Element {
116    let class = compose([Some("ui-timeline-item"), props.class.as_deref()]);
117
118    rsx! {
119        li { class, "data-slot": "timeline-item", ..props.attributes,
120            div { class: "ui-timeline-item__tail", "data-slot": "timeline-item-tail" }
121            div { class: "ui-timeline-item__dot", "data-slot": "timeline-item-dot",
122                if let Some(dot) = props.dot {
123                    {dot}
124                }
125            }
126            div { class: "ui-timeline-item__content", "data-slot": "timeline-item-content", {props.children} }
127        }
128    }
129}
130
131/// One plain-text row of [`Descriptions`].
132#[derive(Clone, Debug, PartialEq, Eq)]
133pub struct DescriptionsItem {
134    /// The term.
135    pub label: String,
136    /// The detail.
137    pub value: String,
138}
139
140impl DescriptionsItem {
141    /// A row with a term and a detail.
142    pub fn new(label: impl Into<String>, value: impl Into<String>) -> Self {
143        Self {
144            label: label.into(),
145            value: value.into(),
146        }
147    }
148}
149
150/// Props for [`Descriptions`].
151#[derive(Props, Clone, PartialEq)]
152pub struct DescriptionsProps {
153    /// Number of columns. Defaults to 1.
154    #[props(default = 1)]
155    pub columns: u32,
156    /// Plain-text rows rendered before the children.
157    #[props(default)]
158    pub items: Vec<DescriptionsItem>,
159    /// Extra classes appended after the Lumen class.
160    #[props(into)]
161    pub class: Option<String>,
162    /// Any other `<dl>` or global attribute.
163    #[props(extends = GlobalAttributes, extends = dl)]
164    pub attributes: Vec<Attribute>,
165    /// Rich rows composed with [`DescriptionItem`], [`DescriptionTerm`], and [`DescriptionDetail`].
166    pub children: Element,
167}
168
169/// A key-value description list laid out in columns.
170#[component]
171pub fn Descriptions(props: DescriptionsProps) -> Element {
172    let class = compose([Some("ui-descriptions"), props.class.as_deref()]);
173    let columns = props.columns;
174
175    rsx! {
176        dl {
177            class,
178            "data-columns": "{columns}",
179            style: "--ui-descriptions-columns:{columns}",
180            ..props.attributes,
181            for (index, item) in props.items.into_iter().enumerate() {
182                div { key: "{index}", class: "ui-descriptions__item",
183                    dt { class: "ui-descriptions__term", "{item.label}" }
184                    dd { class: "ui-descriptions__detail", "{item.value}" }
185                }
186            }
187            {props.children}
188        }
189    }
190}
191
192/// Props for [`DescriptionItem`].
193#[derive(Props, Clone, PartialEq)]
194pub struct DescriptionItemProps {
195    /// Extra classes appended after the Lumen classes.
196    #[props(into)]
197    pub class: Option<String>,
198    /// Any other `<div>` or global attribute.
199    #[props(extends = GlobalAttributes, extends = div)]
200    pub attributes: Vec<Attribute>,
201    /// A [`DescriptionTerm`] and its [`DescriptionDetail`]s.
202    pub children: Element,
203}
204
205/// One rich row of [`Descriptions`].
206#[component]
207pub fn DescriptionItem(props: DescriptionItemProps) -> Element {
208    let class = compose([
209        Some("ui-description-item ui-descriptions__item"),
210        props.class.as_deref(),
211    ]);
212
213    rsx! {
214        div { class, "data-slot": "description-item", ..props.attributes, {props.children} }
215    }
216}
217
218/// Props for [`DescriptionTerm`].
219#[derive(Props, Clone, PartialEq)]
220pub struct DescriptionTermProps {
221    /// Extra classes appended after the Lumen classes.
222    #[props(into)]
223    pub class: Option<String>,
224    /// Any other `<dt>` or global attribute.
225    #[props(extends = GlobalAttributes, extends = dt)]
226    pub attributes: Vec<Attribute>,
227    /// The term.
228    pub children: Element,
229}
230
231/// The term of a [`DescriptionItem`].
232#[component]
233pub fn DescriptionTerm(props: DescriptionTermProps) -> Element {
234    let class = compose([
235        Some("ui-description-term ui-descriptions__term"),
236        props.class.as_deref(),
237    ]);
238
239    rsx! {
240        dt { class, "data-slot": "description-term", ..props.attributes, {props.children} }
241    }
242}
243
244/// Props for [`DescriptionDetail`].
245#[derive(Props, Clone, PartialEq)]
246pub struct DescriptionDetailProps {
247    /// Extra classes appended after the Lumen classes.
248    #[props(into)]
249    pub class: Option<String>,
250    /// Any other `<dd>` or global attribute.
251    #[props(extends = GlobalAttributes, extends = dd)]
252    pub attributes: Vec<Attribute>,
253    /// The detail.
254    pub children: Element,
255}
256
257/// The detail of a [`DescriptionItem`].
258#[component]
259pub fn DescriptionDetail(props: DescriptionDetailProps) -> Element {
260    let class = compose([
261        Some("ui-description-detail ui-descriptions__detail"),
262        props.class.as_deref(),
263    ]);
264
265    rsx! {
266        dd { class, "data-slot": "description-detail", ..props.attributes, {props.children} }
267    }
268}