Skip to content

Commit

Permalink
Merge pull request #811 from anmonteiro/om-799
Browse files Browse the repository at this point in the history
OM-799: Make a new public function, `dom/create-element`
  • Loading branch information
swannodette authored Nov 15, 2016
2 parents f444a11 + cb839c0 commit f9dc813
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/devcards/om/devcards/bugs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,12 @@
(fn [_ node]
(om/add-root! om-779-reconciler OM-779-Root node))))

(defcard om-799-card
(dom/svg #js {:viewBox "0 0 400 100"}
(dom/rect #js {:id "rectangle" :x "100" :y "20"
:width "50" :height "50" :fill "#29e"})
(dom/create-element "use" #js {:xlinkHref "#rectangle"
:x "150"})))

(comment

Expand Down
13 changes: 13 additions & 0 deletions src/main/om/dom.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,16 @@
([component name]
{:pre [(satisfies? p/IReactComponent component)]}
(some-> @(p/-refs component) (get name) p/-render))))

#?(:clj
(defn create-element
"Create a DOM element for which there exists no corresponding function.
Useful to create DOM elements not included in React.DOM. Equivalent
to calling `js/React.createElement`"
([tag]
(create-element tag nil))
([tag opts & children]
(element {:tag tag
:attrs (dissoc opts :ref :key)
:react-key (:key opts)
:children children}))))
9 changes: 9 additions & 0 deletions src/main/om/dom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@
(js/ReactDOM.findDOMNode component))
([component name]
(some-> (.-refs component) (gobj/get name) (js/ReactDOM.findDOMNode))))

(defn create-element
"Create a DOM element for which there exists no corresponding function.
Useful to create DOM elements not included in React.DOM. Equivalent
to calling `js/React.createElement`"
([tag]
(create-element tag nil))
([tag opts & children]
(js/React.createElement tag opts children)))
18 changes: 18 additions & 0 deletions src/test/om/dom_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,21 @@
:dangerouslySetInnerHTML {:__html "'foo bar'"}})
(volatile! 0) sb)
(is (= (str sb) "<script type=\"text/javascript\" data-reactid=\"0\">'foo bar'</script>"))))

(deftest test-om-799
(let [elem (dom/create-element "use")
sb (StringBuilder.)]
(is (instance? om.dom.Element elem))
(p/-render-to-string elem (volatile! 0) sb)
(is (= (str sb) "<use data-reactid=\"0\"></use>")))
(let [sb (StringBuilder.)]
(p/-render-to-string (dom/create-element "use" nil "use-child") (volatile! 0) sb)
(is (= (str sb) "<use data-reactid=\"0\">use-child</use>")))
(let [elem (dom/create-element "use"
#js {:key "foo"
:x "50" :y "10"
:xlinkHref "#Port"}
"use-child")
sb (StringBuilder.)]
(p/-render-to-string elem (volatile! 0) sb)
(is (= (str sb) "<use x=\"50\" y=\"10\" xlink:href=\"#Port\" data-reactid=\"0\">use-child</use>"))))

0 comments on commit f9dc813

Please sign in to comment.