♻️ (pagetop): Deshabilita menús con with_disabled()

Cambia `link_disabled()`, `link_blank_disabled()` y `button_disabled()`
de `nav::Item` y `dropdown::Item` por `with_disabled(bool)`, como en
Button.
This commit is contained in:
Manuel Cillero 2026-11-23 02:01:06 +01:00
parent 5d6852382a
commit f1fd34f974
12 changed files with 123 additions and 93 deletions

View file

@ -24,7 +24,7 @@ impl Extension for NavDemo {
.with_item(dropdown::Item::header(Lc::n("Danger zone"))) .with_item(dropdown::Item::header(Lc::n("Danger zone")))
.with_item(dropdown::Item::button(Lc::n("Reset settings"))), .with_item(dropdown::Item::button(Lc::n("Reset settings"))),
)) ))
.with_item(nav::Item::link_disabled(Lc::n("Disabled"), "#")); .with_item(nav::Item::link(Lc::n("Disabled"), "#").with_disabled(true));
let user_menu = Dropdown::new() let user_menu = Dropdown::new()
.with_title(Lc::n("Account")) .with_title(Lc::n("Account"))

View file

@ -38,10 +38,7 @@ impl Extension for NavbarDemo {
)), )),
)) ))
.with_item(nav::Item::link(Lc::t("navbar_active", &LOC), "/active").with_active(true)) .with_item(nav::Item::link(Lc::t("navbar_active", &LOC), "/active").with_active(true))
.with_item(nav::Item::link_disabled( .with_item(nav::Item::link(Lc::t("navbar_disabled", &LOC), "#").with_disabled(true));
Lc::t("navbar_disabled", &LOC),
"#",
));
let navbar = Navbar::brand_left(brand).with_item(navbar::Item::nav(main_nav)); let navbar = Navbar::brand_left(brand).with_item(navbar::Item::nav(main_nav));

View file

@ -106,15 +106,14 @@ fn menus_block() -> Block {
)) ))
.with_item(dropdown::Item::divider()) .with_item(dropdown::Item::divider())
.with_item(dropdown::Item::label(Lc::t("menus_item_label", &LOC))) .with_item(dropdown::Item::label(Lc::t("menus_item_label", &LOC)))
.with_item(dropdown::Item::link_disabled( .with_item(
Lc::t("menus_item_disabled", &LOC), dropdown::Item::link(Lc::t("menus_item_disabled", &LOC), "#")
"#", .with_disabled(true),
)), ),
)) ))
.with_item(nav::Item::link_disabled( .with_item(
Lc::t("menus_item_disabled", &LOC), nav::Item::link(Lc::t("menus_item_disabled", &LOC), "#").with_disabled(true),
"#", ),
)),
)) ))
.with_item(navbar::Item::nav( .with_item(navbar::Item::nav(
Nav::new() Nav::new()
@ -686,10 +685,7 @@ fn demo_nav() -> Nav {
"/something", "/something",
)), )),
)) ))
.with_item(nav::Item::link_disabled( .with_item(nav::Item::link(Lc::t("menus_item_disabled", &LOC), "#").with_disabled(true))
Lc::t("menus_item_disabled", &LOC),
"#",
))
} }
fn is_bootsier() -> bool { fn is_bootsier() -> bool {

View file

@ -46,19 +46,18 @@ impl Extension for ThemeShowcase {
Lc::t("showcase_menu_messages", &LOC), Lc::t("showcase_menu_messages", &LOC),
"#", "#",
)) ))
.with_item(dropdown::Item::link_disabled( .with_item(
Lc::t("showcase_menu_disabled", &LOC), dropdown::Item::link(Lc::t("showcase_menu_disabled", &LOC), "#")
"#", .with_disabled(true),
)), ),
)) ))
.with_item(nav::Item::link_blank( .with_item(nav::Item::link_blank(
Lc::t("showcase_menu_docs", &LOC), Lc::t("showcase_menu_docs", &LOC),
"https://docs.rs/pagetop", "https://docs.rs/pagetop",
)) ))
.with_item(nav::Item::link_disabled( .with_item(
Lc::t("showcase_menu_disabled", &LOC), nav::Item::link(Lc::t("showcase_menu_disabled", &LOC), "#").with_disabled(true),
"#", );
));
// Menú de la cuenta al final de la barra y, con Bootsier, el selector de modo de color // Menú de la cuenta al final de la barra y, con Bootsier, el selector de modo de color
// (claro, oscuro o automático). // (claro, oscuro o automático).

