Skip to content

Commit f5732df

Browse files
committed
[tree-select-dropdown] Add :only-button part, with better positioning
For day8/dash8.day8.com.au#533 (comment)
1 parent e7e09bf commit f5732df

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/re_com/tree_select.cljs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@
5555
:level 2}
5656
{:name :dropdown-body
5757
:impl "user-defined"
58-
:level 2}])))
58+
:level 2}
59+
{:name :only-button
60+
:impl 're-com.tree-select/only-button
61+
:level 3
62+
:notes [:span "Appears when hovering a choice or group, and when "
63+
[:code ":show-only-button?"] " is true. "
64+
"When clicked, selects only the single choice or group."]}])))
5965

6066
(def tree-select-dropdown-parts
6167
(when include-args-desc?
@@ -309,8 +315,8 @@
309315
:attr (get-in parts [:offset :attr])
310316
:child (apply str (repeat level ""))])
311317

312-
(defn solo-button [{:keys [solo!]}]
313-
[:a {:href "#" :on-click solo!} "only"])
318+
(defn only-button [{:keys [solo! style class attr]}]
319+
[:a (merge {:style style :class class :href "#" :on-click solo!} attr) "only"])
314320

315321
(defn choice [{:keys [parts checked? toggle! label disabled? attr]}]
316322
[h-box
@@ -329,7 +335,7 @@
329335

330336
(defn choice-wrapper [_]
331337
(let [hover? (r/atom nil)]
332-
(fn [{:keys [choice level showing? show-only-button?] :as props}]
338+
(fn [{:keys [choice level showing? show-only-button? theme parts] :as props}]
333339
(when showing?
334340
[h-box
335341
:align :center
@@ -341,9 +347,16 @@
341347
(vec (repeat level [gap :size "10px"]))
342348
[(u/part choice
343349
{:props props
350+
:part ::choice
351+
:theme theme
344352
:impl re-com.tree-select/choice})
345353
[gap :size "1"]
346-
(when (and show-only-button? @hover?) [solo-button props])])]))))
354+
(when (and show-only-button? @hover?)
355+
(u/part (:only-button parts)
356+
{:props props
357+
:part ::only-button
358+
:theme theme
359+
:impl re-com.tree-select/only-button}))])]))))
347360

348361
(defn group-item [& {:keys [checked? hide-show! showing? open? parts] :as props}]
349362
(when showing?
@@ -361,7 +374,7 @@
361374

362375
(defn group-wrapper [_]
363376
(let [hover? (r/atom nil)]
364-
(fn [{:keys [level hide-show! parts open? showing? show-only-button?] :as props}]
377+
(fn [{:keys [level hide-show! parts open? showing? show-only-button? theme] :as props}]
365378
(when showing?
366379
[h-box
367380
:attr {:on-mouse-enter #(reset! hover? true)
@@ -385,7 +398,12 @@
385398
{:props props
386399
:impl re-com.tree-select/group-item})
387400
[gap :size "1"]
388-
(when (and show-only-button? @hover?) [solo-button props])])]))))
401+
(when (and show-only-button? @hover?)
402+
(u/part (:only-button parts)
403+
{:props props
404+
:part ::only-button
405+
:theme theme
406+
:impl re-com.tree-select/only-button}))])]))))
389407

390408
(def group? (comp #{:group} :type))
391409

@@ -504,6 +522,7 @@
504522
group-props {:group item-props
505523
:label (group-label-fn item-props)
506524
:parts parts
525+
:theme theme
507526
:hide-show! (handler-fn (on-group-expand (toggle expanded-groups group)))
508527
:toggle! (handler-fn
509528
(let [new-model (toggle-group model)]
@@ -526,6 +545,7 @@
526545
:model model
527546
:label (label-fn item-props)
528547
:parts parts
548+
:theme theme
529549
:showing? (if-not group
530550
true
531551
(every? (set expanded-groups) (ancestor-paths group)))

src/re_com/tree_select/theme.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@
4343
{:style {#_#_:margin-left "5px"
4444
#_#_:margin-right "5px"
4545
:opacity "50%"}}))
46+
47+
(defmethod base ::ts/only-button
48+
[{{{:keys [background]} :variables} :re-com :as props}]
49+
(merge-style props {:position :absolute
50+
:right 5
51+
:background background}))

src/re_demo/tree_select.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
disabled? (reagent/atom false)
3131
change-on-blur? (reagent/atom nil)
3232
show-reset-button? (reagent/atom false)
33+
show-only-button? (reagent/atom false)
3334
initial-expanded-groups (reagent/atom nil)
3435
label-fn (reagent/atom nil)
3536
group-label-fn (reagent/atom nil)
@@ -91,6 +92,7 @@
9192
:change-on-blur? @change-on-blur?
9293
:disabled? disabled?
9394
:show-reset-button? @show-reset-button?
95+
:show-only-button? @show-only-button?
9496
:label-fn @label-fn
9597
:group-label-fn @group-label-fn
9698
:choice-disabled-fn @choice-disabled-fn
@@ -131,6 +133,12 @@
131133
:child [:code ":show-reset-button?"]]
132134
:model show-reset-button?
133135
:on-change #(reset! show-reset-button? %)]
136+
[checkbox :src (at)
137+
:label [box :src (at)
138+
:align :start
139+
:child [:code ":show-only-button?"]]
140+
:model show-only-button?
141+
:on-change #(reset! show-only-button? %)]
134142
[checkbox :src (at)
135143
:label [box :src (at)
136144
:align :start

0 commit comments

Comments
 (0)