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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use wasm_bindgen::{JsCast, JsValue};

use crate::IntoElement;

#[derive(Debug, Clone)]
pub struct FragmentElement {
    pub key: Option<crate::Key>,
    pub children: Option<crate::Children>,
}

impl FragmentElement {
    #[inline]
    pub(crate) fn unsafe_create_element_js(self) -> react_sys::Element {
        let mut props = None;

        if let Some(key) = self.key {
            let obj = props.get_or_insert_with(|| js_sys::Object::new());
            let obj: &crate::JsProps = obj.unchecked_ref();
            obj.set_key(Some(key.as_ref()));
        }

        const NULL: &JsValue = &JsValue::NULL;
        let props = props.as_ref().map_or(NULL, AsRef::as_ref);

        let el = if let Some(children) = self.children {
            react_sys::create_element(
                &react_sys::Fragment,
                props,
                &children.unsafe_into_js_array(),
            )
        } else {
            react_sys::create_element_no_children(&react_sys::Fragment, props)
        };

        el
    }
}

impl IntoElement for FragmentElement {
    #[inline]
    fn into_element(self) -> crate::Element {
        crate::Element::fragment(self)
    }
}

impl Into<crate::Element> for FragmentElement {
    #[inline]
    fn into(self) -> crate::Element {
        self.into_element()
    }
}

impl crate::Node for FragmentElement {
    #[inline]
    fn to_node(&self) -> crate::AnyNode {
        self.clone().into_node()
    }

    #[inline]
    fn to_children(&self) -> Option<crate::Children> {
        self.clone().into_children()
    }

    #[inline]
    fn into_node(self) -> crate::AnyNode {
        crate::AnyNode::Element(self.into_element())
    }

    #[inline]
    fn into_children(self) -> Option<crate::Children> {
        Some(crate::Children::from_single(self.into_node()))
    }
}