View file

@ -42,7 +42,7 @@ pub(crate) const EXTRA_IN_NAVBAR: &str = "bootsier.nav.in_navbar";
/// bs::dropdown::Item::link(Lc::n("Another"), "/another"), /// bs::dropdown::Item::link(Lc::n("Another"), "/another"),
/// ])), /// ])),
/// )) /// ))
/// .with_item(bs::nav::Item::link_disabled(Lc::n("Disabled"), "#")); /// .with_item(bs::nav::Item::link(Lc::n("Disabled"), "#").with_disabled(true));
/// ``` /// ```
#[builder_impl] #[builder_impl]
pub trait NavBsExt { pub trait NavBsExt {

View file

@ -74,7 +74,7 @@ const EXTRA_LAYOUT: &str = "bootsier.navbar.layout";
/// Lc::n("Reports"), "/tools/reports") /// Lc::n("Reports"), "/tools/reports")
/// ) /// )
/// )) /// ))
/// .with_item(bs::nav::Item::link_disabled(Lc::n("Disabled"), "#")) /// .with_item(bs::nav::Item::link(Lc::n("Disabled"), "#").with_disabled(true))
/// )); /// ));
/// ``` /// ```
/// ///

View file

@ -198,19 +198,6 @@ impl Item {
} }
} }
/// Crea un enlace deshabilitado que no permite la interacción.
pub fn link_disabled(label: Lc, route: impl Into<Route>) -> Self {
Self {
item_kind: ItemKind::Link {
label,
route: route.into(),
blank: false,
disabled: true,
},
..Default::default()
}
}
/// Crea un enlace que se abre en una nueva ventana o pestaña. /// Crea un enlace que se abre en una nueva ventana o pestaña.
pub fn link_blank(label: Lc, route: impl Into<Route>) -> Self { pub fn link_blank(label: Lc, route: impl Into<Route>) -> Self {
Self { Self {
@ -224,19 +211,6 @@ impl Item {
} }
} }
/// Crea un enlace inicialmente deshabilitado que se abriría en una nueva ventana.
pub fn link_blank_disabled(label: Lc, route: impl Into<Route>) -> Self {
Self {
item_kind: ItemKind::Link {
label,
route: route.into(),
blank: true,
disabled: true,
},
..Default::default()
}
}
/// Crea un botón de acción local, sin navegación asociada. /// Crea un botón de acción local, sin navegación asociada.
pub fn button(label: Lc) -> Self { pub fn button(label: Lc) -> Self {
Self { Self {
@ -248,17 +222,6 @@ impl Item {
} }
} }
/// Crea un botón deshabilitado.
pub fn button_disabled(label: Lc) -> Self {
Self {
item_kind: ItemKind::Button {
label,
disabled: true,
},
..Default::default()
}
}
/// Crea un encabezado para un grupo de elementos dentro del menú. /// Crea un encabezado para un grupo de elementos dentro del menú.
pub fn header(label: Lc) -> Self { pub fn header(label: Lc) -> Self {
Self { Self {
@ -289,6 +252,17 @@ impl Item {
self self
} }
/// Establece si el elemento está deshabilitado: se muestra atenuado y no permite la
/// interacción. Sólo se aplica a los enlaces y los botones; en los demás tipos no tiene efecto.
pub fn with_disabled(mut self, disabled: bool) -> Self {
if let ItemKind::Link { disabled: d, .. } | ItemKind::Button { disabled: d, .. } =
&mut self.item_kind
{
*d = disabled;
}
self
}
/// Asigna o quita el icono al principio del elemento, antes de la etiqueta. Si se pasa `None`, /// Asigna o quita el icono al principio del elemento, antes de la etiqueta. Si se pasa `None`,
/// no se mostrará. /// no se mostrará.
/// ///

View file

@ -25,7 +25,7 @@ use crate::prelude::*;
/// .with_item(dropdown::Item::link(Lc::n("Action"), "/action")) /// .with_item(dropdown::Item::link(Lc::n("Action"), "/action"))
/// .with_item(dropdown::Item::link(Lc::n("Another"), "/another")), /// .with_item(dropdown::Item::link(Lc::n("Another"), "/another")),
/// )) /// ))
/// .with_item(nav::Item::link_disabled(Lc::n("Disabled"), "#")); /// .with_item(nav::Item::link(Lc::n("Disabled"), "#").with_disabled(true));
/// ``` /// ```
#[derive(AutoDefault, Clone, Debug, Getters)] #[derive(AutoDefault, Clone, Debug, Getters)]
pub struct Nav { pub struct Nav {
@ -237,7 +237,7 @@ impl Nav {
/// nav.with_item(nav::Item::link("Inicio", "/")); /// nav.with_item(nav::Item::link("Inicio", "/"));
/// nav.with_item(TypedOp::AddMany(vec![ /// nav.with_item(TypedOp::AddMany(vec![
/// nav::Item::link(...), /// nav::Item::link(...),
/// nav::Item::link_disabled(...), /// nav::Item::link(...).with_disabled(true),
/// ])); /// ]));
/// ``` /// ```
pub fn with_item(mut self, op: impl Into<TypedOp<nav::Item>>) -> Self { pub fn with_item(mut self, op: impl Into<TypedOp<nav::Item>>) -> Self {

View file

@ -222,19 +222,6 @@ impl Item {
} }
} }
/// Crea un enlace deshabilitado que no permite la interacción.
pub fn link_disabled(label: Lc, route: impl Into<Route>) -> Self {
Self {
item_kind: ItemKind::Link {
label,
route: route.into(),
blank: false,
disabled: true,
},
..Default::default()
}
}
/// Crea un enlace que se abre en una nueva ventana o pestaña. /// Crea un enlace que se abre en una nueva ventana o pestaña.
pub fn link_blank(label: Lc, route: impl Into<Route>) -> Self { pub fn link_blank(label: Lc, route: impl Into<Route>) -> Self {
Self { Self {
@ -248,19 +235,6 @@ impl Item {
} }
} }
/// Crea un enlace inicialmente deshabilitado que se abriría en una nueva ventana.
pub fn link_blank_disabled(label: Lc, route: impl Into<Route>) -> Self {
Self {
item_kind: ItemKind::Link {
label,
route: route.into(),
blank: true,
disabled: true,
},
..Default::default()
}
}
/// Crea un elemento con contenido HTML arbitrario. /// Crea un elemento con contenido HTML arbitrario.
/// ///
/// El contenido se renderiza tal cual lo devuelve el componente [`Html`], dentro de un `<li>` /// El contenido se renderiza tal cual lo devuelve el componente [`Html`], dentro de un `<li>`
@ -299,6 +273,15 @@ impl Item {
self self
} }
/// Establece si el elemento está deshabilitado: se muestra atenuado y no permite la
/// interacción. Sólo se aplica a los enlaces; en los demás tipos no tiene efecto.
pub fn with_disabled(mut self, disabled: bool) -> Self {
if let ItemKind::Link { disabled: d, .. } = &mut self.item_kind {
*d = disabled;
}
self
}
/// Asigna o quita el icono al principio del elemento, antes de la etiqueta. Si se pasa `None`, /// Asigna o quita el icono al principio del elemento, antes de la etiqueta. Si se pasa `None`,
/// no se mostrará. /// no se mostrará.
/// ///

View file

@ -57,7 +57,7 @@ use crate::prelude::*;
/// .with_item(dropdown::Item::link(Lc::n("Generator"), "/tools/gen")) /// .with_item(dropdown::Item::link(Lc::n("Generator"), "/tools/gen"))
/// .with_item(dropdown::Item::link(Lc::n("Reports"), "/tools/reports")), /// .with_item(dropdown::Item::link(Lc::n("Reports"), "/tools/reports")),
/// )) /// ))
/// .with_item(nav::Item::link_disabled(Lc::n("Disabled"), "#")), /// .with_item(nav::Item::link(Lc::n("Disabled"), "#").with_disabled(true)),
/// )); /// ));
/// ``` /// ```
/// ///

View file

@ -193,3 +193,42 @@ async fn separators_are_also_cleaned_in_a_dropdown_inside_a_nav() {
assert!(!html.contains("dropdown-divider"), "{html}"); assert!(!html.contains("dropdown-divider"), "{html}");
assert!(html.contains("item-shown"), "{html}"); assert!(html.contains("item-shown"), "{html}");
} }
// **< Disabled items >*****************************************************************************
async fn render_item(mut item: dropdown::Item) -> String {
item.render(&mut Context::default()).await.into_string()
}
#[pagetop::test]
async fn a_disabled_link_has_no_href() {
let html = render_item(dropdown::Item::link(Lc::n("Home"), "/").with_disabled(true)).await;
assert!(html.contains(r#"class="dropdown-item disabled""#), "{html}");
assert!(html.contains(r#"aria-disabled="true""#));
assert!(!html.contains("href="));
// Opening in a new window does not apply to a link that cannot be followed.
let html = render_item(
dropdown::Item::link_blank(Lc::n("Docs"), "https://docs.rs").with_disabled(true),
)
.await;
assert!(!html.contains("target="), "{html}");
let html = render_item(dropdown::Item::link(Lc::n("Home"), "/").with_disabled(false)).await;
assert!(html.contains(r#"href="/""#), "{html}");
assert!(!html.contains("disabled"));
}
#[pagetop::test]
async fn a_disabled_button_is_disabled() {
let html = render_item(dropdown::Item::button(Lc::n("Save")).with_disabled(true)).await;
assert!(html.contains(r#"aria-disabled="true""#), "{html}");
assert!(html.contains(r#" disabled"#));
}
#[pagetop::test]
async fn disabled_does_not_apply_to_labels() {
let label = render_item(dropdown::Item::label(Lc::n("Note"))).await;
let disabled = render_item(dropdown::Item::label(Lc::n("Note")).with_disabled(true)).await;
assert_eq!(label, disabled);
}

42
tests/component_nav.rs Normal file
View file

@ -0,0 +1,42 @@
use pagetop::prelude::*;
async fn render_item(mut item: nav::Item) -> String {
item.render(&mut Context::default()).await.into_string()
}
#[pagetop::test]
async fn a_disabled_link_has_no_href() {
let html = render_item(nav::Item::link(Lc::n("Shop"), "/shop").with_disabled(true)).await;
assert!(html.contains(r#"class="nav-link disabled""#), "{html}");
assert!(html.contains(r#"aria-disabled="true""#));
assert!(!html.contains("href="));
// Opening in a new window does not apply to a link that cannot be followed.
let html =
render_item(nav::Item::link_blank(Lc::n("Docs"), "https://docs.rs").with_disabled(true))
.await;
assert!(!html.contains("target="), "{html}");
assert!(!html.contains("rel="));
let html = render_item(nav::Item::link_blank(Lc::n("Docs"), "https://docs.rs")).await;
assert!(html.contains(r#"target="_blank""#), "{html}");
}
#[pagetop::test]
async fn disabled_can_be_turned_off() {
let html = render_item(
nav::Item::link(Lc::n("Shop"), "/shop")
.with_disabled(true)
.with_disabled(false),
)
.await;
assert!(html.contains(r#"href="/shop""#), "{html}");
assert!(!html.contains("disabled"));
}
#[pagetop::test]
async fn disabled_does_not_apply_to_labels() {
let label = render_item(nav::Item::label(Lc::n("Note"))).await;
let disabled = render_item(nav::Item::label(Lc::n("Note")).with_disabled(true)).await;
assert_eq!(label, disabled);
}