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
use derive_more::Display;
use yew::events::MouseEvent;
use yew::prelude::*;
use yewtil::NeqAssign;
use crate::{Alignment, Size};
#[derive(Clone, Debug, Properties, PartialEq)]
pub struct PaginationProps {
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub classes: Option<String>,
#[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,
}
pub struct Pagination {
props: PaginationProps,
}
impl Component for Pagination {
type Message = ();
type Properties = PaginationProps;
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
Self { props }
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props.neq_assign(props)
}
fn view(&self) -> Html {
let mut classes = Classes::from("pagination");
if let Some(extra) = &self.props.classes {
classes = classes.extend(extra);
}
if let Some(size) = &self.props.size {
classes.push(&size.to_string());
}
if let Some(alignment) = &self.props.alignment {
classes.push(&alignment.to_string());
}
if self.props.rounded {
classes.push("is-rounded");
}
html! {
<nav class=classes role="navigation" aria-label="pagination">
{self.props.previous.clone()}
{self.props.next.clone()}
<ul class="pagination-list">
{self.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_else(Callback::noop)]
pub onclick: Callback<MouseEvent>,
}
pub struct PaginationItem {
props: PaginationItemProps,
}
impl Component for PaginationItem {
type Message = ();
type Properties = PaginationItemProps;
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
Self { props }
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props.neq_assign(props)
}
fn view(&self) -> Html {
html! {
<a class=self.props.item_type.to_string() aria-label=self.props.label.clone() onclick=self.props.onclick.clone()>
{self.props.children.clone()}
</a>
}
}
}
#[derive(Clone, Debug, Display, PartialEq)]
#[display(fmt = "pagination-{}")]
pub enum PaginationItemType {
#[display(fmt = "link")]
Link,
#[display(fmt = "next")]
Next,
#[display(fmt = "previous")]
Previous,
}
pub struct PaginationEllipsis;
impl Component for PaginationEllipsis {
type Message = ();
type Properties = ();
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
Self
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {<span class="pagination-ellipsis">{"…"}</span>}
}
}
#[cfg(feature = "router")]
mod router {
use super::*;
use yew_router::components::RouterAnchor;
use yew_router::{RouterState, Switch};
#[derive(Clone, Properties, PartialEq)]
pub struct RouterProps<SW: Switch + Clone + PartialEq + 'static> {
pub route: SW,
#[prop_or_default]
pub children: Children,
pub item_type: PaginationItemType,
}
pub struct PaginationItemRouter<SW: Switch + Clone + PartialEq + 'static, STATE: RouterState = ()> {
props: RouterProps<SW>,
marker: std::marker::PhantomData<STATE>,
}
impl<SW: Switch + Clone + PartialEq + 'static, STATE: RouterState> Component for PaginationItemRouter<SW, STATE> {
type Message = ();
type Properties = RouterProps<SW>;
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
Self {
props,
marker: std::marker::PhantomData,
}
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props.neq_assign(props)
}
#[allow(deprecated)]
fn view(&self) -> Html {
html! {
<RouterAnchor<SW, STATE>
route=self.props.route.clone()
children=self.props.children.clone()
classes=self.props.item_type.to_string()
/>
}
}
}
}
#[cfg(feature = "router")]
pub use router::PaginationItemRouter;