From a79a5e1d45e9b87faed302fcf0909228969e38ba Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Sun, 19 Dec 2021 15:43:29 +0200 Subject: [PATCH] Test different ways to declare fn should be used as functional component --- src/reagent/impl/template.cljs | 19 +++++++------ test/reagenttest/testreagent.cljs | 44 ++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index 959f67e8..c1ec4c36 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -155,14 +155,6 @@ ;; https://www.w3.org/TR/custom-elements/#custom-elements-core-concepts (not= -1 (.indexOf tag "-"))))) -(defn reag-element [tag v compiler] - (let [c (comp/as-class tag compiler) - jsprops #js {}] - (set! (.-argv jsprops) v) - (when-some [key (util/react-key-from-vec v)] - (set! (.-key jsprops) key)) - (react/createElement c jsprops))) - (defn function-element [tag v first-arg compiler] (let [jsprops #js {}] (set! (.-reagentRender jsprops) tag) @@ -172,6 +164,17 @@ (set! (.-key jsprops) key)) (react/createElement (comp/functional-render-fn compiler tag) jsprops))) +(defn reag-element [^clj tag v compiler] + (if (or (:functional (meta tag)) + (.-functional tag)) + (function-element tag v 1 compiler) + (let [c (comp/as-class tag compiler) + jsprops #js {}] + (set! (.-argv jsprops) v) + (when-some [key (util/react-key-from-vec v)] + (set! (.-key jsprops) key)) + (react/createElement c jsprops)))) + (defn maybe-function-element "If given tag is a Class, use it as a class, else wrap in Reagent function wrapper." diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 26cd3bcb..a685350e 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -1604,16 +1604,58 @@ (r/flush) (is (= 3 @render)))))))) +(defn ^:functional f1 + [x] + [:span "Hello " x]) + +(defn f2 + {:functional true} + [x] + [:span "Hello " x]) + +(defn f3 + [x] + [:span "Hello " x]) +(set! (.-functional ^clj f3) true) + +; (js/console.log f1 (pr-str (meta f1)) (pr-str (meta #'f1))) +; (js/console.log f2 (pr-str (meta f2)) (pr-str (meta #'f2))) + (deftest functional-component-poc-simple (when r/is-client (let [c (fn [x] - [:span "Hello " x])] + [:span "Hello " x]) + f0 (with-meta c {:functional true})] (testing ":f>" (with-mounted-component [:f> c "foo"] (fn [c div] (is (nil? c) "Render returns nil for stateless components") (is (= "Hello foo" (.-innerText div)))))) + (testing "with-meta" + (with-mounted-component [f0 "foo"] + (fn [c div] + (is (nil? c) "Render returns nil for stateless components") + (is (= "Hello foo" (.-innerText div)))))) + + (testing "defn symbol meta" + (with-mounted-component [#'f1 "foo"] + (fn [c div] + (is (nil? c) "Render returns nil for stateless components") + (is (= "Hello foo" (.-innerText div)))))) + + (testing "defn fn meta" + (with-mounted-component [#'f2 "foo"] + (fn [c div] + (is (nil? c) "Render returns nil for stateless components") + (is (= "Hello foo" (.-innerText div)))))) + + (testing "fn property" + (with-mounted-component [f3 "foo"] + (fn [c div] + (is (nil? c) "Render returns nil for stateless components") + (is (= "Hello foo" (.-innerText div)))))) + (testing "compiler options" (with-mounted-component [c "foo"] functional-compiler