Reputation: 91
How to make the mat-option
automatically select the width of the longest item in the list.
<div class="col-md-3">
<mat-form-field class="sm-width" appearance="outline">
<mat-label>Дом</mat-label>
<input (input)="InputBuilding($event)" matInput placeholder="№ дома" formControlName="buildingNumber" required maxlength="5" [matAutocomplete]="autoBuilding">
<mat-autocomplete panelClass="mat-auto" #autoBuilding="matAutocomplete">
<mat-option (onSelectionChange)="selectionChange(build)" *ngFor="let build of builds?.result" [value]="build.name">
<span>{{build.typeShort}}. {{build.name}}</span>
</mat-option>
</mat-autocomplete>
</mat-form-field>
<mat-form-field class="sm-width" appearance="outline">
<mat-label>Корпус</mat-label>
<input matInput placeholder="Корпус" formControlName="structure" maxlength="3">
</mat-form-field>
<mat-form-field class="sm-width" appearance="outline">
<mat-label>Квартира</mat-label>
<input matInput placeholder="Квартира / Офис" formControlName="flat" maxlength="4" numbersOnly>
</mat-form-field>
</div>
Upvotes: 4
Views: 6507
Reputation: 2399
A newer and better answer to this question is:
<mat-autocomplete panelWidth="fit-content" ...>
Upvotes: 1
Reputation: 11
u may try like this
.auto-width{
.mat-form-field {
width: auto !important;
}
.mat-select-value {
max-width: 100%;
width: auto;
}
}
Upvotes: 0
Reputation: 766
Try
::ng-deep mat-autocomplete {
width: fit-content !important;
}
Upvotes: 3