✨ (form): Añade campos de fecha y hora por idioma
Nuevo `form:📅:Field` (fecha, hora y fecha y hora) que muestra el
valor en el formato del idioma efectivo (dd/mm/aaaa, mm/dd/yyyy...) y no
con el selector nativo, que sigue el formato del navegador. Contextual
añade `parse_date()`, `parse_time()` y `parse_datetime()` para leer lo
escrito.
This commit is contained in:
parent
144127776d
commit
fc71a077ee
11 changed files with 940 additions and 2 deletions
117
tests/component_form_date.rs
Normal file
117
tests/component_form_date.rs
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
fn cx(language: &str, timezone: &str) -> Context {
|
||||
Context::default()
|
||||
.with_langid(&Locale::resolve(language))
|
||||
.with_timezone(timezone.parse().unwrap())
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn dates_are_shown_in_the_format_of_the_language() {
|
||||
let date = NaiveDate::from_ymd_opt(2026, 10, 6).unwrap();
|
||||
|
||||
let mut field = form::date::Field::date()
|
||||
.with_name("expires")
|
||||
.with_date(date);
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"value="06/10/2026""#));
|
||||
assert!(html.contains(r#"placeholder="dd/mm/aaaa""#));
|
||||
assert!(html.contains("form-field form-field-date"));
|
||||
|
||||
let html = field.render(&mut cx("en-US", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"value="10/06/2026""#));
|
||||
assert!(html.contains(r#"placeholder="mm/dd/yyyy""#));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn datetimes_are_shown_in_the_local_time_of_the_user() {
|
||||
let dt = Utc.with_ymd_and_hms(2026, 10, 6, 12, 30, 0).unwrap();
|
||||
|
||||
let mut field = form::date::Field::datetime()
|
||||
.with_name("at")
|
||||
.with_datetime(dt);
|
||||
let html = field
|
||||
.render(&mut cx("es-ES", "Europe/Madrid"))
|
||||
.await
|
||||
.into_string();
|
||||
assert!(html.contains(r#"value="06/10/2026 14:30""#));
|
||||
assert!(html.contains(r#"placeholder="dd/mm/aaaa hh:mm""#));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn shown_values_are_read_back_unchanged() {
|
||||
let dt = Utc.with_ymd_and_hms(2026, 10, 6, 12, 30, 0).unwrap();
|
||||
let mut cx = cx("en-US", "America/New_York");
|
||||
|
||||
let mut field = form::date::Field::datetime()
|
||||
.with_name("at")
|
||||
.with_datetime(dt);
|
||||
let html = field.render(&mut cx).await.into_string();
|
||||
let value = html
|
||||
.split(r#"value=""#)
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.split('"')
|
||||
.next()
|
||||
.unwrap();
|
||||
assert_eq!(cx.parse_datetime(value), Ok(Some(dt)));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn text_is_shown_as_written_and_empty_fields_have_no_value() {
|
||||
let mut field = form::date::Field::time().with_name("at").with_text("25:99");
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"value="25:99""#));
|
||||
assert!(html.contains("form-field form-field-time"));
|
||||
|
||||
let mut empty = form::date::Field::date().with_name("expires");
|
||||
let html = empty.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(!html.contains("value="));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn a_value_of_another_kind_is_ignored() {
|
||||
let date = NaiveDate::from_ymd_opt(2026, 10, 6).unwrap();
|
||||
let mut field = form::date::Field::time().with_name("at").with_date(date);
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(!html.contains("value="));
|
||||
}
|
||||
|
||||
// The format stays visible (and is announced by screen readers) once the placeholder is gone.
|
||||
#[pagetop::test]
|
||||
async fn the_format_is_also_a_help_text_linked_to_the_input() {
|
||||
let mut field = form::date::Field::date().with_name("expires");
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"aria-describedby="edit-expires-input-format""#));
|
||||
assert!(html.contains(
|
||||
r#"<div id="edit-expires-input-format" class="form-text">Formato: dd/mm/aaaa</div>"#
|
||||
));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn autocomplete_is_off_unless_set() {
|
||||
let mut field = form::date::Field::date().with_name("born");
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"autocomplete="off""#));
|
||||
|
||||
let mut field = form::date::Field::date()
|
||||
.with_name("born")
|
||||
.with_autocomplete(form::Autocomplete::token(form::AutofillField::Bday));
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"autocomplete="bday""#));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn required_readonly_and_disabled_are_rendered() {
|
||||
let mut field = form::date::Field::date()
|
||||
.with_name("expires")
|
||||
.with_label(Lc::n("Expires"))
|
||||
.with_required(true)
|
||||
.with_readonly(true)
|
||||
.with_disabled(true);
|
||||
let html = field.render(&mut cx("es-ES", "UTC")).await.into_string();
|
||||
assert!(html.contains(r#"class="form-required""#));
|
||||
assert!(html.contains(" readonly"));
|
||||
assert!(html.contains(" required"));
|
||||
assert!(html.contains(" disabled"));
|
||||
}
|
||||
151
tests/datetime_input.rs
Normal file
151
tests/datetime_input.rs
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
async fn setup() {
|
||||
Application::new().await;
|
||||
}
|
||||
|
||||
fn cx(language: &str, timezone: &str) -> Context {
|
||||
Context::default()
|
||||
.with_langid(&Locale::resolve(language))
|
||||
.with_timezone(timezone.parse().unwrap())
|
||||
}
|
||||
|
||||
fn date(y: i32, m: u32, d: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, m, d).unwrap()
|
||||
}
|
||||
|
||||
fn utc(y: i32, m: u32, d: u32, h: u32, min: u32) -> DateTime<Utc> {
|
||||
Utc.with_ymd_and_hms(y, m, d, h, min, 0).unwrap()
|
||||
}
|
||||
|
||||
// **< Contextual::parse_date() >*******************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn dates_are_read_in_the_format_of_the_language() {
|
||||
setup().await;
|
||||
|
||||
let es = cx("es-ES", "UTC");
|
||||
assert_eq!(es.parse_date("06/10/2026"), Ok(Some(date(2026, 10, 6))));
|
||||
assert_eq!(es.parse_date(" 6/1/2026 "), Ok(Some(date(2026, 1, 6))));
|
||||
|
||||
let en = cx("en-US", "UTC");
|
||||
assert_eq!(en.parse_date("10/06/2026"), Ok(Some(date(2026, 10, 6))));
|
||||
}
|
||||
|
||||
// British English has no translations of its own except the day-first date formats; the rest falls
|
||||
// back to en-US. Reading 06/10/2026 month first would silently store 10 June instead of 6 October.
|
||||
#[pagetop::test]
|
||||
async fn british_english_reads_and_shows_the_day_first() {
|
||||
setup().await;
|
||||
|
||||
let gb = cx("en-GB", "UTC");
|
||||
assert_eq!(gb.parse_date("06/10/2026"), Ok(Some(date(2026, 10, 6))));
|
||||
assert_eq!(
|
||||
gb.parse_date("10/31/2026"),
|
||||
Err(DateInputError::InvalidDate {
|
||||
hint: "dd/mm/yyyy".to_string()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
gb.format_date(date(2026, 10, 6), DateFormat::Medium),
|
||||
"06/10/2026"
|
||||
);
|
||||
assert_eq!(
|
||||
gb.format_date(date(2026, 10, 6), DateFormat::Long),
|
||||
"6 October 2026"
|
||||
);
|
||||
assert_eq!(
|
||||
Lc::l("relative_today").lookup(&gb).as_deref(),
|
||||
Some("today")
|
||||
);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn dates_also_accept_iso_and_blank_is_none() {
|
||||
setup().await;
|
||||
|
||||
let es = cx("es-ES", "UTC");
|
||||
assert_eq!(es.parse_date("2026-10-06"), Ok(Some(date(2026, 10, 6))));
|
||||
assert_eq!(es.parse_date(" "), Ok(None));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn invalid_dates_report_the_expected_format() {
|
||||
setup().await;
|
||||
|
||||
let es = cx("es-ES", "UTC");
|
||||
let expected = Err(DateInputError::InvalidDate {
|
||||
hint: "dd/mm/aaaa".to_string(),
|
||||
});
|
||||
// A two-digit year would otherwise be read as year 26.
|
||||
assert_eq!(es.parse_date("06/10/26"), expected);
|
||||
assert_eq!(es.parse_date("31/02/2026"), expected);
|
||||
assert_eq!(es.parse_date("10/31/2026"), expected);
|
||||
|
||||
let message = es.parse_date("x").unwrap_err().message();
|
||||
assert_eq!(
|
||||
message.lookup(&es).as_deref(),
|
||||
Some("Fecha no válida: usa el formato dd/mm/aaaa.")
|
||||
);
|
||||
}
|
||||
|
||||
// **< Contextual::parse_time() >*******************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn times_are_read_with_one_or_two_digit_hours_and_minutes() {
|
||||
setup().await;
|
||||
|
||||
let es = cx("es-ES", "UTC");
|
||||
assert_eq!(es.parse_time("9:05"), Ok(NaiveTime::from_hms_opt(9, 5, 0)));
|
||||
assert_eq!(es.parse_time("9:5"), Ok(NaiveTime::from_hms_opt(9, 5, 0)));
|
||||
assert_eq!(
|
||||
es.parse_time("14:30"),
|
||||
Ok(NaiveTime::from_hms_opt(14, 30, 0))
|
||||
);
|
||||
assert!(matches!(
|
||||
es.parse_time("25:00"),
|
||||
Err(DateInputError::InvalidTime { .. })
|
||||
));
|
||||
}
|
||||
|
||||
// **< Contextual::parse_datetime() >***************************************************************
|
||||
|
||||
#[pagetop::test]
|
||||
async fn datetimes_are_read_in_the_local_time_of_the_user() {
|
||||
setup().await;
|
||||
|
||||
// Madrid is UTC+2 in October (summer time).
|
||||
let es = cx("es-ES", "Europe/Madrid");
|
||||
let expected = Ok(Some(utc(2026, 10, 6, 12, 30)));
|
||||
assert_eq!(es.parse_datetime("06/10/2026 14:30"), expected);
|
||||
assert_eq!(es.parse_datetime("2026-10-06T14:30"), expected);
|
||||
assert_eq!(es.parse_datetime("2026-10-06 14:30"), expected);
|
||||
|
||||
let en = cx("en-US", "Europe/Madrid");
|
||||
assert_eq!(en.parse_datetime("10/06/2026 14:30"), expected);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn nonexistent_local_times_are_rejected() {
|
||||
setup().await;
|
||||
|
||||
// On 2026-03-29 Madrid clocks jump from 02:00 to 03:00.
|
||||
let es = cx("es-ES", "Europe/Madrid");
|
||||
assert_eq!(
|
||||
es.parse_datetime("29/03/2026 02:30"),
|
||||
Err(DateInputError::NonexistentTime)
|
||||
);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn ambiguous_local_times_take_the_first_occurrence() {
|
||||
setup().await;
|
||||
|
||||
// On 2026-10-25 Madrid clocks go back from 03:00 to 02:00, so 02:30 happens twice: first at
|
||||
// UTC+2 (00:30 UTC), then at UTC+1 (01:30 UTC).
|
||||
let es = cx("es-ES", "Europe/Madrid");
|
||||
assert_eq!(
|
||||
es.parse_datetime("25/10/2026 02:30"),
|
||||
Ok(Some(utc(2026, 10, 25, 0, 30)))
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue