Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for React.Fragment #907

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/om/dom.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,16 @@
:attrs (dissoc opts :ref :key)
:react-key (:key opts)
:children children}))))

#?(:clj
(defn- is-element?
[e]
(or
(satisfies? p/IReactComponent e)
(satisfies? p/IReactDOMElement e))))

#?(:clj
(defn <>
[& args]
(let [children (if (is-element? (first args)) args (rest args))]
(vec children))))
7 changes: 7 additions & 0 deletions src/main/om/dom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@
(js/React.createElement tag opts))
([tag opts & children]
(js/React.createElement tag opts children)))

(defn <>
"Create a `React.Fragment` with either child or children using `React.createElement`"
([opts child]
(<> opts child nil))
([opts child & more]
(apply js/React.createElement js/React.Fragment opts child more)))
8 changes: 8 additions & 0 deletions src/test/om/dom_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@
</div>
</div>"))))

(deftest render-react-fragment
(is (= (str (#'dom/render-to-str*
(dom/div #js {}
(dom/<> #js {}
(dom/input #js {:value "foo" :id "bar" :type "text"})
(dom/input #js {:value "foo" :id "bar" :type "text"})))))
"<div data-reactroot=\"\" data-reactid=\"1\"><input type=\"text\" value=\"foo\" id=\"bar\" data-reactid=\"2\"/><input type=\"text\" value=\"foo\" id=\"bar\" data-reactid=\"3\"/></div>")))

(deftest render-wrapped-attrs
(is (= (str (#'dom/render-to-str* (dom/input #js {:value "foo" :id "bar" :type "text"})))
"<input type=\"text\" value=\"foo\" id=\"bar\" data-reactroot=\"\" data-reactid=\"1\"/>"))
Expand Down