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

View File

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