Allow dynamic id with hiccup-style classes

And id in props wins over static id.

Fixes #11
This commit is contained in:
Dan Holmsand 2014-01-28 18:00:15 +01:00
parent 68da921e35
commit d1851d5a42
2 changed files with 23 additions and 4 deletions

View File

@ -44,7 +44,7 @@
:else (clj->js val)))
(defn set-id-class [props [id class]]
(aset props "id" id)
(aset props "id" (or (aget props "id") id))
(when class
(aset props "className" (if-let [old (aget props "className")]
(str class " " old)
@ -104,12 +104,12 @@
(def DOM (aget React "DOM"))
(defn parse-tag [tag]
(let [[tag id class] (->> tag name (re-matches re-tag) next)
(defn parse-tag [hiccup-tag]
(let [[tag id class] (->> hiccup-tag name (re-matches re-tag) next)
comp (aget DOM tag)
class' (when class
(string/replace class #"\." " "))]
(assert comp (str "Unknown tag: " tag))
(assert comp (str "Unknown tag: '" hiccup-tag "'"))
[comp (when (or id class')
[id class'])]))

View File

@ -155,3 +155,22 @@
(is (re-find #"enctype"
(as-string [:div {:enc-type "x"}]))
"Strings are passed through to React, and have to be camelcase."))
(deftest dynamic-id-class []
(is (re-find #"id=.foo"
(as-string [:div#foo {:class "bar"}])))
(is (re-find #"class=.foo bar"
(as-string [:div.foo {:class "bar"}])))
(is (re-find #"class=.foo bar"
(as-string [:div.foo.bar])))
(is (re-find #"id=.foo"
(as-string [:div#foo.foo.bar])))
(is (re-find #"class=.xxx bar"
(as-string [:div#foo.xxx.bar])))
(is (re-find #"id=.foo"
(as-string [:div.bar {:id "foo"}])))
(is (re-find #"id=.foo"
(as-string [:div.bar.xxx {:id "foo"}])))
(is (re-find #"id=.foo"
(as-string [:div#bar {:id "foo"}]))
"Dynamic id overwrites static"))