Skip to content

Commit

Permalink
Merge branch 'master' into spread-obj
Browse files Browse the repository at this point in the history
  • Loading branch information
lilactown committed May 2, 2020
2 parents d38e9cd + d844d23 commit bd0f8c6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/helix/impl/props.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,45 @@
#?(:cljs (doto o1
(gobj/extend o2))))

(defn seq-to-class [class]
(if (sequential? class)
(->> class
(remove nil?)
(map str)
(string/join " "))
class))

#?(:clj
(defn unquote-class
"Handle the case of (quote '[foo bar])"
[class]
(cond
(string? class)
class

(and (list? class)
(= (first class) 'quote))
(-> class
second
seq-to-class
str)

:default
`(normalize-class ~class))))

#?(:clj
(defn normalize-class [class]
(-> class
unquote-class)))

#?(:cljs
(defn normalize-class [class]
(if (string? class)
;; quick path
class
(-> class
seq-to-class
str))))

(defn -native-props
([m] #?(:clj (if-let [spread-sym (cond
Expand All @@ -92,7 +131,7 @@
k (key entry)
v (val entry)]
(case k
:class (set-obj o "className" v)
:class (set-obj o "className" (normalize-class v))
:for (set-obj o "htmlFor" v)
:style (set-obj o "style"
(if (vector? v)
Expand Down
29 changes: 29 additions & 0 deletions test/helix/impl/props_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,32 @@
(t/is (eq (impl/props {:foo "bar"
& #js {:baz "asdf"}})
#js {:foo "bar" :baz "asdf"}))))


(t/deftest test-normalize-class
#?(:clj
(do
(t/testing "macro expansion - string value shall be kept as is"
(t/is (= (impl/normalize-class "foo")
"foo")))
(t/testing "macro expansion - quoted forms shall be converted to string"
(t/is (= (impl/normalize-class (quote '[foo bar]))
"foo bar"))
(t/is (= (impl/normalize-class (quote 'bar))
"bar")))
(t/testing "macro expansion - other value shall be passed to runtime check"
(t/is (= (impl/normalize-class 'foo)
'(helix.impl.props/normalize-class foo)))
(t/is (= (impl/normalize-class '[foo bar])
'(helix.impl.props/normalize-class [foo bar])))
(t/is (= (impl/normalize-class '(vector foo bar))
'(helix.impl.props/normalize-class (vector foo bar)))))))
#?(:cljs
(do (t/testing "runtime - all shall be converted to string"
(t/is (= (impl/normalize-class 'foo)
"foo"))
(t/is (= (impl/normalize-class '[foo bar])
"foo bar")))
(t/testing "runtime - nil shall be filtered out"
(t/is (= (impl/normalize-class ["foo" nil])
"foo"))))))

0 comments on commit bd0f8c6

Please sign in to comment.