Skip to content

Commit ff0cc7a

Browse files
mmalerbaandrewseguin
authored andcommitted
docs(form-field): update documentation regarding label and placeholder (#11037)
* docs(form-field): update documentation regarding label and placeholder * address comments
1 parent ae0430a commit ff0cc7a

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

src/lib/form-field/form-field.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
*ngIf="_hasFloatingLabel()"
2323
[ngSwitch]="_hasLabel()">
2424

25+
<!-- @deletion-target 8.0.0 remove in favor of mat-label element an placeholder attr. -->
2526
<ng-container *ngSwitchCase="false">
2627
<ng-content select="mat-placeholder"></ng-content>
2728
{{_control.placeholder}}
2829
</ng-container>
2930

3031
<ng-content select="mat-label" *ngSwitchCase="true"></ng-content>
3132

33+
<!-- @deletion-target 8.0.0 remove `mat-placeholder-required` class -->
3234
<span
3335
class="mat-placeholder-required mat-form-field-required-marker"
3436
aria-hidden="true"

src/lib/form-field/form-field.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,22 @@ want a floating label, add a `<mat-label>` to the `mat-form-filed`.
4242

4343
The floating label is a text label displayed on top of the form field control when
4444
the control does not contain any text. By default, when text is present the floating label
45-
floats above the form field control. The label for a form field can be defined either through a
46-
`mat-label` element, by setting the `placeholder` attribute on the form control or using the
47-
`mat-placeholder` element. If there are multiple clashing values (e.g. a `placeholder` and a
48-
label) the `mat-label` will take precedence and the `placeholder` will be shown only when there is
49-
no value.
50-
51-
Placeholder text can be specified using the `placeholder` property on the form field control, or
52-
by adding a `<mat-placeholder>` element inside the form field. Only one of these options should be
53-
used, specifying both will raise an error.
45+
floats above the form field control. The label for a form field can be specified by adding a
46+
`mat-label` element.
47+
48+
In the legacy version of the `<mat-form-field>` (one that has no `appearance` attribute or has
49+
`appearance="legacy"`) if a label is not specified, the `placeholder` attribute on the form control
50+
is promoted to a label. If a label is specified, the `placeholder` will be displayed as a normal
51+
placeholder. The `placeholder` will never be promoted to a label for `standard`, `fill`, and
52+
`outline` form fields. If you want to create a legacy form field with a placeholder but no label,
53+
you will need to specify an empty label to prevent the `placeholder` from being promoted.
54+
55+
```html
56+
<mat-form-field>
57+
<mat-label></mat-label>
58+
<input placeholder="Just a placeholder">
59+
</mat-form-field>
60+
```
5461

5562
If the form field control is marked with a `required` attribute, an asterisk will be appended to the
5663
label to indicate the fact that it is a required field. If unwanted, this can be disabled by
@@ -154,7 +161,8 @@ Any errors and hints added to the form field are automatically added to the form
154161

155162
This error occurs when you have specified two conflicting placeholders. Make sure that you haven't
156163
included both a `placeholder` property on your form field control and a `<mat-placeholder>`
157-
element.
164+
element. The `<mat-placeholder>` element is deprecated, you should use `placeholder` for
165+
placeholders and `<mat-label>` for labels.
158166

159167
#### Error: A hint was already declared for align="..."
160168

src/lib/form-field/placeholder.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import {Directive} from '@angular/core';
1010

1111

12-
/** The placeholder text for an `MatFormField`. */
12+
/**
13+
* The placeholder text for an `MatFormField`.
14+
* @deprecated Use `<mat-label>` to specify the label and the `placeholder` attribute to specify the
15+
* placeholder.
16+
* @deletion-target 8.0.0
17+
*/
1318
@Directive({
1419
selector: 'mat-placeholder'
1520
})

src/lib/input/input.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ All of the attributes that can be used with normal `<input>` and `<textarea>` el
99
on elements inside `<mat-form-field>` as well. This includes Angular directives such as `ngModel`
1010
and `formControl`.
1111

12-
The only limitations are that the `type` attribute can only be one of the values supported by
13-
`matInput` and the native element cannot specify a `placeholder` attribute if the `<mat-form-field>`
14-
also contains an `<mat-placeholder>` element.
12+
The only limitation is that the `type` attribute can only be one of the values supported by
13+
`matInput`.
1514

1615
### Supported `<input>` types
1716

@@ -40,12 +39,11 @@ additional information about these features, see the
4039

4140
### Placeholder
4241

43-
A placeholder is a text label displayed in the input area when the input does not contain text.
44-
When text is present, the placeholder will float above the input area. The placeholder can be
45-
specified either via a `placeholder` attribute on the input or a `<mat-placeholder>` element in the
46-
same form field as the `matInput`. The `<mat-form-field>` also has additional options for changing
47-
the behavior of the placeholder. For more information see the
48-
[form field placeholder documentation](https://material.angular.io/components/form-field/overview#floating-placeholder).
42+
The placeholder is text shown when the `<mat-form-field>` label is floating but the input is empty.
43+
It is used to give the user an additional hint about what they should type in the input. The
44+
placeholder can be specified by setting the `placeholder` attribute on the `<input>` or `<textarea>`
45+
element. In some cases that `<mat-form-field>` may use the placeholder as the label (see the
46+
[form field label documentation](https://material.angular.io/components/form-field/overview#floating-label)).
4947

5048
### Changing when error messages are shown
5149

@@ -90,10 +88,9 @@ for detecting when an input becomes autofilled and changing the appearance of th
9088

9189
The `matInput` directive works with native `<input>` to provide an accessible experience.
9290

93-
If a placeholder attribute is added to the input, or a `mat-placeholder` element is added
94-
in the form field, the placeholder text will automatically be used as the label for the input.
95-
If there's no placeholder specified, `aria-label`, `aria-labelledby` or `<label for=...>` should be
96-
added.
91+
If the containing `<mat-form-field>` has a label it will automatically be used as the `aria-label`
92+
for the `<input>`. However, if there's no label specified in the form field, `aria-label`,
93+
`aria-labelledby` or `<label for=...>` should be added.
9794

9895
Any `mat-error` and `mat-hint` are automatically added to the input's `aria-describedby` list, and
9996
`aria-invalid` is automatically updated based on the input's validity state.

src/lib/select/select.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ these features, see the
3636

3737
### Setting a static placeholder
3838

39-
The placeholder is a text label displayed in the select trigger area when no value is selected. When
40-
a value is selected, the placeholder will float above the select trigger area. The placeholder can
41-
be specified either via a `placeholder` attribute on the `<mat-select>` or a `<mat-placeholder>`
42-
element in the same form field as the `<mat-select>`. The `<mat-form-field>` also has additional
43-
options for changing the behavior of the placeholder. For more information see the
44-
[form field placeholder documentation](https://material.angular.io/components/form-field/overview#floating-placeholder).
39+
The placeholder is text shown when the `<mat-form-field>` label is floating but the `<mat-select>`
40+
is empty. It is used to give the user an additional hint about the value they should select. The
41+
placeholder can be specified by setting the `placeholder` attribute on the `<mat-select>` element.
42+
In some cases that `<mat-form-field>` may use the placeholder as the label (see the
43+
[form field label documentation](https://material.angular.io/components/form-field/overview#floating-label)).
4544

4645
### Disabling the select or individual options
4746

src/material-examples/form-field-label/form-field-label-example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
<mat-option>-- None --</mat-option>
3030
<mat-option value="option">Option</mat-option>
3131
</mat-select>
32-
<mat-placeholder><mat-icon>favorite</mat-icon> <b> Fancy</b> <i> placeholder</i></mat-placeholder>
32+
<mat-label><mat-icon>favorite</mat-icon> <b> Fancy</b> <i> label</i></mat-label>
3333
</mat-form-field>
3434
</div>

0 commit comments

Comments
 (0)