diff --git a/project.clj b/project.clj index aaa6f4e..10f06a3 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject reagent "0.5.1-rc" +(defproject reagent "0.5.1-SNAPSHOT" :url "http://github.com/reagent-project/reagent" :license {:name "MIT"} :description "A simple ClojureScript interface to React" diff --git a/src/reagent/core.cljs b/src/reagent/core.cljs index a00db6b..3e40c56 100644 --- a/src/reagent/core.cljs +++ b/src/reagent/core.cljs @@ -47,6 +47,7 @@ which is equivalent to "Returns an adapter for a native React class, that may be used just like a Reagent component function or class in Hiccup forms." [c] + (assert c) (tmpl/adapt-react-class c)) (defn reactify-component @@ -54,6 +55,7 @@ just like a Reagent component function or class in Hiccup forms." React, for example in JSX. A single argument, props, is passed to the component, converted to a map." [c] + (assert c) (comp/reactify-component c)) (defn render @@ -286,3 +288,9 @@ the result can be compared with =" [f & args] (util/partial-ifn. f args nil)) +(defn component-path + ;; Try to return the path of component c as a string. + ;; Maybe useful for debugging and error reporting, but may break + ;; with future versions of React (and return nil). + [c] + (comp/component-path c)) diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 8af5c89..616e9b1 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -188,7 +188,8 @@ name (str (or (:displayName fun-map) (fun-name render-fun))) name' (if (empty? name) - (str (gensym "reagent")) name) + (str (gensym "reagent")) + (clojure.string/replace name #"\$" ".")) fmap (-> fun-map (assoc :displayName name') (add-render render-fun name'))] @@ -223,10 +224,26 @@ (util/cache-react-class res res) f)) +(defn component-path [c] + (let [elem (some-> (or (some-> c + (.' :_reactInternalInstance)) + c) + (.' :_currentElement)) + name (some-> elem + (.' :type) + (.' :displayName)) + path (some-> elem + (.' :_owner) + component-path + (str " > ")) + res (str path name)] + (when-not (empty? res) res))) + (defn comp-name [] (if (dev?) - (let [n (some-> *current-component* - (.' cljsName))] + (let [c *current-component* + n (or (component-path c) + (some-> c (.' cljsName)))] (if-not (empty? n) (str " (in " n ")") "")) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index ec81bae..b51a1c2 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -540,3 +540,15 @@ (r/force-update (:c2 @comps) true) (is (= @v {:v1 3 :v2 3})))))) + +(deftest test-component-path + (let [a (atom nil) + tc (r/create-class {:display-name "atestcomponent" + :render (fn [] + (let [c (r/current-component)] + (reset! a (r/component-path c)) + [:div]))})] + (with-mounted-component [tc] + (fn [c] + (is (seq @a)) + (is (re-find #"atestcomponent" @a) "component-path should work")))))