Skip to content

Commit

Permalink
feat(combobox): add min-selection property
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklharper committed Jan 15, 2025
1 parent 03a0aad commit 21e7122
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/calcite-components/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ export class Combobox
*/
messages = useT9n<typeof T9nStrings>();

/**
* Specifies the minimum number of selected items when selection-mode is multiple.
*/
@property({ reflect: true }) minSelection: number = 0;

/**
* Specifies the name of the component.
*
Expand Down Expand Up @@ -540,6 +545,7 @@ export class Combobox

constructor() {
super();
this.listen("blur", this.hostBlurHandler);
this.listenOn(document, "click", this.documentClickHandler);
this.listen("calciteComboboxItemChange", this.calciteComboboxItemChangeHandler);
this.listen("calciteInternalComboboxItemChange", this.calciteInternalComboboxItemChangeHandler);
Expand Down Expand Up @@ -1429,6 +1435,24 @@ export class Combobox
this.textInput.value?.focus();
}

private validateMinSelection() {
if (!this.isMulti()) {
return;
}
if (this.minSelection > 0 && this.selectedItems.length < this.minSelection) {
this.status = "invalid";
// TODO: use translated messages here
this.validationMessage = `This field requires at least ${this.minSelection} item${this.minSelection > 1 ? "s" : ""} to be selected.`;
} else {
this.status = "valid";
this.validationMessage = "";
}
}

private hostBlurHandler() {
this.validateMinSelection();
}

// #endregion

// #region Rendering
Expand Down

0 comments on commit 21e7122

Please sign in to comment.