🚚 Renombra TypedSlot
por TypedOpt
This commit is contained in:
parent
54bed638a4
commit
476ea7de7f
4 changed files with 64 additions and 69 deletions
|
@ -8,5 +8,5 @@ pub use children::Children;
|
|||
pub use children::{Child, ChildOp};
|
||||
pub use children::{Typed, TypedOp};
|
||||
|
||||
mod slot;
|
||||
pub use slot::TypedSlot;
|
||||
mod optional;
|
||||
pub use optional::TypedOpt;
|
||||
|
|
59
src/core/component/optional.rs
Normal file
59
src/core/component/optional.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use crate::core::component::{Component, Typed};
|
||||
use crate::html::{html, Context, Markup};
|
||||
use crate::{builder_fn, AutoDefault};
|
||||
|
||||
/// Contenedor **opcional** para un componente [`Typed`].
|
||||
///
|
||||
/// Un `TypedOpt` actúa como un contenedor para incluir o no un subcomponente tipado. Internamente
|
||||
/// encapsula `Option<Typed<C>>`, pero ofrece una API más sencilla para construir estructuras
|
||||
/// jerárquicas o contenidas de componentes.
|
||||
///
|
||||
/// # Ejemplo
|
||||
///
|
||||
/// ```rust
|
||||
/// use pagetop::prelude::*;
|
||||
///
|
||||
/// let icon = Icon::default();
|
||||
/// let icon = TypedOpt::new(icon);
|
||||
/// assert!(icon.get().is_some());
|
||||
/// ```
|
||||
#[derive(AutoDefault)]
|
||||
pub struct TypedOpt<C: Component>(Option<Typed<C>>);
|
||||
|
||||
impl<C: Component> TypedOpt<C> {
|
||||
/// Crea un nuevo [`TypedOpt`].
|
||||
///
|
||||
/// El componente se envuelve automáticamente en un [`Typed`] y se almacena.
|
||||
pub fn new(component: C) -> Self {
|
||||
TypedOpt(Some(Typed::with(component)))
|
||||
}
|
||||
|
||||
// TypedOpt BUILDER ****************************************************************************
|
||||
|
||||
/// Establece un componente nuevo, o lo vacía.
|
||||
///
|
||||
/// Si se proporciona `Some(component)`, se guarda como [`Typed`]; y si es `None`, se limpia.
|
||||
#[builder_fn]
|
||||
pub fn with_component(mut self, component: Option<C>) -> Self {
|
||||
self.0 = component.map(Typed::with);
|
||||
self
|
||||
}
|
||||
|
||||
// TypedOpt GETTERS ****************************************************************************
|
||||
|
||||
/// Devuelve un clon (incrementa el contador `Arc`) de [`Typed<C>`], si existe.
|
||||
pub fn get(&self) -> Option<Typed<C>> {
|
||||
self.0.clone()
|
||||
}
|
||||
|
||||
// TypedOpt RENDER *****************************************************************************
|
||||
|
||||
/// Renderiza el componente, si existe.
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
if let Some(component) = &self.0 {
|
||||
component.render(cx)
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
use crate::builder_fn;
|
||||
use crate::core::component::{Component, Typed};
|
||||
use crate::html::{html, Context, Markup};
|
||||
|
||||
/// Contenedor para un componente [`Typed`] opcional.
|
||||
///
|
||||
/// Un `TypedSlot` actúa como un contenedor dentro de otro componente para incluir o no un
|
||||
/// subcomponente. Internamente encapsula `Option<Typed<C>>`, pero proporciona una API más sencilla
|
||||
/// para construir estructuras jerárquicas.
|
||||
///
|
||||
/// # Ejemplo
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use pagetop::prelude::*;
|
||||
///
|
||||
/// let comp = MyComponent::new();
|
||||
/// let opt = TypedSlot::new(comp);
|
||||
/// assert!(opt.get().is_some());
|
||||
/// ```
|
||||
pub struct TypedSlot<C: Component>(Option<Typed<C>>);
|
||||
|
||||
impl<C: Component> Default for TypedSlot<C> {
|
||||
fn default() -> Self {
|
||||
TypedSlot(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Component> TypedSlot<C> {
|
||||
/// Crea un nuevo [`TypedSlot`].
|
||||
///
|
||||
/// El componente se envuelve automáticamente en un [`Typed`] y se almacena.
|
||||
pub fn new(component: C) -> Self {
|
||||
TypedSlot(Some(Typed::with(component)))
|
||||
}
|
||||
|
||||
// TypedSlot BUILDER *********************************************************************
|
||||
|
||||
/// Establece un componente nuevo, o lo vacía.
|
||||
///
|
||||
/// Si se proporciona `Some(component)`, se guarda en [`Typed`]; y si es `None`, se limpia.
|
||||
#[builder_fn]
|
||||
pub fn with_value(mut self, component: Option<C>) -> Self {
|
||||
self.0 = component.map(Typed::with);
|
||||
self
|
||||
}
|
||||
|
||||
// TypedSlot GETTERS *********************************************************************
|
||||
|
||||
/// Devuelve un clon (incrementa el contador `Arc`) de [`Typed<C>`], si existe.
|
||||
pub fn get(&self) -> Option<Typed<C>> {
|
||||
self.0.clone()
|
||||
}
|
||||
|
||||
// TypedSlot RENDER ************************************************************************
|
||||
|
||||
/// Renderiza el componente, si existe.
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
if let Some(component) = &self.0 {
|
||||
component.render(cx)
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue