From 12ee54b083276196c0220154c12a09b69917ad21 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Tue, 11 Feb 2014 20:56:48 +0100 Subject: [PATCH] Allow "key" to be specified with meta-data These are now equivalent [foo {:key 1}] and (with-meta [foo] {:key 1}) --- demo/reagentdemo/news/async.cljs | 7 ++++--- src/reagent/impl/template.cljs | 16 ++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/demo/reagentdemo/news/async.cljs b/demo/reagentdemo/news/async.cljs index 2c0a735..c24daf0 100644 --- a/demo/reagentdemo/news/async.cljs +++ b/demo/reagentdemo/news/async.cljs @@ -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 [] diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index c618e48..07a0266 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -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 {})