added IntoFragment trait for all IntoIterator and also impl FromIterator for Fragment

This commit is contained in:
Jose Quesada 2022-12-12 13:39:41 -06:00
parent 0b11a8dda6
commit e2ef293d19
2 changed files with 31 additions and 1 deletions

View file

@ -1,9 +1,39 @@
use leptos_reactive::Scope;
use crate::{ComponentRepr, IntoView, View};
/// Trait for converting any iterable into a [`Fragment`].
pub trait IntoFragment {
/// Consumes this type, returning [`Fragment`].
fn into_fragment(self, cx: Scope) -> Fragment;
}
impl<I, V> IntoFragment for I
where
I: IntoIterator<Item = V>,
V: IntoView,
{
fn into_fragment(self, cx: Scope) -> Fragment {
self.into_iter().map(|v| v.into_view(cx)).collect()
}
}
/// Represents a group of [`views`](View).
#[derive(Debug, Clone)]
pub struct Fragment(Vec<View>);
impl FromIterator<View> for Fragment {
fn from_iter<T: IntoIterator<Item = View>>(iter: T) -> Self {
Fragment::new(iter.into_iter().collect())
}
}
impl From<View> for Fragment {
fn from(view: View) -> Self {
Fragment::new(vec![view])
}
}
impl Fragment {
/// Creates a new [`Fragment`] from a [`Vec<Node>`].
pub fn new(nodes: Vec<View>) -> Self {

View file

@ -24,8 +24,8 @@ pub use components::*;
pub use events::typed as ev;
pub use helpers::*;
pub use html::*;
pub use js_sys;
use hydration::HydrationCtx;
pub use js_sys;
use leptos_reactive::Scope;
pub use logging::*;
pub use node_ref::*;