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

Test different ways to declare fn should be used as functional component #555

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/reagent/impl/template.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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."
Expand Down
44 changes: 43 additions & 1 deletion test/reagenttest/testreagent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down