From a52f5f2b20e446670c414f033979457528df65ca Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Mon, 29 Jan 2018 20:18:16 +0200 Subject: [PATCH] Move selection behaviour and option list population to dropdown component --- src/cljs/commiteth/common.cljs | 31 ++++++++++++++++---------- src/cljs/commiteth/update_address.cljs | 5 ++--- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/cljs/commiteth/common.cljs b/src/cljs/commiteth/common.cljs index bc44638..a613925 100644 --- a/src/cljs/commiteth/common.cljs +++ b/src/cljs/commiteth/common.cljs @@ -10,19 +10,26 @@ (merge props {:type "text" :value @val-ratom :on-change #(reset! val-ratom (-> % .-target .-value))})])) + (defn dropdown [props title val-ratom items] - (fn [] - [:select.ui.basic.selection.dropdown - (merge props {:on-change - #(let [selected-value (-> % .-target .-value)] - (when (not= selected-value title) - (reset! val-ratom selected-value))) - :default-value (if (contains? (set items) @val-ratom) - @val-ratom - title)}) - (conj (doall (for [item items] - ^{:key item} [:option {:value item} item])) - ^{:key title} [:option {:value title :disabled true} title])])) + "If val-ratom is set, preselect it in the dropdown. + Add value of val-ratom if it's missing from items list. + Otherwise, prepend title as a disabled option" + (let [items (cond-> items + (and @val-ratom + (not (contains? (set items) @val-ratom))) + (conj @val-ratom) + (not @val-ratom) + (conj title))] + (fn [] + [:select.ui.basic.selection.dropdown + (merge props {:on-change + #(reset! val-ratom (-> % .-target .-value)) + :default-value (or @val-ratom title)}) + (doall (for [item items] + ^{:key item} [:option {:value item + :disabled (= item title)} + item]))]))) (defn moment-timestamp [time] (let [now (.now js/Date.) diff --git a/src/cljs/commiteth/update_address.cljs b/src/cljs/commiteth/update_address.cljs index 41364c6..9261671 100644 --- a/src/cljs/commiteth/update_address.cljs +++ b/src/cljs/commiteth/update_address.cljs @@ -11,9 +11,8 @@ (let [db (rf/subscribe [:db]) user (rf/subscribe [:user]) updating-address (rf/subscribe [:get-in [:updating-address]]) - address (-> @(rf/subscribe [:get-in [:user :address]]) - str/lower-case - r/atom)] + address (r/atom (some-> @(rf/subscribe [:get-in [:user :address]]) + str/lower-case))] (fn [] (let [web3 (:web3 @db) web3-accounts (when web3