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>{children()}</ul>
49        </nav>
50    }
51}
52
53#[component]
54pub fn PageItem<'a>(
55    #[prop(default = false)] active: bool,
56    #[prop(default = false)] disabled: bool,
57    #[prop(optional, into)] href: &'a str,
58    children: Children,
59) -> impl IntoView {
60    let mut class = "page-item".to_string();
61    if active {
62        class.push_str(" active");
63    }
64    if disabled {
65        class.push_str(" disabled");
66    }
67    view! {
68        <li class=class>
69            <a href=href class="page-link">
70                {children()}
71            </a>
72        </li>
73    }
74}