✨ (base): Oculta separadores en menús sin opciones

`Dropdown::items()` devuelve `dropdown::Items`, cuyo `render()` sólo
pinta un encabezado si su grupo tiene algún elemento visible, y un
separador sólo entre elementos visibles. Lo aprovecha cualquier tema
que pinte el menú con `items().render()`.
This commit is contained in:
Manuel Cillero 2026-11-08 10:11:42 +01:00
parent e2e2ccb31e
commit c940403869
9 changed files with 292 additions and 10 deletions

View file

@ -68,6 +68,15 @@ async fn child_id_returns_component_id() {
assert_eq!(child.id(), Some("my-id".to_string()));
}
#[pagetop::test]
async fn child_downcast_ref_returns_the_component_only_with_its_type() {
let child = Child::with(TestComp::text("hello"));
let component = child.downcast_ref::<TestComp>().expect("a TestComp");
assert_eq!(component.text, "hello");
assert!(child.downcast_ref::<Html>().is_none());
assert!(Child::default().downcast_ref::<TestComp>().is_none());
}
#[pagetop::test]
async fn child_from_component_is_equivalent_to_with() {
let child: Child = TestComp::text("from-trait").into();
@ -257,6 +266,19 @@ async fn children_iter_by_id_yields_all_matching_children() {
assert_eq!(c.iter_by_id("none").count(), 0);
}
#[pagetop::test]
async fn children_iter_yields_all_children_in_order() {
let c = Children::new()
.with_child(TestComp::text("a"))
.with_child(TestComp::text("b"));
let texts: Vec<&str> = c
.iter()
.filter_map(|child| child.downcast_ref::<TestComp>())
.map(|component| component.text.as_str())
.collect();
assert_eq!(texts, ["a", "b"]);
}
#[pagetop::test]
async fn children_render_concatenates_all_outputs_in_order() {
let c = Children::new()

View file

@ -48,3 +48,148 @@ async fn menu_end_is_ignored_without_title() {
assert!(html.contains(r#"<ul class="dropdown-menu">"#));
assert!(!html.contains("dropdown-menu-end"));
}
// **< Separators >*********************************************************************************
// A route that nobody can see without a request: its links are always hidden in these tests.
#[derive(Clone, Copy)]
struct Hidden;
impl Endpoint for Hidden {
fn path(&self) -> CowStr {
"/hidden".into()
}
fn access(&self) -> Access {
Access::Authenticated
}
}
fn shown(label: &'static str) -> dropdown::Item {
dropdown::Item::link(Lc::n(label), "/shown")
}
fn hidden(label: &'static str) -> dropdown::Item {
dropdown::Item::link(Lc::n(label), Hidden)
}
fn header(label: &'static str) -> dropdown::Item {
dropdown::Item::header(Lc::n(label))
}
// Position of `text` in `html`, which must contain it.
fn at(html: &str, text: &str) -> usize {
html.find(text)
.unwrap_or_else(|| panic!("`{text}` is missing in {html}"))
}
async fn render_menu(items: Vec<dropdown::Item>) -> String {
Dropdown::new()
.with_title(Lc::n("Menu"))
.with_item(TypedOp::AddMany(items))
.render(&mut Context::default())
.await
.into_string()
}
#[pagetop::test]
async fn a_header_without_visible_items_is_not_shown() {
let html = render_menu(vec![
header("group-empty"),
hidden("item-hidden"),
header("group-full"),
shown("item-shown"),
])
.await;
assert!(!html.contains("group-empty"), "{html}");
assert!(!html.contains("item-hidden"), "{html}");
assert!(html.contains("group-full"), "{html}");
assert!(at(&html, "group-full") < at(&html, "item-shown"), "{html}");
}
#[pagetop::test]
async fn dividers_only_separate_visible_items() {
let html = render_menu(vec![
dropdown::Item::divider(),
shown("first"),
dropdown::Item::divider(),
hidden("item-hidden"),
dropdown::Item::divider(),
shown("second"),
dropdown::Item::divider(),
])
.await;
assert_eq!(html.matches("dropdown-divider").count(), 1, "{html}");
let divider = at(&html, "dropdown-divider");
assert!(
at(&html, "first") < divider && divider < at(&html, "second"),
"{html}"
);
}
#[pagetop::test]
async fn a_divider_and_a_header_are_shown_before_the_next_visible_group() {
let html = render_menu(vec![
shown("first"),
dropdown::Item::divider(),
header("group"),
shown("second"),
])
.await;
let (divider, group) = (at(&html, "dropdown-divider"), at(&html, "group"));
assert!(at(&html, "first") < divider && divider < group, "{html}");
assert!(group < at(&html, "second"), "{html}");
}
#[pagetop::test]
async fn labels_and_buttons_count_as_visible_items() {
let html = render_menu(vec![
header("group-label"),
dropdown::Item::label(Lc::n("text")),
header("group-button"),
dropdown::Item::button(Lc::n("action")),
])
.await;
assert!(html.contains("group-label"), "{html}");
assert!(html.contains("group-button"), "{html}");
}
#[pagetop::test]
async fn a_dropdown_with_only_separators_left_is_not_rendered() {
let html = render_menu(vec![
header("group"),
hidden("item-hidden"),
dropdown::Item::divider(),
])
.await;
assert!(html.is_empty(), "{html}");
}
#[pagetop::test]
async fn separators_are_also_cleaned_in_a_dropdown_inside_a_nav() {
let menu = Dropdown::new()
.with_title(Lc::n("menu-empty"))
.with_item(header("group-empty"))
.with_item(hidden("item-hidden"));
let partial = Dropdown::new()
.with_title(Lc::n("menu-partial"))
.with_item(header("group-empty"))
.with_item(hidden("item-hidden"))
.with_item(dropdown::Item::divider())
.with_item(shown("item-shown"));
let mut nav = Nav::new()
.with_item(nav::Item::dropdown(menu))
.with_item(nav::Item::dropdown(partial));
let html = nav.render(&mut Context::default()).await.into_string();
assert!(!html.contains("menu-empty"), "{html}");
assert!(html.contains("menu-partial"), "{html}");
assert!(!html.contains("group-empty"), "{html}");
assert!(!html.contains("dropdown-divider"), "{html}");
assert!(html.contains("item-shown"), "{html}");
}