leptos_bootstrap/v5/
pagination.rs

1use leptos::prelude::*;
2use std::fmt;
3
4pub enum PaginationSize {
5    Small,
6    Normal,
7    Large,
8}
9
10impl fmt::Display for PaginationSize {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        let s = match self {
13            Self::Small => "pagination-sm",
14            Self::Normal => "",
15            Self::Large => "pagination-lg",
16        };
17        write!(f, "{}", s)
18    }
19}
20
21pub enum PaginationAlignment {
22    Start,
23    Center,
24    End,
25}
26
27impl fmt::Display for PaginationAlignment {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        let s = match self {
30            Self::Start => "",
31            Self::Center => "justify-content-center",
32            Self::End => "justify-content-end",
33        };
34        write!(f, "{}", s)
35    }
36}
37
38#[component]
39pub fn Pagination<'a>(
40    #[prop(default = PaginationSize::Normal)] size: PaginationSize,
41    #[prop(default = PaginationAlignment::Start)] align: PaginationAlignment,
42    #[prop(optional, into)] class: &'a str,
43    children: Children,
44) -> impl IntoView {
45    let class = format!("pagination {} {} {}", align, size, class);
46    view! {
47        <nav>
48            <ul class=class>
49                {children()}
50            </ul>
51        </nav>
52    }
53}
54
55#[component]
56pub fn PageItem<'a>(
57    #[prop(default = false)] active: bool,
58    #[prop(default = false)] disabled: bool,
59    #[prop(optional, into)] href: &'a str,
60    children: Children,
61) -> impl IntoView {
62    let mut class = "page-item".to_string();
63    if active {
64        class.push_str(" active");
65    }
66    if disabled {
67        class.push_str(" disabled");
68    }
69    view! {
70        <li class=class>
71            <a href=href class="page-link">{children()}</a>
72        </li>
73    }
74}