(base): Añade componente Table

Incluye columnas ordenables (`table::SortLink`), paso de props/atributos
a filas y celdas para HTMX, y un mensaje de "sin datos" traducible con
valor por defecto.
This commit is contained in:
Manuel Cillero 2026-08-13 15:17:23 +02:00
parent 17a9f0c9e0
commit 5842ea6efc
10 changed files with 723 additions and 0 deletions

View file

@ -0,0 +1,114 @@
use crate::prelude::*;
/// Representa una celda de datos (`<td>`) de una [`table::Row`].
///
/// El contenido es una lista [`Children`] para admitir cualquier número de componentes.
/// [`Cell::new()`] añade un componente como contenido inicial, normalmente un [`Lc`] con texto
/// literal ([`Lc::n()`]) o traducible (con [`Lc::l()`]/[`Lc::t()`]).
///
/// Un `&str`, un `String` o un [`Lc`] se convierten directamente en `Cell`; para estos tipos no
/// sería necesario llamar a [`Cell::new()`] de forma explícita.
///
/// ```rust,no_run
/// use pagetop::prelude::*;
///
/// let email: table::Cell = "ana@example.com".into();
/// let label: table::Cell = Lc::l("table-status").into();
/// let name = table::Cell::new(Html::with(|_| html! { "Julia" }));
/// ```
#[derive(AutoDefault, Clone, Debug, Getters)]
pub struct Cell {
/// Devuelve identificador, clases CSS y atributos HTML de la celda (`<td>`).
props: Props,
/// Devuelve la lista de componentes hijo que genera el contenido de la celda.
children: Children,
}
impl Cell {
/// Crea una celda a partir del componente o texto ([`Lc`]) indicado.
///
/// Para combinar varios componentes en la misma celda basta con encadenar llamadas a
/// [`with_child()`](Cell::with_child):
///
/// ```rust,no_run
/// # use pagetop::prelude::*;
/// let status = table::Cell::new(Lc::n("Admin ")).with_child(Badge::labeled(Lc::n("active")));
/// ```
pub fn new(child: impl Into<Child>) -> Self {
Self {
children: Children::with(child.into()),
..Default::default()
}
}
// **< Cell BUILDER >***************************************************************************
/// Establece el identificador único de la celda.
#[builder_fn]
pub fn with_id(mut self, id: impl Into<CowStr>) -> Self {
self.props.alter_id(id);
self
}
/// Modifica identificador, clases CSS o atributos HTML de la celda.
#[builder_fn]
pub fn with_prop(mut self, op: PropsOp) -> Self {
self.props.alter_prop(op);
self
}
/// Establece el número de columnas que ocupa la celda (atributo `colspan`).
///
/// Con `1` (el valor por defecto de HTML) elimina el atributo en vez de fijarlo.
#[builder_fn]
pub fn with_colspan(mut self, span: u8) -> Self {
self.props.alter_prop(if span == 1 {
PropsOp::remove("colspan")
} else {
PropsOp::set("colspan", span.to_string())
});
self
}
/// Establece el número de filas que ocupa la celda (atributo `rowspan`).
///
/// Con `1` (el valor por defecto de HTML) elimina el atributo en vez de fijarlo.
#[builder_fn]
pub fn with_rowspan(mut self, span: u8) -> Self {
self.props.alter_prop(if span == 1 {
PropsOp::remove("rowspan")
} else {
PropsOp::set("rowspan", span.to_string())
});
self
}
/// Añade un nuevo componente a la celda o modifica la lista de componentes (`children`) con una
/// operación [`ChildOp`].
#[builder_fn]
pub fn with_child(mut self, op: impl Into<ChildOp>) -> Self {
self.children.alter_child(op.into());
self
}
}
impl From<&str> for Cell {
/// Convierte un `&str` en una celda de texto literal; equivale a `Cell::new(Lc::n(text))`.
fn from(text: &str) -> Self {
Cell::new(Lc::n(text.to_string()))
}
}
impl From<String> for Cell {
/// Convierte un `String` en una celda de texto literal; equivale a `Cell::new(Lc::n(text))`.
fn from(text: String) -> Self {
Cell::new(Lc::n(text))
}
}
impl From<Lc> for Cell {
/// Convierte un [`Lc`] en una celda traducible; equivale a `Cell::new(label)`.
fn from(label: Lc) -> Self {
Cell::new(label)
}
}