🚧 (base): Añade BadgeKind para el tipo de Badge
This commit is contained in:
parent
997017bb75
commit
4ba66f5995
6 changed files with 230 additions and 43 deletions
|
|
@ -3,7 +3,7 @@
|
|||
pub mod layout;
|
||||
|
||||
mod badge;
|
||||
pub use badge::Badge;
|
||||
pub use badge::{Badge, BadgeKind};
|
||||
|
||||
mod brand;
|
||||
pub use brand::Brand;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,31 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
// **< BadgeKind >**********************************************************************************
|
||||
|
||||
/// Tipo de [`Badge`].
|
||||
#[derive(AutoDefault, Clone, Copy, Debug, PartialEq)]
|
||||
pub enum BadgeKind {
|
||||
Primary,
|
||||
#[default]
|
||||
Secondary,
|
||||
Success,
|
||||
Info,
|
||||
Warning,
|
||||
Danger,
|
||||
}
|
||||
|
||||
// **< Badge >**************************************************************************************
|
||||
|
||||
/// Componente para mostrar una **etiqueta corta informativa** (*badge*).
|
||||
///
|
||||
/// # Ejemplo
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use pagetop::prelude::*;
|
||||
/// let badge = Badge::labeled(Lc::n("Admin"));
|
||||
/// let badge = Badge::labeled(Lc::n("Admin")).with_kind(BadgeKind::Danger);
|
||||
///
|
||||
/// // Equivalente usando el constructor directo del tipo.
|
||||
/// let badge = Badge::danger(Lc::n("Admin"));
|
||||
/// ```
|
||||
#[derive(AutoDefault, Clone, Debug, Getters)]
|
||||
pub struct Badge {
|
||||
|
|
@ -14,6 +33,9 @@ pub struct Badge {
|
|||
props: Props,
|
||||
/// Devuelve la etiqueta del badge.
|
||||
label: Lc,
|
||||
/// Devuelve el tipo del badge.
|
||||
#[getters(copy)]
|
||||
kind: BadgeKind,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
|
@ -26,8 +48,16 @@ impl Component for Badge {
|
|||
self.props.get_id()
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
fn setup(&mut self, _cx: &Context) {
|
||||
self.alter_prop(PropsOp::prepend_classes("badge"));
|
||||
self.alter_prop(PropsOp::prepend_classes(match self.kind() {
|
||||
BadgeKind::Primary => "badge badge-primary",
|
||||
BadgeKind::Secondary => "badge badge-secondary",
|
||||
BadgeKind::Success => "badge badge-success",
|
||||
BadgeKind::Info => "badge badge-info",
|
||||
BadgeKind::Warning => "badge badge-warning",
|
||||
BadgeKind::Danger => "badge badge-danger",
|
||||
}));
|
||||
}
|
||||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
|
|
@ -40,7 +70,7 @@ impl Component for Badge {
|
|||
}
|
||||
|
||||
impl Badge {
|
||||
/// Crea un badge a partir de la etiqueta indicada.
|
||||
/// Crea un badge predeterminado (`BadgeKind::default()`) con la etiqueta indicada.
|
||||
pub fn labeled(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
|
|
@ -48,6 +78,60 @@ impl Badge {
|
|||
}
|
||||
}
|
||||
|
||||
/// Crea un badge de tipo *primary* con la etiqueta indicada.
|
||||
pub fn primary(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
kind: BadgeKind::Primary,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un badge de tipo *secondary* con la etiqueta indicada.
|
||||
pub fn secondary(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
kind: BadgeKind::Secondary,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un badge de tipo *success* con la etiqueta indicada.
|
||||
pub fn success(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
kind: BadgeKind::Success,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un badge de tipo *info* con la etiqueta indicada.
|
||||
pub fn info(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
kind: BadgeKind::Info,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un badge de tipo *warning* con la etiqueta indicada.
|
||||
pub fn warning(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
kind: BadgeKind::Warning,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un badge de tipo *danger* con la etiqueta indicada.
|
||||
pub fn danger(label: Lc) -> Self {
|
||||
Self {
|
||||
label,
|
||||
kind: BadgeKind::Danger,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
// **< Badge BUILDER >**************************************************************************
|
||||
|
||||
/// Establece el identificador único del componente; igual a `with_prop(PropsOp::set_id(id))`.
|
||||
|
|
@ -70,4 +154,11 @@ impl Badge {
|
|||
self.label = label;
|
||||
self
|
||||
}
|
||||
|
||||
/// Establece el tipo del badge.
|
||||
#[builder_fn]
|
||||
pub fn with_kind(mut self, kind: BadgeKind) -> Self {
|
||||
self.kind = kind;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue