detaxine-ui 0.8.29

A UI Library for Leptos
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use crate::components::actions::button::BasicButton;
use crate::components::forms::reactive_form::ReactiveForm;
use crate::utils::forms::fire_bubbled_and_cancelable_event;
use icondata::Icon as IconId;
use leptos::html::Form;
use leptos::prelude::*;
use leptos::wasm_bindgen::JsCast;
use leptos_icons::Icon;
use web_sys::HtmlFormElement;
use web_sys::SubmitEvent;

#[derive(Clone, Debug, Default)]
pub struct StepInfo {
    pub label: String,
    pub icon: Option<IconId>,
}

impl StepInfo {
    pub fn new(label: &str, icon: Option<IconId>) -> Self {
        StepInfo {
            label: label.to_string(),
            icon,
        }
    }
}

/// A multi-step form wizard with a step indicator, per-step form validation, and linear/non-linear navigation.
///
/// Each direct child should be a `<Step>` component wrapping form fields. Every step is
/// wrapped in a `ReactiveForm` automatically. When `is_linear=true`, the Next button is
/// disabled until the current step's form passes `checkValidity()`.
///
/// Form refs for all steps are sent to the parent via `send_all_form_refs` whenever the
/// user navigates to the final step or clicks a step indicator directly.
///
/// # Props
///
/// - `step_labels` – `RwSignal<Vec<StepInfo>>` holding the label and optional icon for each step indicator.
/// - `final_button_text` – Label on the submit button shown at the last step.
/// - `on_click_final_button` – Callback fired when the final button is clicked.
/// - `send_all_form_refs` – Callback receiving `Vec<NodeRef<Form>>` for all steps.
/// - `is_linear` – When `true`, the Next button is disabled until the current step is valid. Defaults to `false`.
/// - `final_button_is_disabled` – `Signal<bool>` that disables the final button. Defaults to `false`.
/// - `ext_wrapper_styles` – `Signal<String>` of additional Tailwind classes on the step content wrapper.
/// - `handle_on_cleanup` – Callback fired when the component is cleaned up.
/// - `children` – One or more `<Step>` components.
///
/// # Example
///
/// ```
/// use leptos::prelude::*;
/// use leptos::html::Form;
/// use detaxine_ui::components::navigation::stepper::{Stepper, Step, StepInfo};
///
/// #[component]
/// fn Example() -> impl IntoView {
///     let form_refs = RwSignal::new(Vec::<NodeRef<Form>>::new());
///     view! {
///         <Stepper
///             step_labels=RwSignal::new(vec![
///                 StepInfo::new("Details", None),
///                 StepInfo::new("Confirm", None),
///             ])
///             final_button_text="Submit"
///             send_all_form_refs=Callback::new(move |refs| form_refs.set(refs))
///             is_linear=true
///         >
///             <Step><p>"Step 1 content"</p></Step>
///             <Step><p>"Step 2 content"</p></Step>
///         </Stepper>
///     }
/// }
/// ```
#[component]
pub fn Stepper(
    mut children: ChildrenFragmentMut, // Children passed as a function
    #[prop(into, optional)] final_button_text: String,
    #[prop(optional, default = Callback::new(|_| {}))] on_click_final_button: Callback<()>,
    #[prop(into)] step_labels: RwSignal<Vec<StepInfo>>,
    #[prop(optional, default = false)] is_linear: bool,
    #[prop(optional, default = Callback::new(|_| {}))] send_all_form_refs: Callback<
        Vec<NodeRef<Form>>,
    >,
    #[prop(into, optional)] ext_wrapper_styles: Signal<String>,
    #[prop(into, optional, default = Signal::derive(|| false))] final_button_is_disabled: Signal<
        bool,
    >,
    #[prop(optional, default = Callback::new(|_| {}))] handle_on_cleanup: Callback<()>,
) -> impl IntoView {
    let (current_step, set_current_step) = signal(0); // Leptos signal for state
    let (step_form_is_valid, set_step_form_is_valid) = signal(false); // Leptos signal for state
    let child_nodes: Vec<AnyView> = children().nodes.into_iter().map(|n| n.into_any()).collect();
    let step_count = child_nodes.len();
    let form_refs = RwSignal::new(
        (0..step_count)
            .map(|_| NodeRef::<Form>::new())
            .collect::<Vec<_>>(),
    );

    let onclick_next = Callback::new(move |_| {
        if current_step.get() < step_count - 1 {
            set_current_step.update(|step| *step += 1);
        }

        // if second last, send all form_refs to parent in a callback
        if current_step.get() == step_count - 1 {
            let form_refs = form_refs.get();
            send_all_form_refs.run(form_refs);
        }
    });

    let onclick_prev = Callback::new(move |_| {
        if current_step.get() > 0 {
            set_current_step.update(|step| *step -= 1);
        }
    });

    let handle_final_button_click = Callback::new(move |_| {
        on_click_final_button.run(());
    });

    let handle_step_form_submit = move |ev: SubmitEvent| {
        ev.prevent_default();
        ev.stop_propagation();
        // Implement logic to show form validity
        let target = ev
            .target()
            .and_then(|t| t.dyn_into::<HtmlFormElement>().ok());

        if let Some(form) = target {
            // let form_data = FormData::new_with_form(&form).unwrap_or_default();
            let is_valid = form.check_validity();
            set_step_form_is_valid.set(is_valid);
        }
    };

    let next_is_disabled = Memo::new(move |_| !step_form_is_valid.get() && is_linear);

    // A workaround for updating the next step's form's validity state when navigating to the next step or previous step
    Effect::new(move || {
        if let Some(form_ref) = form_refs.get().get(current_step.get()) {
            if let Some(form) = form_ref.get() as Option<HtmlFormElement> {
                fire_bubbled_and_cancelable_event("submit", true, true, &form);
            }
        }
    });

    // Cleanup
    on_cleanup(move || {
        handle_on_cleanup.run(());
    });

    view! {
        <div class="flex flex-col items-center gap-[40px] w-full h-full p-4">
            <div class="relative flex items-center w-full overflow-x-auto">
                <div class="relative flex items-center justify-between w-full">
                    <For
                        each=move || step_labels.get().into_iter().enumerate()
                        key=|(index, _)| *index
                        let:((index, step_label))
                    >
                        {
                            let is_current = move || index == current_step.get();
                            let step_count_inner = step_count;
                            view! {
                                // Step circle + label
                                <div
                                    on:click=move |_| {
                                        if next_is_disabled.get() {
                                            return;
                                        }
                                        set_current_step.update(|step| *step = index);
                                        let form_refs = form_refs.get();
                                        send_all_form_refs.run(form_refs);
                                    }
                                    class="flex items-center gap-[10px] cursor-pointer shrink-0"
                                >
                                    <div class=move || format!(
                                        "w-8 h-8 flex items-center justify-center rounded-full text-sm {}",
                                        if is_current() {
                                            "bg-primary text-contrast-white"
                                        } else {
                                            "bg-light-gray"
                                        }
                                    )>
                                        {if step_label.icon.is_none() {
                                            Some(index + 1)
                                        } else {
                                            None
                                        }}
                                        {if let Some(icon) = step_label.icon {
                                            Some(view! { <Icon icon=icon /> })
                                        } else {
                                            None
                                        }}
                                    </div>
                                    <div class=move || format!(
                                        "text-sm {}",
                                        if is_current() {
                                            "font-bold text-primary"
                                        } else {
                                            "hidden md:flex"
                                        }
                                    )>
                                        {step_label.label.clone()}
                                    </div>
                                </div>

                                // Connector — only between steps, never after the last
                                {if index < step_count_inner - 1 {
                                    Some(view! {
                                        <div class="flex-1 h-px bg-mid-gray mx-2" />
                                    })
                                } else {
                                    None
                                }}
                            }
                        }
                    </For>
                </div>
            </div>
            <div on:submit=handle_step_form_submit class=move || format!("flex-1 w-full {}", ext_wrapper_styles.get())>
            {
                    child_nodes.into_iter().enumerate().map(|(i, child)| {
                        let form_ref = form_refs.get_untracked()[i].clone();
                        view! {
                            <ReactiveForm
                                form_ref=form_ref
                                ext_styles=Signal::derive(move || {
                                    if current_step.get() == i { "block".to_string() } else { "hidden".to_string() }
                                })
                            >
                                { child }
                            </ReactiveForm>
                        }
                    }).collect_view()
                }
            </div>
            <div class="mt-auto flex w-full justify-start gap-4">
                {
                    move || if current_step.get() == 0 {
                        None
                    } else {
                        Some(view! {
                            <BasicButton
                                onclick=onclick_prev
                                button_text="Previous"
                                style_ext="bg-white"
                            />
                        })
                    }
                }
                {
                    move || if current_step.get() == step_count - 1 {
                        view! {
                            <BasicButton
                                onclick=handle_final_button_click
                                button_text=final_button_text.clone()
                                style_ext="bg-primary text-contrast-white"
                                disabled=final_button_is_disabled
                            />
                        }
                    } else {
                        view! {
                            <BasicButton
                                disabled=next_is_disabled
                                onclick=onclick_next
                                button_text="Next"
                                style_ext="bg-primary text-contrast-white"
                            />
                        }
                    }
                }
            </div>
        </div>
    }
}

