react/element/
fragment.rs

1use wasm_bindgen::{JsCast, JsValue};
2
3use crate::IntoElement;
4
5#[derive(Debug, Clone)]
6pub struct FragmentElement {
7    pub key: Option<crate::Key>,
8    pub children: Option<crate::Children>,
9}
10
11impl FragmentElement {
12    #[inline]
13    pub(crate) fn unsafe_create_element_js(self) -> react_sys::Element {
14        let mut props = None;
15
16        if let Some(key) = self.key {
17            let obj = props.get_or_insert_with(|| js_sys::Object::new());
18            let obj: &crate::JsProps = obj.unchecked_ref();
19            obj.set_key(Some(key.as_ref()));
20        }
21
22        const NULL: &JsValue = &JsValue::NULL;
23        let props = props.as_ref().map_or(NULL, AsRef::as_ref);
24
25        let el = if let Some(children) = self.children {
26            react_sys::create_element(
27                &react_sys::Fragment,
28                props,
29                &children.unsafe_into_js_array(),
30            )
31        } else {
32            react_sys::create_element_no_children(&react_sys::Fragment, props)
33        };
34
35        el
36    }
37}
38
39impl IntoElement for FragmentElement {
40    #[inline]
41    fn into_element(self) -> crate::Element {
42        crate::Element::fragment(self)
43    }
44}
45
46impl Into<crate::Element> for FragmentElement {
47    #[inline]
48    fn into(self) -> crate::Element {
49        self.into_element()
50    }
51}
52
53impl crate::Node for FragmentElement {
54    #[inline]
55    fn to_node(&self) -> crate::AnyNode {
56        self.clone().into_node()
57    }
58
59    #[inline]
60    fn to_children(&self) -> Option<crate::Children> {
61        self.clone().into_children()
62    }
63
64    #[inline]
65    fn into_node(self) -> crate::AnyNode {
66        crate::AnyNode::Element(self.into_element())
67    }
68
69    #[inline]
70    fn into_children(self) -> Option<crate::Children> {
71        Some(crate::Children::from_single(self.into_node()))
72    }
73}