1717
1818import static java .util .Objects .isNull ;
1919import static java .util .Objects .nonNull ;
20+ import static org .dominokit .domino .ui .style .DisplayCss .dui_hidden ;
21+ import static org .dominokit .domino .ui .style .SpacingCss .dui_flex_nowrap ;
2022
2123import java .util .Arrays ;
2224import java .util .HashSet ;
2325import java .util .List ;
2426import java .util .Optional ;
2527import java .util .Set ;
2628import java .util .stream .Collectors ;
29+ import org .dominokit .domino .ui .badges .Badge ;
2730import org .dominokit .domino .ui .elements .DivElement ;
31+ import org .dominokit .domino .ui .style .BooleanCssClass ;
2832import org .dominokit .domino .ui .utils .BaseDominoElement ;
33+ import org .dominokit .domino .ui .utils .PrimaryAddOn ;
2934
3035/**
3136 * Represents a multi-selection dropdown menu UI component, allowing users to select multiple
@@ -45,6 +50,7 @@ public class MultiSelect<V>
4550 extends AbstractSelect <V , List <V >, DivElement , SelectOption <V >, MultiSelect <V >> {
4651
4752 private Set <SelectOption <V >> selectedOptions = new HashSet <>();
53+ private Badge selectionCountBadge ;
4854
4955 /**
5056 * Creates a new instance of {@link MultiSelect} without any predefined label.
@@ -71,6 +77,9 @@ public static <V> MultiSelect<V> create(String label) {
7177 public MultiSelect () {
7278 optionsMenu .setMultiSelect (true );
7379 setAutoCloseOnSelect (false );
80+ selectionCountBadge =
81+ Badge .create (getConfig ().multiSelectSelectionCountFormatExpression (0 )).addCss (dui_hidden );
82+ appendChild (PrimaryAddOn .of (selectionCountBadge ));
7483 }
7584
7685 /**
@@ -163,6 +172,9 @@ protected void onOptionSelected(SelectOption<V> option, boolean silent) {
163172 fieldInput .appendChild (option );
164173 selectedOptions .add (option );
165174 getInputElement ().element ().focus ();
175+ selectionCountBadge .setText (
176+ getConfig ().multiSelectSelectionCountFormatExpression (selectedOptions .size ()));
177+ option .onSelected ();
166178 }
167179
168180 /**
@@ -199,6 +211,9 @@ protected void onOptionDeselected(SelectOption<V> option, boolean silent) {
199211 if (!silent ) {
200212 triggerChangeListeners (oldValue , getValue ());
201213 }
214+ selectionCountBadge .setText (
215+ getConfig ().multiSelectSelectionCountFormatExpression (selectedOptions .size ()));
216+ option .onDeselected ();
202217 }
203218 }
204219
@@ -243,4 +258,33 @@ protected void onTypingEnd() {
243258 protected void onOptionRemoved (SelectOption <V > option ) {
244259 selectedOptions .remove (option );
245260 }
261+
262+ /**
263+ * Sets whether the selection should wrap or not.
264+ *
265+ * <p>If {@code wrapSelection} is {@code true}, the selection will wrap. Otherwise, it will be
266+ * forced to stay on a single line using a no-wrap CSS class.
267+ *
268+ * @param wrapSelection {@code true} to allow wrapping, {@code false} to prevent wrapping
269+ * @return this {@code MultiSelect} instance for method chaining
270+ */
271+ public MultiSelect <V > setWrapSelection (boolean wrapSelection ) {
272+ fieldInput .addCss (BooleanCssClass .of (dui_flex_nowrap , !wrapSelection ));
273+ return this ;
274+ }
275+
276+ /**
277+ * Sets whether the selection count badge should be visible.
278+ *
279+ * <p>If {@code showSelectionCount} is {@code true}, the selection count badge will be shown.
280+ * Otherwise, it will be hidden using the appropriate CSS class.
281+ *
282+ * @param showSelectionCount {@code true} to display the selection count badge, {@code false} to
283+ * hide it
284+ * @return this {@code MultiSelect} instance for method chaining
285+ */
286+ public MultiSelect <V > setShowSelectionCount (boolean showSelectionCount ) {
287+ selectionCountBadge .addCss (BooleanCssClass .of (dui_hidden , !showSelectionCount ));
288+ return this ;
289+ }
246290}
0 commit comments