From e1019a1c9c5104b500d0b42bf724eeaaa519c259 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 4 May 2018 16:10:41 +0300 Subject: [PATCH] Check if value is named before calling name --- src/reagent/impl/template.cljs | 12 ++++++++---- test/reagenttest/testreagent.cljs | 29 +++++++++++++++++------------ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index 214830c..416f27d 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -122,14 +122,18 @@ ;; Merge classes class (assoc :class (let [old-class (:class props)] - (if (nil? old-class) class (str class " " (name old-class)))))))) + (if (nil? old-class) class (str class " " (if (named? old-class) + (name old-class) + old-class)))))))) (defn stringify-class [{:keys [class] :as props}] - ;; (keep name) doesn't work because class vector could contain false, which is not Named (if (coll? class) (->> class - (filter identity) - (map name) + (keep (fn [c] + (if c + (if (named? c) + (name c) + c)))) (string/join " ") (assoc props :class)) props)) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index e1b8736..d965935 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -574,19 +574,24 @@ (is (= (rstr [:p {:class #{"a" "b" "c"}}]) (rstr [:p {:class "a b c"}])))) -(deftest class-named-values - (is (= (rstr [:p {:class :a}]) - (rstr [:p {:class "a"}]))) - (is (= (rstr [:p.a {:class :b}]) - (rstr [:p {:class "a b"}]))) - (is (= (rstr [:p.a {:class 'b}]) - (rstr [:p {:class "a b"}]))) - (is (= (rstr [:p {:class [:a :b]}]) - (rstr [:p {:class "a b"}]))) - (is (= (rstr [:p {:class ['a :b]}]) - (rstr [:p {:class "a b"}]))) +(deftest class-different-types + (testing "named values are supported" + (is (= (rstr [:p {:class :a}]) + (rstr [:p {:class "a"}]))) + (is (= (rstr [:p.a {:class :b}]) + (rstr [:p {:class "a b"}]))) + (is (= (rstr [:p.a {:class 'b}]) + (rstr [:p {:class "a b"}]))) + (is (= (rstr [:p {:class [:a :b]}]) + (rstr [:p {:class "a b"}]))) + (is (= (rstr [:p {:class ['a :b]}]) + (rstr [:p {:class "a b"}])))) - (testing "class collection can contain false value" + (testing "non-named values like numbers" + (is (= (rstr [:p {:class [1 :b]}]) + (rstr [:p {:class "1 b"}])))) + + (testing "falsey values are filtered from collections" (is (= (rstr [:p {:class [:a :b false nil]}]) (rstr [:p {:class "a b"}])))) )