lbc 0.1.19

A Leptos component library based on the Bulma CSS framework.
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
use crate::util::{Size, TestAttr};
use leptos::callback::Callback;
#[allow(unused_imports)]
use leptos::prelude::Effect;
use leptos::prelude::{
    AriaAttributes, Callable, Children, ClassAttribute, CustomAttribute, ElementChild, Get,
    GlobalAttributes, IntoView, OnAttribute, Signal, component, view,
};
use leptos::web_sys;

fn size_class(size: Size) -> &'static str {
    match size {
        Size::Small => "is-small",
        Size::Normal => "is-normal",
        Size::Medium => "is-medium",
        Size::Large => "is-large",
    }
}

/// A pagination item type mapped to Bulma CSS classes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PaginationItemType {
    /// A pagination link for a specific page number.
    Link,
    /// A pagination button for the next page.
    Next,
    /// A pagination button for the previous page.
    Previous,
}

impl PaginationItemType {
    fn bulma(self) -> &'static str {
        match self {
            PaginationItemType::Link => "pagination-link",
            PaginationItemType::Next => "pagination-next",
            PaginationItemType::Previous => "pagination-previous",
        }
    }
}

/// A responsive, usable, and flexible pagination component.
/// https://bulma.io/documentation/components/pagination/
#[component]
pub fn Pagination(
    /// Pagination list items to render inside <ul class="pagination-list">.
    children: Children,

    /// Extra classes for the root "pagination" container.
    #[prop(optional, into)]
    classes: Signal<String>,

    /// The size of this component.
    #[prop(optional)]
    size: Option<Size>,

    /// The alignment of this component (`is-centered`, `is-right`).
    ///
    /// Reuses Alignment enum from Tabs (Centered, Right).
    #[prop(optional)]
    alignment: Option<crate::components::tabs::Alignment>,

    /// Make the pagination elements rounded.
    #[prop(optional, into)]
    rounded: Signal<bool>,

    /// Label for the "previous" control.
    #[prop(into)]
    previous_label: Signal<String>,

    /// Label for the "next" control.
    #[prop(into)]
    next_label: Signal<String>,

    /// Click handler for the previous control.
    #[prop(optional)]
    on_previous: Option<Callback<()>>,

    /// Click handler for the next control.
    #[prop(optional)]
    on_next: Option<Callback<()>>,

    /// Optional test attribute for the root <nav>.
    ///
    /// When provided as a &str or String, this becomes `data-testid="value"`.
    /// You can also pass a full `TestAttr` to override the attribute key.
    #[prop(optional, into)]
    test_attr: Option<TestAttr>,
) -> impl IntoView {
    let class = {
        let classes = classes.clone();
        let rounded = rounded.clone();
        move || {
            let mut parts = vec!["pagination".to_string()];

            let extra = classes.get();
            if !extra.trim().is_empty() {
                parts.push(extra);
            }
            if let Some(sz) = size {
                parts.push(size_class(sz).to_string());
            }
            if let Some(align) = alignment {
                parts.push(match align {
                    crate::components::tabs::Alignment::Centered => "is-centered".to_string(),
                    crate::components::tabs::Alignment::Right => "is-right".to_string(),
                });
            }
            if rounded.get() {
                parts.push("is-rounded".to_string());
            }

            parts.join(" ")
        }
    };

    // Derive specific optional attributes that our macro can render.
    let (data_testid, data_cy) = match &test_attr {
        Some(ta) if ta.key == "data-testid" => (Some(ta.value.clone()), None),
        Some(ta) if ta.key == "data-cy" => (None, Some(ta.value.clone())),
        _ => (None, None),
    };

    let on_prev_click = move |ev: web_sys::MouseEvent| {
        ev.prevent_default();
        if let Some(cb) = on_previous.as_ref() {
            cb.run(());
        }
    };

    let on_next_click = move |ev: web_sys::MouseEvent| {
        ev.prevent_default();
        if let Some(cb) = on_next.as_ref() {
            cb.run(());
        }
    };

    view! {
        <nav
            class=move || class()
            role="navigation"
            aria-label="pagination"
            // Only support a known small set of custom attributes here.
            attr:data-testid=move || data_testid.clone()
            attr:data-cy=move || data_cy.clone()
        >
            <a
                class="pagination-previous"
                href="#"
                on:click=on_prev_click
            >
                {previous_label.get()}
            </a>
            <a
                class="pagination-next"
                href="#"
                on:click=on_next_click
            >
                {next_label.get()}
            </a>
            <ul class="pagination-list">
                {children()}
            </ul>
        </nav>
    }
}

