♻️ (form): Reúne etiqueta y ayuda en form::parts

Label y Help generan el marcado común de los campos de formulario. La
etiqueta con la marca opcional de obligatorio y el texto de ayuda, con
sus enlaces de accesibilidad.
This commit is contained in:
Manuel Cillero 2026-10-10 13:15:49 +02:00
parent 8690f994c3
commit d80a6787c7
20 changed files with 534 additions and 262 deletions

View file

@ -55,7 +55,7 @@ pub(crate) fn setup(field: &mut Field) {
pub(crate) fn render(field: &Field, cx: &mut Context) -> Result<Markup, ComponentError> {
let container_id = field.id();
let input_id = container_id.as_deref().map(|id| util::join!(id, "-input"));
let help = form::FieldHelp::new(field.help_text(), input_id.as_deref(), cx);
let help = form::parts::Help::new(field.help_text(), input_id.as_deref());
let input_class = if *field.plaintext() {
"form-control-plaintext"
} else {
@ -77,26 +77,12 @@ pub(crate) fn render(field: &Field, cx: &mut Context) -> Result<Markup, Componen
} else {
field.placeholder().lookup(cx)
};
let label = match field.label().lookup(cx) {
Some(text) => html! {
label for=[input_id.as_deref()] class="form-label" {
(text)
@if *field.required() {
span
class="form-required"
title=[Lc::l("field_required").lookup(cx)]
{
"*"
}
}
}
},
None => html! {},
};
let label = form::parts::Label::new(field.label(), input_id.as_deref())
.with_required(*field.required());
Ok(html! {
div (field.props().unpack(cx)) {
@if !floating { (label) }
@if !floating { (label.render(cx)) }
input
type=(field.kind())
id=[input_id.as_deref()]
@ -117,8 +103,8 @@ pub(crate) fn render(field: &Field, cx: &mut Context) -> Result<Markup, Componen
onfocus=[strict.then_some("this.removeAttribute('readonly')")]
required[*field.required()]
disabled[*field.disabled()];
@if floating { (label) }
(help)
@if floating { (label.render(cx)) }
(help.render(cx))
}
})
}

View file

@ -62,27 +62,13 @@ pub(crate) fn setup(field: &mut Field) {
pub(crate) fn render(field: &Field, cx: &mut Context) -> Result<Markup, ComponentError> {
let container_id = field.id();
let select_id = container_id.as_deref().map(|id| util::join!(id, "-select"));
let help = form::FieldHelp::new(field.help_text(), select_id.as_deref(), cx);
let help = form::parts::Help::new(field.help_text(), select_id.as_deref());
let floating = field.props().extra_or(EXTRA_FLOATING_LABEL, false);
let label = match field.label().lookup(cx) {
Some(text) => html! {
label for=[select_id.as_deref()] class="form-label" {
(text)
@if *field.required() {
span
class="form-required"
title=[Lc::l("field_required").lookup(cx)]
{
"*"
}
}
}
},
None => html! {},
};
let label = form::parts::Label::new(field.label(), select_id.as_deref())
.with_required(*field.required());
Ok(html! {
div (field.props().unpack(cx)) {
@if !floating { (label) }
@if !floating { (label.render(cx)) }
select
id=[select_id.as_deref()]
aria-describedby=[help.id()]
@ -125,8 +111,8 @@ pub(crate) fn render(field: &Field, cx: &mut Context) -> Result<Markup, Componen
}
}
}
@if floating { (label) }
(help)
@if floating { (label.render(cx)) }
(help.render(cx))
}
})
}

View file

@ -61,7 +61,7 @@ pub(crate) fn render(field: &Textarea, cx: &mut Context) -> Result<Markup, Compo
let textarea_id = container_id
.as_deref()
.map(|id| util::join!(id, "-textarea"));
let help = form::FieldHelp::new(field.help_text(), textarea_id.as_deref(), cx);
let help = form::parts::Help::new(field.help_text(), textarea_id.as_deref());
let floating = field.props().extra_or(EXTRA_FLOATING_LABEL, false);
// La etiqueta flotante requiere `placeholder` para animar la etiqueta; si no está definido se
// fuerza `placeholder=""`.
@ -70,25 +70,11 @@ pub(crate) fn render(field: &Textarea, cx: &mut Context) -> Result<Markup, Compo
} else {
field.placeholder().lookup(cx)
};
let label = match field.label().lookup(cx) {
Some(text) => html! {
label for=[textarea_id.as_deref()] class="form-label" {
(text)
@if *field.required() {
span
class="form-required"
title=[Lc::l("field_required").lookup(cx)]
{
"*"
}
}
}
},
None => html! {},
};
let label = form::parts::Label::new(field.label(), textarea_id.as_deref())
.with_required(*field.required());
Ok(html! {
div (field.props().unpack(cx)) {
@if !floating { (label) }
@if !floating { (label.render(cx)) }
textarea
id=[textarea_id.as_deref()]
aria-describedby=[help.id()]
@ -106,8 +92,8 @@ pub(crate) fn render(field: &Textarea, cx: &mut Context) -> Result<Markup, Compo
{
@if let Some(value) = field.value().as_deref() { (value) }
}
@if floating { (label) }
(help)
@if floating { (label.render(cx)) }
(help.render(cx))
}
})
}