Allow "key" to be specified with meta-data

These are now equivalent
[foo {:key 1}] and
(with-meta [foo] {:key 1})
This commit is contained in:
Dan Holmsand 2014-02-11 20:56:48 +01:00
parent 4882b51b64
commit 12ee54b083
2 changed files with 14 additions and 9 deletions

View File

@ -59,7 +59,7 @@
:value @ncolors
:on-change #(reset! ncolors (-> % .-target .-value))}]])
(defn color-plate [{color :color}]
(defn color-plate [color]
[:div.color-plate
{:style {:background-color color}}])
@ -69,11 +69,12 @@
[:div
[:div
[:p "base color: "]
[color-plate {:color (to-rgb color)}]]
[color-plate (to-rgb color)]]
[:div.color-samples
[:p n " random matching colors:"]
(map-indexed (fn [k v]
[color-plate {:key k :color v}])
(with-meta [color-plate v]
{:key k}))
(take n @random-colors))]]))
(defn color-demo []

View File

@ -194,18 +194,22 @@
(set! (.-cljsReactClass tag) (wrap-component tag nil nil))
(fn-to-class tag)))))))
(defn get-key [x]
(when (map? x) (get x :key)))
(defn vec-to-comp [v level]
(assert (pos? (count v)) "Hiccup form should not be empty")
(assert (valid-tag? (v 0))
(str "Invalid Hiccup form: " (pr-str v)))
(let [props (get v 1)
c (as-class (v 0))
(let [c (as-class (v 0))
jsprops (js-obj cljs-argv v
cljs-level level)]
(when (map? props)
(let [key (:key props)]
(when-not (nil? key)
(aset jsprops "key" key))))
(let [k (-> v meta get-key)
k' (if (nil? k)
(-> v (get 1) get-key)
k)]
(when-not (nil? k')
(aset jsprops "key" k')))
(c jsprops)))
(def tmp #js {})