/// A pagination element representing a link to a page number, the previous page or the next page.
/// https://bulma.io/documentation/components/pagination/
#[component]
pub fn PaginationItem(
    /// Inner content, usually a page number.
    children: Children,

    /// The pagination item type for this component.
    item_type: PaginationItemType,

    /// The aria-label to use for this element.
    #[prop(optional, into)]
    label: Signal<String>,

    /// Mark this item as the current page (adds "is-current").
    #[prop(optional, into)]
    current: Signal<bool>,

    /// Click handler for this item.
    #[prop(optional)]
    on_click: Option<Callback<()>>,

    /// Optional test attribute for the <a>.
    ///
    /// When provided as a &str or String, this becomes `data-testid="value"`.
    /// You can also pass a full `TestAttr` to override the attribute key.
    #[prop(optional, into)]
    test_attr: Option<TestAttr>,
) -> impl IntoView {
    let class = {
        let current = current.clone();
        move || {
            let mut parts = vec![item_type.bulma().to_string()];
            if current.get() {
                parts.push("is-current".to_string());
            }
            parts.join(" ")
        }
    };

    // Derive specific optional attributes that our macro can render.
    let (data_testid, data_cy) = match &test_attr {
        Some(ta) if ta.key == "data-testid" => (Some(ta.value.clone()), None),
        Some(ta) if ta.key == "data-cy" => (None, Some(ta.value.clone())),
        _ => (None, None),
    };

    let on_item_click = move |ev: web_sys::MouseEvent| {
        ev.prevent_default();
        if let Some(cb) = on_click.as_ref() {
            cb.run(());
        }
    };

    view! {
        <a
            class=move || class()
            aria-label=label.get()
            href="#"
            on:click=on_item_click
            attr:data-testid=move || data_testid.clone()
            attr:data-cy=move || data_cy.clone()
        >
            {children()}
        </a>
    }
}

