✨ (base) Añade ListView y ListFilter para listados
- ListView: botones, mensajes, tabla y paginador en orden fijo; anuncia el número de resultados a los lectores de pantalla. - ListFilter: filtros en fila con "Filtrar" y "Limpiar", envío automático opcional y marcas de cambios sin aplicar. - pagetop-htmx: hx_pager::boost() y hx_filter::boost(); cancelan las peticiones solapadas y atenúan el listado mientras carga, con estilos sólo en las páginas con listado. - Usuarios y roles migrados al nuevo patrón.
This commit is contained in:
parent
0f71eba7ab
commit
d8493291e4
28 changed files with 1264 additions and 144 deletions
|
|
@ -10,7 +10,9 @@ impl Extension for TestApp {
|
|||
}
|
||||
|
||||
fn configure_router(&self, routes: Routes) -> Routes {
|
||||
routes.public_route("/page", web::get(render_page))
|
||||
routes
|
||||
.public_route("/page", web::get(render_page))
|
||||
.public_route("/list", web::get(render_list))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -21,6 +23,13 @@ async fn render_page(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
|||
.await
|
||||
}
|
||||
|
||||
async fn render_list(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||
Page::new(request)
|
||||
.with_child(ListView::new().with_table(Table::new().with_column(Lc::n("Name"))))
|
||||
.render()
|
||||
.await
|
||||
}
|
||||
|
||||
// All tests in this file share the same root extension (`TestApp`), since `EXTENSIONS` is a global
|
||||
// `OnceLock` initialized only once per test binary (see `core/extension/all.rs`).
|
||||
|
||||
|
|
@ -60,3 +69,22 @@ async fn rendered_pages_automatically_include_the_pinned_htmx_script_tag() {
|
|||
assert!(body.contains("defer"));
|
||||
assert!(body.contains("<p>hello</p>"));
|
||||
}
|
||||
|
||||
// **< List loading styles >************************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn pages_with_a_list_embed_the_list_loading_styles_and_others_do_not() {
|
||||
let app = web::test::init_router(Application::prepare(&TestApp).await.test());
|
||||
|
||||
let req = web::test::TestRequest::get().uri("/list").to_request();
|
||||
let body = web::test::read_body_text(web::test::send_request(&app, req).await).await;
|
||||
// HTMX sets `htmx-request` on the element named by `hx-indicator`: the list.
|
||||
assert!(
|
||||
body.contains(".list-view.htmx-request{opacity:0.5}"),
|
||||
"{body}"
|
||||
);
|
||||
|
||||
let req = web::test::TestRequest::get().uri("/page").to_request();
|
||||
let body = web::test::read_body_text(web::test::send_request(&app, req).await).await;
|
||||
assert!(!body.contains("htmx-request"), "{body}");
|
||||
}
|
||||
|
|
|
|||
100
extensions/pagetop-htmx/tests/hx_list.rs
Normal file
100
extensions/pagetop-htmx/tests/hx_list.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
use pagetop::prelude::*;
|
||||
use pagetop_htmx::prelude::*;
|
||||
|
||||
// Forces an effective language different from the default negotiated one (en-US, with no `?lang` in
|
||||
// the request), so that `Context::route()` decides to propagate `?lang=...` in local routes.
|
||||
fn cx_with_lang(lang: &str) -> Context {
|
||||
Context::default().with_langid(&Locale::resolve(lang))
|
||||
}
|
||||
|
||||
// **< hx_pager::boost() >**************************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn boost_targets_the_list_from_the_pager_nav() {
|
||||
let mut pager = hx_pager::boost(
|
||||
Pager::new()
|
||||
.with_base_path("/items")
|
||||
.with_current_page(1)
|
||||
.with_items_per_page(10)
|
||||
.with_total_items(50),
|
||||
"#item-list",
|
||||
);
|
||||
let html = pager.render(&mut Context::default()).await.into_string();
|
||||
|
||||
assert!(html.contains(r#"hx-boost="true""#), "{html}");
|
||||
assert!(html.contains(r##"hx-target="#item-list""##), "{html}");
|
||||
assert!(html.contains(r#"hx-swap="outerHTML scroll:top""#), "{html}");
|
||||
assert!(html.contains(r#"hx-push-url="true""#), "{html}");
|
||||
}
|
||||
|
||||
// **< hx_filter::boost() >*************************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn boost_sends_the_filter_form_to_the_list() {
|
||||
let mut filter = hx_filter::boost(
|
||||
ListFilter::new()
|
||||
.with_filter_action("/items")
|
||||
.with_field(form::input::Field::search().with_name("q")),
|
||||
"#item-list",
|
||||
);
|
||||
let html = filter.render(&mut Context::default()).await.into_string();
|
||||
|
||||
// `hx-boost` on the form sends its fields to its own `action`, so no `hx-get` is needed.
|
||||
assert!(html.contains(r#"hx-boost="true""#), "{html}");
|
||||
assert!(!html.contains("hx-get"), "{html}");
|
||||
// When it applies is up to the filter (button or auto submit), not to HTMX.
|
||||
assert!(!html.contains("hx-trigger"), "{html}");
|
||||
assert!(html.contains(r##"hx-target="#item-list""##), "{html}");
|
||||
assert!(html.contains(r#"hx-push-url="true""#), "{html}");
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn boosted_filter_keeps_the_language_in_a_hidden_field() {
|
||||
// A boosted GET form drops the query of its `action`, the same as the browser does.
|
||||
let mut filter = hx_filter::boost(
|
||||
ListFilter::new()
|
||||
.with_filter_action("/items")
|
||||
.with_field(form::input::Field::search().with_name("q")),
|
||||
"#item-list",
|
||||
);
|
||||
let html = filter
|
||||
.render(&mut cx_with_lang("es-ES"))
|
||||
.await
|
||||
.into_string();
|
||||
|
||||
assert!(html.contains(r#"name="lang""#), "{html}");
|
||||
assert!(html.contains(r#"value="es-ES""#), "{html}");
|
||||
}
|
||||
|
||||
// **< Overlapping requests and loading indicator >*************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn list_helpers_cancel_overlapping_requests_and_mark_the_list_while_loading() {
|
||||
let mut pager = hx_pager::boost(
|
||||
Pager::new()
|
||||
.with_base_path("/items")
|
||||
.with_current_page(1)
|
||||
.with_items_per_page(10)
|
||||
.with_total_items(50),
|
||||
"#item-list",
|
||||
);
|
||||
let mut filter = hx_filter::boost(ListFilter::new().with_filter_action("/items"), "#item-list");
|
||||
let mut table = Table::new().with_column(
|
||||
table::Column::new(Lc::n("Name")).with_sort(hx_table::sort_link(
|
||||
"/items",
|
||||
"#item-list",
|
||||
None,
|
||||
)),
|
||||
);
|
||||
|
||||
let mut cx = Context::default();
|
||||
for html in [
|
||||
pager.render(&mut cx).await.into_string(),
|
||||
filter.render(&mut cx).await.into_string(),
|
||||
table.render(&mut cx).await.into_string(),
|
||||
] {
|
||||
// Every request against the list cancels the one in progress, wherever it comes from.
|
||||
assert!(html.contains(r##"hx-sync="#item-list:replace""##), "{html}");
|
||||
assert!(html.contains(r##"hx-indicator="#item-list""##), "{html}");
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ async fn render_column(column: table::Column) -> String {
|
|||
// **< sort_link() - htmx attributes >**************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn sort_link_sets_the_four_fixed_htmx_attributes() {
|
||||
async fn sort_link_sets_the_fixed_htmx_attributes() {
|
||||
let column = table::Column::new(Lc::n("User")).with_sort(hx_table::sort_link(
|
||||
"/admin/users",
|
||||
"#user-table",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue