yew_nav_link/components/page_link.rs
1// SPDX-FileCopyrightText: 2024-2026 RAprogramm <andrey.rozanov-vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4//! # `PageLink`
5//!
6//! Link or span element within a pagination page item.
7//! Renders an `<a>` when `href` is set, otherwise a `<span>`.
8//!
9//! # Example
10//!
11//! ```rust
12//! use yew::prelude::*;
13//! use yew_nav_link::components::{PageItem, PageLink};
14//!
15//! #[component]
16//! fn PaginationNav() -> Html {
17//! html! {
18//! <nav><ul class="pagination">
19//! <PageItem page={1} active=true>
20//! <PageLink href={Some("/page/1")}>{ "1" }</PageLink>
21//! </PageItem>
22//! <PageItem page={0} disabled=true>
23//! <PageLink href={None}>{ "..." }</PageLink>
24//! </PageItem>
25//! </ul></nav>
26//! }
27//! }
28//! ```
29//!
30//! # CSS Classes
31//!
32//! | Class | Condition |
33//! |-------|-----------|
34//! | `page-link` | Always applied |
35//!
36//! # Props
37//!
38//! | Prop | Type | Default | Description |
39//! |------|------|---------|-------------|
40//! | `href` | `Option<&'static str>` | `None` | Link URL |
41//! | `classes` | `Classes` | — | Additional CSS classes |
42//! | `children` | `Children` | — | Content |
43
44use yew::prelude::*;
45
46/// Properties for the [`PageLink`] component.
47///
48/// | Prop | Type | Default | Description |
49/// |------|------|---------|-------------|
50/// | `href` | `Option<&'static str>` | `None` | Link URL |
51/// | `classes` | `Classes` | — | Additional CSS classes |
52/// | `children` | `Children` | — | Content |
53#[derive(Properties, Clone, PartialEq, Debug)]
54pub struct PageLinkProps {
55 /// Additional CSS classes applied to the page link.
56 #[prop_or_default]
57 pub classes: Classes,
58
59 /// Optional URL. When set, renders an `<a>` element; otherwise a `<span>`.
60 #[prop_or_default]
61 pub href: Option<&'static str>,
62
63 /// Content rendered inside the page link.
64 #[prop_or_default]
65 pub children: Children
66}
67
68/// Link or span element within a pagination page item.
69///
70/// Renders an `<a>` when `href` is set, otherwise a `<span>`.
71///
72/// # CSS Classes
73///
74/// - `page-link` - Always applied
75#[function_component]
76pub fn PageLink(props: &PageLinkProps) -> Html {
77 let mut classes = props.classes.clone();
78 classes.push("page-link");
79
80 if let Some(href) = props.href {
81 html! {
82 <a {href} {classes}>
83 { for props.children.iter() }
84 </a>
85 }
86 } else {
87 html! {
88 <span {classes}>
89 { for props.children.iter() }
90 </span>
91 }
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn page_link_with_href() {
101 let props = PageLinkProps {
102 classes: Classes::default(),
103 href: Some("/page/2"),
104 children: Children::new(vec![])
105 };
106
107 assert_eq!(props.href, Some("/page/2"));
108 }
109
110 #[test]
111 fn page_link_without_href() {
112 let props = PageLinkProps {
113 classes: Classes::default(),
114 href: None,
115 children: Children::new(vec![])
116 };
117
118 assert!(props.href.is_none());
119 }
120}