Skip to main content

lumen_dioxus/navigation/
stepper.rs

1//! `Stepper`.
2
3use dioxus::prelude::*;
4
5use crate::class::{Orientation, compose};
6
7/// One step of a [`Stepper`].
8#[derive(Clone, Debug, Default, PartialEq, Eq)]
9pub struct StepperStep {
10    /// Step name.
11    pub title: String,
12    /// Optional supporting line.
13    pub description: Option<String>,
14}
15
16impl StepperStep {
17    /// A step with a title and a description.
18    pub fn new(title: impl Into<String>, description: impl Into<String>) -> Self {
19        Self {
20            title: title.into(),
21            description: Some(description.into()),
22        }
23    }
24}
25
26impl From<&str> for StepperStep {
27    fn from(title: &str) -> Self {
28        Self {
29            title: title.to_string(),
30            description: None,
31        }
32    }
33}
34
35impl From<String> for StepperStep {
36    fn from(title: String) -> Self {
37        Self {
38            title,
39            description: None,
40        }
41    }
42}
43
44/// Progress of one step relative to the current step.
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
46#[non_exhaustive]
47pub enum StepperState {
48    /// Before the current step.
49    Complete,
50    /// The current step.
51    Current,
52    /// After the current step.
53    Upcoming,
54}
55
56impl StepperState {
57    /// The `data-state` value.
58    pub fn as_str(self) -> &'static str {
59        match self {
60            Self::Complete => "complete",
61            Self::Current => "current",
62            Self::Upcoming => "upcoming",
63        }
64    }
65
66    /// The state of the step at `index` when `current` is the zero-based current step. A
67    /// negative `current` leaves every step upcoming.
68    pub fn of(index: usize, current: i64) -> Self {
69        let index = i64::try_from(index).unwrap_or(i64::MAX);
70
71        match index.cmp(&current) {
72            std::cmp::Ordering::Less => Self::Complete,
73            std::cmp::Ordering::Equal => Self::Current,
74            std::cmp::Ordering::Greater => Self::Upcoming,
75        }
76    }
77}
78
79/// Props for [`Stepper`].
80#[derive(Props, Clone, PartialEq)]
81pub struct StepperProps {
82    /// The steps, in order.
83    #[props(default)]
84    pub steps: Vec<StepperStep>,
85    /// Zero-based index of the current step. Earlier steps are complete.
86    #[props(default)]
87    pub current_step: i64,
88    /// Layout direction.
89    #[props(default)]
90    pub orientation: Orientation,
91    /// Extra classes appended after the Lumen classes.
92    #[props(into)]
93    pub class: Option<String>,
94    /// Any other `<ol>` or global attribute. Name the process with `aria_label`.
95    #[props(extends = GlobalAttributes, extends = ol)]
96    pub attributes: Vec<Attribute>,
97    /// Extra `li` steps appended after `steps`.
98    pub children: Element,
99}
100
101/// Progress through an ordered, multi-step process.
102#[component]
103pub fn Stepper(props: StepperProps) -> Element {
104    let class = compose([
105        Some("ui-stepper"),
106        (props.orientation == Orientation::Vertical).then_some("ui-stepper--vertical"),
107        props.class.as_deref(),
108    ]);
109
110    rsx! {
111        ol { class, "data-orientation": props.orientation.as_str(), ..props.attributes,
112            for (index, step) in props.steps.iter().enumerate() {
113                {
114                    let state = StepperState::of(index, props.current_step);
115
116                    rsx! {
117                        li {
118                            key: "{step.title}",
119                            aria_current: (state == StepperState::Current).then_some("step"),
120                            class: "ui-stepper__step",
121                            "data-state": state.as_str(),
122                            span { class: "ui-stepper__marker", "{index + 1}" }
123                            span { class: "ui-stepper__content",
124                                span { class: "ui-stepper__title", "{step.title}" }
125                                if let Some(description) = step.description.as_deref().filter(|text| !text.is_empty()) {
126                                    span { class: "ui-stepper__description", "{description}" }
127                                }
128                            }
129                        }
130                    }
131                }
132            }
133            {props.children}
134        }
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::StepperState;
141
142    #[test]
143    fn states_follow_the_current_step() {
144        assert_eq!(StepperState::of(0, 1), StepperState::Complete);
145        assert_eq!(StepperState::of(1, 1), StepperState::Current);
146        assert_eq!(StepperState::of(2, 1), StepperState::Upcoming);
147        assert_eq!(StepperState::of(0, -1), StepperState::Upcoming);
148        assert_eq!(
149            StepperState::of(usize::MAX, i64::MAX),
150            StepperState::Current
151        );
152    }
153}