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
use derive_more::Display;
use yew::events::MouseEvent;
use yew::prelude::*;
use crate::{Alignment, Size};
#[derive(Clone, Debug, Properties, PartialEq)]
pub struct PaginationProps {
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub classes: Classes,
#[prop_or_default]
pub size: Option<Size>,
#[prop_or_default]
pub alignment: Option<Alignment>,
#[prop_or_default]
pub rounded: bool,
pub previous: Html,
pub next: Html,
}
#[function_component(Pagination)]
pub fn pagination(props: &PaginationProps) -> Html {
let class = classes!(
"pagination",
props.classes.clone(),
props.size.as_ref().map(|size| size.to_string()),
props.alignment.as_ref().map(|alignment| alignment.to_string()),
props.rounded.then_some("is-rounded"),
);
html! {
<nav {class} role="navigation" aria-label="pagination">
{props.previous.clone()}
{props.next.clone()}
<ul class="pagination-list">
{props.children.clone()}
</ul>
</nav>
}
}
#[derive(Clone, Debug, Properties, PartialEq)]
pub struct PaginationItemProps {
pub children: Children,
pub item_type: PaginationItemType,
#[prop_or_default]
pub label: String,
#[prop_or_default]
pub onclick: Callback<MouseEvent>,
}
#[function_component(PaginationItem)]
pub fn pagination_item(props: &PaginationItemProps) -> Html {
html! {
<a class={props.item_type.to_string()} aria-label={props.label.clone()} onclick={props.onclick.clone()}>
{props.children.clone()}
</a>
}
}
#[derive(Clone, Debug, Display, PartialEq, Eq)]
#[display(fmt = "pagination-{}")]
pub enum PaginationItemType {
#[display(fmt = "link")]
Link,
#[display(fmt = "next")]
Next,
#[display(fmt = "previous")]
Previous,
}
#[function_component(PaginationEllipsis)]
pub fn pagination_ellipsis() -> Html {
html! {<span class="pagination-ellipsis">{"…"}</span>}
}
#[cfg(feature = "router")]
mod router {
use super::*;
use serde::Serialize;
use yew_router::components::Link;
use yew_router::Routable;
#[derive(Clone, Properties, PartialEq)]
pub struct RouterProps<R: Routable + Clone + PartialEq + 'static> {
pub route: R,
#[prop_or_default]
pub children: Children,
pub item_type: PaginationItemType,
}
pub struct PaginationItemRouter<R: Routable + Clone + PartialEq + 'static, Q: Clone + PartialEq + Serialize + 'static = ()> {
_route: std::marker::PhantomData<R>,
_query: std::marker::PhantomData<Q>,
}
impl<R: Routable + Clone + PartialEq + 'static, Q: Clone + PartialEq + Serialize + 'static> Component for PaginationItemRouter<R, Q> {
type Message = ();
type Properties = RouterProps<R>;
fn create(_ctx: &Context<Self>) -> Self {
Self {
_route: std::marker::PhantomData,
_query: std::marker::PhantomData,
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<Link<R, Q>
to={ctx.props().route.clone()}
children={ctx.props().children.clone()}
classes={classes!(ctx.props().item_type.to_string())}
/>
}
}
}
}
#[cfg(feature = "router")]
pub use router::PaginationItemRouter;