Skip to content

Commit 26ab43b

Browse files
authored
fix(material/mdc-form-field): text-field should stretch based on host element (#18778)
Currently the MDC text-field uses `inline-flex` but does not expand to the width of the form-field host element. We want the text-field to be based on the actual form-field host element as it makes it easier for developers to control the width of form-fields. i.e. adding `display: block`, or specifying a maximum width.
1 parent 76c3fb6 commit 26ab43b

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/dev-app/mdc-input/mdc-input-demo.html

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,23 +637,27 @@ <h3>&lt;textarea&gt; with bindable autosize </h3>
637637
<mat-card class="demo-card demo-basic">
638638
<mat-toolbar color="primary">Autocomplete</mat-toolbar>
639639
<mat-card-content>
640-
<mat-form-field class="demo-block">
641-
<mat-label>Pick Number</mat-label>
642-
<input type="text" aria-label="Number" matInput
643-
[matAutocomplete]="autoComplete1">
644-
<mat-autocomplete #autoComplete1="matAutocomplete">
645-
<mat-option *ngFor="let option of options" [value]="option">{{option}}</mat-option>
646-
</mat-autocomplete>
647-
</mat-form-field>
640+
<p>
641+
<mat-form-field>
642+
<mat-label>Pick Number</mat-label>
643+
<input type="text" aria-label="Number" matInput
644+
[matAutocomplete]="autoComplete1">
645+
<mat-autocomplete #autoComplete1="matAutocomplete">
646+
<mat-option *ngFor="let option of options" [value]="option">{{option}}</mat-option>
647+
</mat-autocomplete>
648+
</mat-form-field>
649+
</p>
648650

649-
<mat-form-field appearance="outline" class="demo-block">
650-
<mat-label>Pick Number</mat-label>
651-
<input type="text" aria-label="Number" matInput
652-
[matAutocomplete]="autoComplete2">
653-
<mat-autocomplete #autoComplete2="matAutocomplete">
654-
<mat-option *ngFor="let option of options" [value]="option">{{option}}</mat-option>
655-
</mat-autocomplete>
656-
</mat-form-field>
651+
<p>
652+
<mat-form-field appearance="outline">
653+
<mat-label>Pick Number</mat-label>
654+
<input type="text" aria-label="Number" matInput
655+
[matAutocomplete]="autoComplete2">
656+
<mat-autocomplete #autoComplete2="matAutocomplete">
657+
<mat-option *ngFor="let option of options" [value]="option">{{option}}</mat-option>
658+
</mat-autocomplete>
659+
</mat-form-field>
660+
</p>
657661
</mat-card-content>
658662
</mat-card>
659663

src/material-experimental/mdc-form-field/form-field.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
width: 100%;
3939
}
4040

41+
// The MDC text-field should stretch to the width of the host `<mat-form-field>` element.
42+
// This allows developers to control the width without needing custom CSS overrides.
43+
.mat-mdc-text-field-wrapper {
44+
width: 100%;
45+
}
46+
4147
// Infix that contains the projected content (usually an input or a textarea). We ensure
4248
// that the projected form-field control and content can stretch as needed, but we also
4349
// apply a default infix width to make the form-field's look natural.

0 commit comments

Comments
 (0)