♻️ (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:
parent
5d6852382a
commit
f1fd34f974
12 changed files with 123 additions and 93 deletions
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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).
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
/// ));
|
||||
/// ```
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
pub fn link_blank(label: Lc, route: impl Into<Route>) -> 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.
|
||||
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á.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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<TypedOp<nav::Item>>) -> Self {
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
pub fn link_blank(label: Lc, route: impl Into<Route>) -> 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.
|
||||
///
|
||||
/// El contenido se renderiza tal cual lo devuelve el componente [`Html`], dentro de un `<li>`
|
||||
|
|
@ -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á.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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)),
|
||||
/// ));
|
||||
/// ```
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
42
tests/component_nav.rs
Normal file
42
tests/component_nav.rs
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue