From 5045046f6a2e53961bc1a0c5f5e7022c294f8aad Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Thu, 2 Jul 2015 18:12:48 +0200 Subject: [PATCH 1/2] Add support for collections in :class property --- README.md | 6 ++++++ src/reagent/impl/template.cljs | 10 +++++++++- test/reagenttest/testreagent.cljs | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 11f98c9..0a25ee4 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,12 @@ can be written as: [:div>p>b "Nested Element"] ``` +The `:class` attribute accepts collections in addition to plain string. + +```clj +[:div {:class ["a-class" (when active? "active") "b-class"]}] +``` + You can use one component inside another: ```clj diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index 97b9108..c24deb0 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -91,12 +91,20 @@ (str class " " old)))) p))) +(defn stringify-class [{:keys [class] :as props}] + (if (coll? class) + (->> class + (filter identity) + (string/join " ") + (assoc props :class)) + props)) + (defn convert-props [props id-class] (-> props + stringify-class convert-prop-value (set-id-class id-class))) - ;;; Specialization for input components ;; This gets set from reagent.dom diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 3199fc7..67a8c8d 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -573,6 +573,16 @@ (is (= (rstr [:div>p.bar.foo>a.foobar {:href "href"} "xy"]) (rstr [:div [:p.bar.foo [:a.foobar {:href "href"} "xy"]]])))) +(deftest test-class-from-collection + (is (= (rstr [:p {:class ["a" "b" "c" "d"]}]) + (rstr [:p {:class "a b c d"}]))) + (is (= (rstr [:p {:class ["a" nil "b" false "c" nil]}]) + (rstr [:p {:class "a b c"}]))) + (is (= (rstr [:p {:class '("a" "b" "c")}]) + (rstr [:p {:class "a b c"}]))) + (is (= (rstr [:p {:class #{"a" "b" "c"}}]) + (rstr [:p {:class "a b c"}])))) + (deftest test-force-update (let [v (atom {:v1 0 :v2 0}) From cc731e8a77294911a9de8338ea007be86edc64ba Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Fri, 20 Oct 2017 08:41:55 +0200 Subject: [PATCH 2/2] Improve language in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a25ee4..9d6476b 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ can be written as: [:div>p>b "Nested Element"] ``` -The `:class` attribute accepts collections in addition to plain string. +The `:class` attribute can accept either a collection or a string. ```clj [:div {:class ["a-class" (when active? "active") "b-class"]}]