Move selection behaviour and option list population to dropdown component

This commit is contained in:
Vitaliy Vlasov 2018-01-29 20:18:16 +02:00
parent c5fdad5b8e
commit a52f5f2b20
2 changed files with 21 additions and 15 deletions

View File

@ -10,19 +10,26 @@
(merge props {:type "text" (merge props {:type "text"
:value @val-ratom :value @val-ratom
:on-change #(reset! val-ratom (-> % .-target .-value))})])) :on-change #(reset! val-ratom (-> % .-target .-value))})]))
(defn dropdown [props title val-ratom items] (defn dropdown [props title val-ratom items]
(fn [] "If val-ratom is set, preselect it in the dropdown.
[:select.ui.basic.selection.dropdown Add value of val-ratom if it's missing from items list.
(merge props {:on-change Otherwise, prepend title as a disabled option"
#(let [selected-value (-> % .-target .-value)] (let [items (cond-> items
(when (not= selected-value title) (and @val-ratom
(reset! val-ratom selected-value))) (not (contains? (set items) @val-ratom)))
:default-value (if (contains? (set items) @val-ratom) (conj @val-ratom)
@val-ratom (not @val-ratom)
title)}) (conj title))]
(conj (doall (for [item items] (fn []
^{:key item} [:option {:value item} item])) [:select.ui.basic.selection.dropdown
^{:key title} [:option {:value title :disabled true} title])])) (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] (defn moment-timestamp [time]
(let [now (.now js/Date.) (let [now (.now js/Date.)

View File

@ -11,9 +11,8 @@
(let [db (rf/subscribe [:db]) (let [db (rf/subscribe [:db])
user (rf/subscribe [:user]) user (rf/subscribe [:user])
updating-address (rf/subscribe [:get-in [:updating-address]]) updating-address (rf/subscribe [:get-in [:updating-address]])
address (-> @(rf/subscribe [:get-in [:user :address]]) address (r/atom (some-> @(rf/subscribe [:get-in [:user :address]])
str/lower-case str/lower-case))]
r/atom)]
(fn [] (fn []
(let [web3 (:web3 @db) (let [web3 (:web3 @db)
web3-accounts (when web3 web3-accounts (when web3