/// A wrapper component representing a single step inside a `Stepper`.
///
/// Renders its children directly; layout and visibility are controlled by the parent `Stepper`.
///
/// # Example
///
/// ```
/// use leptos::prelude::*;
/// use detaxine_ui::components::navigation::stepper::Step;
///
/// #[component]
/// fn Example() -> impl IntoView {
///     view! {
///         <Step>
///             <p>"Step content"</p>
///         </Step>
///     }
/// }
/// ```
#[component]
pub fn Step(children: Children) -> impl IntoView {
    view! {
        { children() }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // StepInfo::new

    #[test]
    fn step_info_new_sets_label() {
        let info = StepInfo::new("Details", None);
        assert_eq!(info.label, "Details");
        assert!(info.icon.is_none());
    }

    #[test]
    fn step_info_clone() {
        let info = StepInfo::new("Confirm", None);
        let cloned = info.clone();
        assert_eq!(cloned.label, info.label);
    }

    #[test]
    fn step_info_default_has_empty_label() {
        let info = StepInfo::default();
        assert_eq!(info.label, "");
        assert!(info.icon.is_none());
    }

    // next/prev navigation logic

    fn can_go_next(current: usize, total: usize) -> bool {
        current < total - 1
    }

    fn can_go_prev(current: usize) -> bool {
        current > 0
    }

    #[test]
    fn can_advance_before_last_step() {
        assert!(can_go_next(0, 3));
        assert!(can_go_next(1, 3));
    }

    #[test]
    fn cannot_advance_past_last_step() {
        assert!(!can_go_next(2, 3));
    }

    #[test]
    fn can_go_back_after_first_step() {
        assert!(can_go_prev(1));
        assert!(can_go_prev(2));
    }

    #[test]
    fn cannot_go_back_from_first_step() {
        assert!(!can_go_prev(0));
    }

    // next_is_disabled (linear mode)

    fn next_is_disabled(step_form_is_valid: bool, is_linear: bool) -> bool {
        !step_form_is_valid && is_linear
    }

    #[test]
    fn next_disabled_when_linear_and_invalid() {
        assert!(next_is_disabled(false, true));
    }

    #[test]
    fn next_enabled_when_linear_and_valid() {
        assert!(!next_is_disabled(true, true));
    }

    #[test]
    fn next_enabled_when_not_linear_regardless_of_validity() {
        assert!(!next_is_disabled(false, false));
        assert!(!next_is_disabled(true, false));
    }

    // is_last_step

    fn is_last_step(current: usize, total: usize) -> bool {
        current == total - 1
    }

    #[test]
    fn final_button_shown_on_last_step() {
        assert!(is_last_step(2, 3));
    }

    #[test]
    fn next_button_shown_before_last_step() {
        assert!(!is_last_step(0, 3));
        assert!(!is_last_step(1, 3));
    }

    // form_refs initialised per step

    #[test]
    fn form_refs_count_matches_step_count() {
        let owner = Owner::new();
        owner.with(|| {
            let step_count = 4;
            let form_refs = RwSignal::new(
                (0..step_count)
                    .map(|_| NodeRef::<leptos::html::Form>::new())
                    .collect::<Vec<_>>(),
            );
            assert_eq!(form_refs.get().len(), step_count);
        });
    }

    // send_all_form_refs fires on last step

    #[test]
    fn send_all_form_refs_fires_when_reaching_last_step() {
        let owner = Owner::new();
        owner.with(|| {
            let fired = RwSignal::new(false);
            let send_all_form_refs: Callback<Vec<NodeRef<leptos::html::Form>>> =
                Callback::new(move |_| fired.set(true));

            let step_count = 3;
            let (current_step, set_current_step) = signal(step_count - 2); // second-to-last

            // simulate clicking next from second-to-last step
            set_current_step.update(|s| *s += 1);

            if current_step.get() == step_count - 1 {
                send_all_form_refs.run(vec![]);
            }

            assert!(fired.get());
        });
    }

    // step indicator click blocked when disabled

    #[test]
    fn step_click_blocked_when_next_disabled() {
        let owner = Owner::new();
        owner.with(|| {
            let (current_step, set_current_step) = signal(0usize);
            let disabled = true;

            // simulate step click with guard
            if !disabled {
                set_current_step.set(2);
            }

            assert_eq!(current_step.get(), 0);
        });
    }

    #[test]
    fn step_click_allowed_when_not_disabled() {
        let owner = Owner::new();
        owner.with(|| {
            let (current_step, set_current_step) = signal(0usize);
            let disabled = false;

            if !disabled {
                set_current_step.set(2);
            }

            assert_eq!(current_step.get(), 2);
        });
    }

    // reactive step validity

    #[test]
    fn step_form_validity_updates_reactively() {
        let owner = Owner::new();
        owner.with(|| {
            let (step_form_is_valid, set_step_form_is_valid) = signal(false);
            assert!(!step_form_is_valid.get());
            set_step_form_is_valid.set(true);
            assert!(step_form_is_valid.get());
        });
    }
}