added impl IntoNode for most types IntoChild supported

This commit is contained in:
Jose Quesada 2022-11-25 14:37:47 -06:00
parent fbfdb9fd15
commit 24b1fc01ca
5 changed files with 109 additions and 1 deletions

View file

@ -1,7 +1,11 @@
mod dyn_child;
mod dyn_text;
mod each;
mod fragment;
mod unit;
pub use dyn_child::*;
pub use dyn_text::*;
pub use each::*;
pub use fragment::*;
pub use unit::*;

View file

@ -0,0 +1,30 @@
use std::marker::PhantomData;
use leptos_reactive::Scope;
// use crate::IntoNode;
// #[derive(typed_builder::TypedBuilder)]
// struct EachProps {}
// #[allow(non_snake_case)]
// /// ```html
// /// <ul>
// /// <!-- <Each> -->
// /// <!-- <Item> -->
// /// <li>1</li>
// /// <!-- </Item> -->
// /// <!-- <Item> -->
// /// <li>2</li>
// /// <!-- </Item> -->
// /// <!-- </Each> -->
// /// </ul>
// /// ```
// struct Each<I, T, IF>
// where
// for<'a> &'a I: IntoIterator<Item = T>,
// T: Eq,
// IF: Fn(&T) ->
// {
// items: I,
// }

View file

@ -0,0 +1,21 @@
use crate::{Component, IntoNode, Node};
/// Represents a group of [`Nodes`](Node).
pub struct Fragment(Vec<Node>);
impl Fragment {
/// Creates a new [`Fragment`] from a [`Vec<Node>`].
pub fn new(nodes: Vec<Node>) -> Self {
Self(nodes)
}
}
impl IntoNode for Fragment {
fn into_node(self, cx: leptos_reactive::Scope) -> Node {
let frag = Component::new("Fragment");
*frag.children.borrow_mut() = self.0;
Node::Component(frag)
}
}

View file

@ -1,6 +1,6 @@
use crate::{
components::{DynChild, DynText},
mount_child, Element, IntoNode, Node, Text,
mount_child, Element, Fragment, IntoNode, Node, Text,
};
use leptos_reactive::Scope;
@ -136,6 +136,12 @@ impl<El: IntoElement> IntoNode for HtmlElement<El> {
}
}
impl<El: IntoElement> IntoNode for Vec<HtmlElement<El>> {
fn into_node(self, cx: Scope) -> Node {
Fragment::new(self.into_iter().map(|el| el.into_node(cx)).collect()).into_node(cx)
}
}
/// Creates any custom element, such as `<my-element>`.
pub fn custom<El: IntoElement>(name: &str) -> HtmlElement<Custom> {
HtmlElement::new(Custom {

View file

@ -23,6 +23,29 @@ impl IntoNode for () {
}
}
impl<T> IntoNode for Option<T>
where
T: IntoNode,
{
fn into_node(self, cx: Scope) -> Node {
if let Some(t) = self {
t.into_node(cx)
} else {
Unit.into_node(cx)
}
}
}
impl<F, N> IntoNode for F
where
F: Fn() -> N + 'static,
N: IntoNode,
{
fn into_node(self, cx: Scope) -> Node {
DynChild::new(self).into_node(cx)
}
}
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", feature = "web"))] {
#[derive(Clone, educe::Educe)]
@ -59,6 +82,12 @@ pub struct Element {
children: Vec<Node>,
}
impl IntoNode for Element {
fn into_node(self, _: Scope) -> Node {
Node::Element(self)
}
}
impl Element {
#[track_caller]
fn new<El: IntoElement>(el: El) -> Self {
@ -117,6 +146,12 @@ pub struct Text {
content: String,
}
impl IntoNode for Text {
fn into_node(self, _: Scope) -> Node {
Node::Text(self)
}
}
impl Text {
/// Creates a new [`Text`].
pub fn new(content: &str) -> Self {
@ -147,6 +182,12 @@ pub struct Component {
closing: Comment,
}
impl IntoNode for Component {
fn into_node(self, _: Scope) -> Node {
Node::Component(self)
}
}
impl Component {
/// Creates a new [`Component`].
pub fn new(name: &str) -> Self {
@ -201,6 +242,12 @@ impl IntoNode for Node {
}
}
impl IntoNode for Vec<Node> {
fn into_node(self, cx: Scope) -> Node {
Fragment::new(self).into_node(cx)
}
}
impl Node {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
fn get_web_sys_node(&self) -> web_sys::Node {