From f1fd34f974ef0a08b8d019353884b1769916118e Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 23 Nov 2026 02:01:06 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(pagetop):=20Deshabilita?= =?UTF-8?q?=20men=C3=BAs=20con=20with=5Fdisabled()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cambia `link_disabled()`, `link_blank_disabled()` y `button_disabled()` de `nav::Item` y `dropdown::Item` por `with_disabled(bool)`, como en Button. --- examples/basic-nav.rs | 2 +- examples/basic-navbar.rs | 5 +- examples/navbar-menus.rs | 20 ++++---- examples/theme-showcase.rs | 15 +++--- .../pagetop-bootsier/src/theme/bs/nav.rs | 2 +- .../src/theme/bs/navbar/component.rs | 2 +- src/base/component/dropdown/item.rs | 48 +++++-------------- src/base/component/nav/component.rs | 4 +- src/base/component/nav/item.rs | 35 ++++---------- src/base/component/navbar/component.rs | 2 +- tests/component_dropdown.rs | 39 +++++++++++++++ tests/component_nav.rs | 42 ++++++++++++++++ 12 files changed, 123 insertions(+), 93 deletions(-) create mode 100644 tests/component_nav.rs diff --git a/examples/basic-nav.rs b/examples/basic-nav.rs index ba875b57..b26d7599 100644 --- a/examples/basic-nav.rs +++ b/examples/basic-nav.rs @@ -24,7 +24,7 @@ impl Extension for NavDemo { .with_item(dropdown::Item::header(Lc::n("Danger zone"))) .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() .with_title(Lc::n("Account")) diff --git a/examples/basic-navbar.rs b/examples/basic-navbar.rs index 544d3d44..46f29207 100644 --- a/examples/basic-navbar.rs +++ b/examples/basic-navbar.rs @@ -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_disabled( - Lc::t("navbar_disabled", &LOC), - "#", - )); + .with_item(nav::Item::link(Lc::t("navbar_disabled", &LOC), "#").with_disabled(true)); let navbar = Navbar::brand_left(brand).with_item(navbar::Item::nav(main_nav)); diff --git a/examples/navbar-menus.rs b/examples/navbar-menus.rs index 249669b0..e0c0122e 100644 --- a/examples/navbar-menus.rs +++ b/examples/navbar-menus.rs @@ -106,15 +106,14 @@ fn menus_block() -> Block { )) .with_item(dropdown::Item::divider()) .with_item(dropdown::Item::label(Lc::t("menus_item_label", &LOC))) - .with_item(dropdown::Item::link_disabled( - Lc::t("menus_item_disabled", &LOC), - "#", - )), + .with_item( + dropdown::Item::link(Lc::t("menus_item_disabled", &LOC), "#") + .with_disabled(true), + ), )) - .with_item(nav::Item::link_disabled( - Lc::t("menus_item_disabled", &LOC), - "#", - )), + .with_item( + nav::Item::link(Lc::t("menus_item_disabled", &LOC), "#").with_disabled(true), + ), )) .with_item(navbar::Item::nav( Nav::new() @@ -686,10 +685,7 @@ fn demo_nav() -> Nav { "/something", )), )) - .with_item(nav::Item::link_disabled( - Lc::t("menus_item_disabled", &LOC), - "#", - )) + .with_item(nav::Item::link(Lc::t("menus_item_disabled", &LOC), "#").with_disabled(true)) } fn is_bootsier() -> bool { diff --git a/examples/theme-showcase.rs b/examples/theme-showcase.rs index c4dedc7d..2b7a7b44 100644 --- a/examples/theme-showcase.rs +++ b/examples/theme-showcase.rs @@ -46,19 +46,18 @@ impl Extension for ThemeShowcase { Lc::t("showcase_menu_messages", &LOC), "#", )) - .with_item(dropdown::Item::link_disabled( - Lc::t("showcase_menu_disabled", &LOC), - "#", - )), + .with_item( + dropdown::Item::link(Lc::t("showcase_menu_disabled", &LOC), "#") + .with_disabled(true), + ), )) .with_item(nav::Item::link_blank( Lc::t("showcase_menu_docs", &LOC), "https://docs.rs/pagetop", )) - .with_item(nav::Item::link_disabled( - Lc::t("showcase_menu_disabled", &LOC), - "#", - )); + .with_item( + 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 // (claro, oscuro o automático). diff --git a/extensions/pagetop-bootsier/src/theme/bs/nav.rs b/extensions/pagetop-bootsier/src/theme/bs/nav.rs index dcc72f67..c3b6189b 100644 --- a/extensions/pagetop-bootsier/src/theme/bs/nav.rs +++ b/extensions/pagetop-bootsier/src/theme/bs/nav.rs @@ -42,7 +42,7 @@ pub(crate) const EXTRA_IN_NAVBAR: &str = "bootsier.nav.in_navbar"; /// 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] pub trait NavBsExt { diff --git a/extensions/pagetop-bootsier/src/theme/bs/navbar/component.rs b/extensions/pagetop-bootsier/src/theme/bs/navbar/component.rs index 7de60cd7..3b9f897d 100644 --- a/extensions/pagetop-bootsier/src/theme/bs/navbar/component.rs +++ b/extensions/pagetop-bootsier/src/theme/bs/navbar/component.rs @@ -74,7 +74,7 @@ const EXTRA_LAYOUT: &str = "bootsier.navbar.layout"; /// 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)) /// )); /// ``` /// diff --git a/src/base/component/dropdown/item.rs b/src/base/component/dropdown/item.rs index 00bb9fea..e01f1f35 100644 --- a/src/base/component/dropdown/item.rs +++ b/src/base/component/dropdown/item.rs @@ -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) -> 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. pub fn link_blank(label: Lc, route: impl Into) -> 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) -> 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. pub fn button(label: Lc) -> 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ú. pub fn header(label: Lc) -> Self { Self { @@ -289,6 +252,17 @@ impl Item { 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`, /// no se mostrará. /// diff --git a/src/base/component/nav/component.rs b/src/base/component/nav/component.rs index 3a267c68..fb902a68 100644 --- a/src/base/component/nav/component.rs +++ b/src/base/component/nav/component.rs @@ -25,7 +25,7 @@ use crate::prelude::*; /// .with_item(dropdown::Item::link(Lc::n("Action"), "/action")) /// .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)] pub struct Nav { @@ -237,7 +237,7 @@ impl Nav { /// nav.with_item(nav::Item::link("Inicio", "/")); /// nav.with_item(TypedOp::AddMany(vec![ /// nav::Item::link(...), - /// nav::Item::link_disabled(...), + /// nav::Item::link(...).with_disabled(true), /// ])); /// ``` pub fn with_item(mut self, op: impl Into>) -> Self { diff --git a/src/base/component/nav/item.rs b/src/base/component/nav/item.rs index 1f3f9459..178117b1 100644 --- a/src/base/component/nav/item.rs +++ b/src/base/component/nav/item.rs @@ -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) -> 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. pub fn link_blank(label: Lc, route: impl Into) -> 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) -> Self { - Self { - item_kind: ItemKind::Link { - label, - route: route.into(), - blank: true, - disabled: true, - }, - ..Default::default() - } - } - /// Crea un elemento con contenido HTML arbitrario. /// /// El contenido se renderiza tal cual lo devuelve el componente [`Html`], dentro de un `
  • ` @@ -299,6 +273,15 @@ impl Item { 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`, /// no se mostrará. /// diff --git a/src/base/component/navbar/component.rs b/src/base/component/navbar/component.rs index fd3efc28..4729be23 100644 --- a/src/base/component/navbar/component.rs +++ b/src/base/component/navbar/component.rs @@ -57,7 +57,7 @@ use crate::prelude::*; /// .with_item(dropdown::Item::link(Lc::n("Generator"), "/tools/gen")) /// .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)), /// )); /// ``` /// diff --git a/tests/component_dropdown.rs b/tests/component_dropdown.rs index f48044f2..7c388952 100644 --- a/tests/component_dropdown.rs +++ b/tests/component_dropdown.rs @@ -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("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); +} diff --git a/tests/component_nav.rs b/tests/component_nav.rs new file mode 100644 index 00000000..8d27d298 --- /dev/null +++ b/tests/component_nav.rs @@ -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); +}