/// A horizontal ellipsis for pagination range separators.
/// https://bulma.io/documentation/components/pagination/
#[component]
pub fn PaginationEllipsis(
    /// Character which will be used as ellipsis (default: "…")
    #[prop(into)]
    character: Signal<String>,
) -> impl IntoView {
    view! { <span class="pagination-ellipsis">{character.get()}</span> }
}

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

    #[test]
    fn pagination_renders_base_and_list() {
        let html = view! {
            <Pagination previous_label="Prev" next_label="Next">
                <li><a class="pagination-link">"1"</a></li>
                <li><a class="pagination-link">"2"</a></li>
            </Pagination>
        }
        .to_html();

        assert!(
            html.contains(r#"class="pagination""#),
            "expected base 'pagination' class; got: {}",
            html
        );
        assert!(
            html.contains(r#"class="pagination-list""#),
            "expected pagination-list; got: {}",
            html
        );
        assert!(
            html.contains("Prev") && html.contains("Next"),
            "expected prev/next labels; got: {}",
            html
        );
    }

    #[test]
    fn pagination_item_types_and_current() {
        let html = view! {
            <>
                <PaginationItem item_type=PaginationItemType::Next label="Next">
                    {"Next"}
                </PaginationItem>
                <PaginationItem item_type=PaginationItemType::Previous label="Prev">
                    {"Prev"}
                </PaginationItem>
                <PaginationItem item_type=PaginationItemType::Link label="1" current=true>
                    {"1"}
                </PaginationItem>
            </>
        }
        .to_html();

        assert!(
            html.contains("pagination-next"),
            "expected pagination-next class; got: {}",
            html
        );
        assert!(
            html.contains("pagination-previous"),
            "expected pagination-previous class; got: {}",
            html
        );
        assert!(
            html.contains(r#"class="pagination-link is-current""#)
                || html.contains("pagination-link is-current "),
            "expected current class on link; got: {}",
            html
        );
    }

    #[test]
    fn pagination_ellipsis_renders() {
        let html = view! { <PaginationEllipsis character="..." /> }.to_html();
        assert!(
            html.contains("pagination-ellipsis") && html.contains("..."),
            "expected ellipsis; got: {}",
            html
        );
    }
}

#[cfg(all(test, target_arch = "wasm32"))]
mod wasm_tests {
    use super::*;
    use crate::components::tabs::Alignment;
    use crate::util::{Size, TestAttr};
    use leptos::prelude::*;
    use wasm_bindgen_test::*;

    fn noop() -> Callback<()> {
        Callback::new(|_| {})
    }

    wasm_bindgen_test_configure!(run_in_browser);

    #[wasm_bindgen_test]
    fn pagination_renders_test_attr_as_data_testid() {
        let html = view! {
            <Pagination
                previous_label="Prev"
                next_label="Next"
                classes="is-centered"
                size=Size::Small
                alignment=Alignment::Centered
                rounded=true
                on_previous=noop()
                on_next=noop()
                test_attr="pagination-test"
            >
                <li>
                    <PaginationItem item_type=PaginationItemType::Link label="1" current=true>
                        {"1"}
                    </PaginationItem>
                </li>
            </Pagination>
        }
        .to_html();

        assert!(
            html.contains(r#"data-testid="pagination-test""#),
            "expected data-testid attribute on Pagination; got: {}",
            html
        );
    }

    #[wasm_bindgen_test]
    fn pagination_no_test_attr_when_not_provided() {
        let html = view! {
            <Pagination previous_label="Prev" next_label="Next">
                <li>
                    <PaginationItem item_type=PaginationItemType::Link label="1" current=true>
                        {"1"}
                    </PaginationItem>
                </li>
            </Pagination>
        }
        .to_html();

        assert!(
            !html.contains("data-testid"),
            "expected no data-testid attribute on Pagination when not provided; got: {}",
            html
        );
    }

    #[wasm_bindgen_test]
    fn pagination_item_renders_test_attr_as_data_testid() {
        let html = view! {
            <PaginationItem
                item_type=PaginationItemType::Link
                label="1"
                current=true
                on_click=noop()
                test_attr="pagination-item-test"
            >
                {"1"}
            </PaginationItem>
        }
        .to_html();

        assert!(
            html.contains(r#"data-testid="pagination-item-test""#),
            "expected data-testid attribute on PaginationItem; got: {}",
            html
        );
    }

    #[wasm_bindgen_test]
    fn pagination_item_no_test_attr_when_not_provided() {
        let html = view! {
            <PaginationItem
                item_type=PaginationItemType::Link
                label="1"
                current=true
                on_click=noop()
            >
                {"1"}
            </PaginationItem>
        }
        .to_html();

        assert!(
            !html.contains("data-testid"),
            "expected no data-testid attribute on PaginationItem when not provided; got: {}",
            html
        );
    }

    #[wasm_bindgen_test]
    fn pagination_accepts_custom_test_attr_key() {
        let custom = TestAttr::new("data-cy", "pagination-cy");
        let html = view! {
            <Pagination
                previous_label="Prev"
                next_label="Next"
                test_attr=custom
            >
                <li>
                    <PaginationItem
                        item_type=PaginationItemType::Link
                        label="1"
                        current=true
                        test_attr=TestAttr::new("data-cy", "pagination-item-cy")
                    >
                        {"1"}
                    </PaginationItem>
                </li>
            </Pagination>
        }
        .to_html();

        assert!(
            html.contains(r#"data-cy="pagination-cy""#),
            "expected custom data-cy attribute on Pagination; got: {}",
            html
        );
        assert!(
            html.contains(r#"data-cy="pagination-item-cy""#),
            "expected custom data-cy attribute on PaginationItem; got: {}",
            html
        );
    }
}