Introduce create-element

This commit is contained in:
Dan Holmsand 2014-12-02 11:43:35 +01:00
parent a48e70c4de
commit 0a8bc67d5f
2 changed files with 41 additions and 3 deletions

View File

@ -11,14 +11,26 @@
(def is-client util/is-client)
(defn create-element
"Create a native React element, by calling React.createElement directly."
([type props]
(assert (not (map? props)))
(js/React.createElement type props))
([type props child]
(assert (not (map? props)))
(js/React.createElement type props child))
([type props child & children]
(assert (not (map? props)))
(apply js/React.createElement type props child children)))
(defn as-element
"Turns a vector of Hiccup syntax into a React component. Returns form unchanged if it is not a vector."
"Turns a vector of Hiccup syntax into a React element. Returns form unchanged if it is not a vector."
[form]
(tmpl/as-element form))
(defn render
"Render a Reagent component into the DOM. The first argument may be either a
vector (using Reagent's Hiccup syntax), or a React component. The second argument should be a DOM node.
"Render a Reagent component into the DOM. The first argument may be
either a vector (using Reagent's Hiccup syntax), or a React element. The second argument should be a DOM node.
Optionally takes a callback that is called when the component is in place.

View File

@ -404,3 +404,29 @@
(is (found-in #"hi me" div))
(is (= 1 @top-ran))
(is (= 4 @ran)))))))
(defn rstr [react-elem]
(reagent/render-to-static-markup react-elem))
(deftest test-create-element
(let [ae reagent/as-element
ce reagent/create-element]
(is (= (rstr (ae [:div]))
(rstr (ce "div" nil))))
(is (= (rstr (ae [:div "foo"]))
(rstr (ce "div" nil "foo"))))
(is (= (rstr (ae [:div "foo" "bar"]))
(rstr (ce "div" nil "foo" "bar"))))
(is (= (rstr (ae [:div "foo" "bar" "foobar"]))
(rstr (ce "div" nil "foo" "bar" "foobar"))))
(is (= (rstr (ae [:div.foo "bar"]))
(rstr (ce "div" #js{:className "foo"} "bar"))))
(is (= (rstr (ae [:div [:div "foo"]]))
(rstr (ce "div" nil (ce "div" nil "foo")))))
(is (= (rstr (ae [:div [:div "foo"]]))
(rstr (ce "div" nil (ae [:div "foo"])))))
(is (= (rstr (ae [:div [:div "foo"]]))
(rstr (ae [:div (ce "div" nil "foo")]))))))