Select
Angular Material Select provides a dropdown list of options from which users can choose one or more items. Closed, it reads as a form field with a label and the current choice; open, it drops a panel where the options can be grouped under headings, ticked one by one when more than one is allowed, or greyed out when they are not available. BuilderKit restyles the field, the panel and the options so the mat-select component matches the rest of the design system.
For a long list that is easier to filter by typing than to scroll, the autocomplete component narrows the options down as the text is entered.
Example
Below is an example of how BuilderKit restyles the Angular Material Select and gives it a more modern appearance.
<mat-form-field>
<mat-label>Favorite fruit</mat-label>
<mat-select placeholder="Select a food">
@for (option of options; track option.id) {
@if (option.type === 'group') {
<mat-optgroup [label]="option.label">
@for (child of option.children; track child.id) {
<mat-option [value]="child.value">{{ child.label }}</mat-option>
}
</mat-optgroup>
} @else {
<mat-option [value]="option.value">{{ option.label }}</mat-option>
}
}
</mat-select>
<mat-hint align="end">Select your best "fruit"</mat-hint>
</mat-form-field> The arrow is drawn by CSS from an inline chevron, so it does not depend on the icon set loaded in the app. To match a different icon library, swap the mask in select.css.
Size
The tiny, small and large classes on the form field change the size; medium is the default.
<mat-form-field class="tiny">
<mat-label>Tiny</mat-label>
<mat-select placeholder="Select a topping">
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>
<mat-form-field class="small">
<mat-label>Small</mat-label>
<mat-select placeholder="Select a topping">
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-label>Medium</mat-label>
<mat-select placeholder="Select a topping">
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>
<mat-form-field class="large">
<mat-label>Large</mat-label>
<mat-select placeholder="Select a topping">
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>Disabled
The disabled attribute dims the select and keeps its panel closed.
<mat-form-field>
<mat-label>Toppings</mat-label>
<mat-select placeholder="Select a topping" disabled>
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>Native select
A native select with matNativeControl gets the same field styling and opens the platform picker.
<mat-form-field>
<mat-label>Favorite fruit</mat-label>
<select matNativeControl>
@for (option of options; track option.id) {
@if (option.type === 'group') {
<optgroup [label]="option.label">
@for (child of option.children; track child.id) {
<option [value]="child.value">{{ child.label }}</option>
}
</optgroup>
} @else {
<option [value]="option.value">{{ option.label }}</option>
}
}
</select>
</mat-form-field>Selecting multiple options
A multiple select ticks every chosen option in the panel and lists them all, separated by commas, in the closed field.
<mat-form-field>
<mat-label>Toppings</mat-label>
<mat-select multiple [value]="selectedToppings">
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>Custom text in the closed select
Instead of the full list of choices, the closed field can show the first one and a count of the rest, which keeps a narrow field readable.
<mat-form-field>
<mat-label>Toppings</mat-label>
<mat-select multiple [value]="selectedToppings" #toppingsSelect="matSelect">
<mat-select-trigger>
{{ toppingsSelect.value?.[0] }}
@if (toppingsSelect.value?.length > 1) {
<span>(+{{ toppingsSelect.value.length - 1 }} more)</span>
}
</mat-select-trigger>
@for (topping of toppings; track topping) {
<mat-option [value]="topping">{{ topping }}</mat-option>
}
</mat-select>
</mat-form-field>Group divider
The with-divider class on mat-optgroup draws a line above the group and adds space over its label, so it goes on every group but the first.
<mat-select placeholder="Select a food">
@for (group of groups; track group.label) {
<mat-optgroup [label]="group.label" [class.with-divider]="!$first">
@for (option of group.options; track option.value) {
<mat-option [value]="option.value">{{ option.label }}</mat-option>
}
</mat-optgroup>
}
</mat-select>Group without label
The no-label class hides the group heading, keeps the options together and drops the space the heading left behind.
<mat-select placeholder="Select a food">
@for (group of groups; track group.label) {
<mat-optgroup class="no-label" [label]="group.label">
@for (option of group.options; track option.value) {
<mat-option [value]="option.value">{{ option.label }}</mat-option>
}
</mat-optgroup>
}
</mat-select>Group divider without label
Combined with with-divider, the no-label class keeps the space over the group and gives plain sections split by a line.
<mat-select placeholder="Select a food">
@for (group of groups; track group.label) {
<mat-optgroup class="no-label" [label]="group.label" [class.with-divider]="!$first">
@for (option of group.options; track option.value) {
<mat-option [value]="option.value">{{ option.label }}</mat-option>
}
</mat-optgroup>
}
</mat-select>