mirror of https://github.com/status-im/reagent.git
Allow dynamic id with hiccup-style classes
And id in props wins over static id. Fixes #11
This commit is contained in:
parent
68da921e35
commit
d1851d5a42
|
@ -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'])]))
|
||||
|
||||
|
|
|
@ -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"))
|
||||
|
|
Loading…
Reference in New Issue