Skip to content

Commit 7a55fbb

Browse files
committed
[tree-select] Add :required? prop
For day8/rf8.day8.com.au#132
1 parent 529f94a commit 7a55fbb

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/re_com/tree_select.cljs

+24-11
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,18 @@
130130
:required false
131131
:default false
132132
:type "boolean"
133-
:description "if true, no user selection is allowed"}
133+
:description "When true, no user selection is allowed"}
134+
{:name :required?
135+
:default false
136+
:type "boolean"
137+
:description [:span "When true, requires at least 1 choice to be selected. "
138+
"If clicking a choice would result in 0 choices being selected (i.e. a model value of #{}), "
139+
"then " [:code ":on-change"] " will not be called."]}
134140
{:name :groups-first?
135141
:required false
136142
:default false
137143
:type "boolean"
138-
:description "If true, puts groups at the top of the list. Ungrouped items will appear last."}
144+
:description "When true, puts groups at the top of the list. Ungrouped items will appear last."}
139145
{:name :choice-disabled-fn
140146
:required false
141147
:default nil
@@ -412,7 +418,7 @@
412418
min-width max-width min-height max-height
413419
on-change choice-disabled-fn label-fn
414420
choice on-group-expand
415-
empty-means-full?
421+
empty-means-full? required?
416422
parts class style attr]
417423
:as args}]
418424
(or
@@ -437,16 +443,21 @@
437443
full? :all
438444
(every? model descendant-ids) :all
439445
(some model descendant-ids) :some)
440-
new-model (->> (cond->> descendants choice-disabled-fn (remove choice-disabled-fn))
441-
(map id-fn)
442-
((if (= :all checked?) set/difference set/union) model)
443-
set)
444-
new-groups (into #{} (map :group) (full-groups new-model choices {:id-fn id-fn}))
446+
toggle-group #(->> (cond->> descendants choice-disabled-fn (remove choice-disabled-fn))
447+
(map id-fn)
448+
((if (= :all checked?) set/difference set/union) %)
449+
set)
450+
new-groups (into #{} (map :group) (full-groups (toggle-group model)
451+
choices
452+
{:id-fn id-fn}))
445453
group-props {:group item-props
446454
:label (group-label-fn item-props)
447455
:parts parts
448456
:hide-show! (handler-fn (on-group-expand (toggle expanded-groups group)))
449-
:toggle! (handler-fn (on-change new-model new-groups))
457+
:toggle! (handler-fn
458+
(let [new-model (toggle-group model)]
459+
(when (or (not required?) (seq new-model))
460+
(on-change new-model new-groups))))
450461
:solo! (handler-fn (let [new-model (set descendant-ids)
451462
new-groups (into #{} (map :group) (full-groups new-model choices {:id-fn id-fn}))]
452463
(on-change new-model new-groups)))
@@ -472,7 +483,8 @@
472483
(on-change new-model new-groups)))
473484
:toggle! (handler-fn (let [new-model (toggle model (id-fn item-props))
474485
new-groups (into #{} (map :group) (full-groups new-model choices {:id-fn id-fn}))]
475-
(on-change new-model new-groups)))
486+
(when (or (not required?) (seq new-model))
487+
(on-change new-model new-groups))))
476488
:checked? (or full? (get model (id-fn item-props)))
477489
:level level}]
478490
[choice-wrapper choice-props]))))]
@@ -517,7 +529,7 @@
517529
(let [showing? (r/atom false)
518530
expanded-groups (r/atom nil)]
519531
(fn tree-select-dropdown-render
520-
[& {:keys [choices disabled?
532+
[& {:keys [choices disabled? required?
521533
width min-width max-width min-height max-height on-change
522534
label-fn alt-text-fn group-label-fn model placeholder id-fn field-label-fn
523535
groups-first? initial-expanded-groups
@@ -578,6 +590,7 @@
578590
(themed ::dropdown-body
579591
{:choices choices
580592
:choice choice
593+
:required? required?
581594
:group-label-fn group-label-fn
582595
:expanded-groups expanded-groups
583596
:disabled? disabled?

0 commit comments

Comments
 (0)