1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::collections::HashMap;

use itertools::Itertools;

use super::{
    define_el_function_for_vec_element_newtype, Element, ElementComponent, ElementComponentExt,
    Hooks,
};

/// Wraps the inner [Element].
/// This is useful for introducing an intermediate component node in a tree.
#[derive(Debug, Clone)]
pub struct Wrap(pub Element);
impl ElementComponent for Wrap {
    fn render(self: Box<Self>, _hooks: &mut Hooks) -> Element {
        self.0
    }
}

#[derive(Debug, Clone)]
/// Wrap multiple [Element]s in a flat hierarchy.
pub struct Group(pub Vec<Element>);
define_el_function_for_vec_element_newtype!(Group);
impl ElementComponent for Group {
    fn render(self: Box<Self>, _hooks: &mut Hooks) -> Element {
        Element::new().children(self.0)
    }
}
impl<T: ElementComponent + Clone + 'static> ElementComponent for Vec<T> {
    fn render(self: Box<Self>, _: &mut Hooks) -> Element {
        Group(
            self.into_iter()
                .map(|part| Element::from(part))
                .collect_vec(),
        )
        .into()
    }
}
impl<T: ElementComponent + Clone + 'static> ElementComponent for HashMap<String, T> {
    fn render(self: Box<Self>, _: &mut Hooks) -> Element {
        Group(
            self.into_iter()
                .sorted_by_key(|x| x.0.clone())
                .map(|(key, part)| Element::from(part).key(key))
                .collect_vec(),
        )
        .into()
    }
}

#[derive(Debug, Clone)]
/// Memoize the [ElementComponent], such that it is only re-rendered if the component changes.
pub struct Memo<P: ElementComponent + PartialEq + Clone + 'static>(pub P);
impl<P: ElementComponent + PartialEq + Clone + 'static> ElementComponent for Memo<P> {
    fn render(self: Box<Self>, _hooks: &mut Hooks) -> Element {
        let key = format!("{:?}", self.0);
        Element::from(self.0).memoize_subtree(key)
    }
}