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"
: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.)

View File

@ -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