Skip to content

Commit 8e7a8c6

Browse files
committed
select docs & examples
1 parent a028e81 commit 8e7a8c6

34 files changed

+491
-104
lines changed

guides/creating-a-custom-form-field-control.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class MyTelInput {
7070
```
7171

7272
### Providing our component as a MatFormFieldControl
73+
7374
The first step is to provide our new component as an implementation of the `MatFormFieldControl`
7475
interface that the `<mat-form-field>` knows how to work with. To do this, we will have our class
7576
implement `MatFormFieldControl`. Since this is a generic interface, we'll need to include a type
@@ -95,12 +96,15 @@ the `MatFormFieldControl` interface, see the
9596
properties in this guide.)
9697

9798
### Implementing the methods and properties of MatFormFieldControl
99+
98100
#### `value`
101+
99102
This property allows someone to set or get the value of our control. Its type should be the same
100103
type we used for the type parameter when we implemented `MatFormFieldControl`. Since our component
101104
already has a value property, we don't need to do anything for this one.
102105

103106
#### `stateChanges`
107+
104108
Because the `<mat-form-field>` uses the `OnPush` change detection strategy, we need to let it know
105109
when something happens in the form field control that may require the form field to run change
106110
detection. We do this via the `stateChanges` property. So far the only thing the form field needs to
@@ -122,6 +126,7 @@ ngOnDestroy() {
122126
```
123127

124128
#### `id`
129+
125130
This property should return the ID of an element in the component's template that we want the
126131
`<mat-form-field>` to associate all of its labels and hints with. In this case, we'll use the host
127132
element and just generate a unique ID for it.
@@ -133,6 +138,7 @@ static nextId = 0;
133138
```
134139

135140
#### `placeholder`
141+
136142
This property allows us to tell the `<mat-form-field>` what to use as a placeholder. In this
137143
example, we'll do the same thing as `matInput` and `<mat-select>` and allow the user to specify it
138144
via an `@Input()`. Since the value of the placeholder may change over time, we need to make sure to
@@ -152,6 +158,7 @@ private _placeholder: string;
152158
```
153159

154160
#### `ngControl`
161+
155162
This property allows the form field control to specify the `@angular/forms` control that is bound to
156163
this component. Since we haven't set up our component to act as a `ControlValueAccessor`, we'll just
157164
set this to `null` in our component. In any real component, you would probably want to implement
@@ -170,6 +177,7 @@ constructor(..., @Optional() @Self() public ngControl: NgControl) { ... }
170177
```
171178

172179
#### `focused`
180+
173181
This property indicates whether or not the form field control should be considered to be in a
174182
focused state. When it is in a focused state, the form field is displayed with a solid color
175183
underline. For the purposes of our component, we want to consider it focused if any of the part
@@ -195,6 +203,7 @@ ngOnDestroy() {
195203
```
196204

197205
#### `empty`
206+
198207
This property indicates whether the form field control is empty. For our control, we'll consider it
199208
empty if all of the parts are empty.
200209

@@ -206,6 +215,7 @@ get empty() {
206215
```
207216

208217
#### `shouldPlaceholderFloat`
218+
209219
This property is used to indicate whether the placeholder should be in the floating position. We'll
210220
use the same logic as `matInput` and float the placeholder when the input is focused or non-empty.
211221
Since the placeholder will be overlapping our control when when it's not floating, we should hide
@@ -228,6 +238,7 @@ span {
228238
```
229239

230240
#### `required`
241+
231242
This property is used to indicate whether the input is required. `<mat-form-field>` uses this
232243
information to add a required indicator to the placeholder. Again, we'll want to make sure we run
233244
change detection if the required state changes.
@@ -245,6 +256,7 @@ private _required = false;
245256
```
246257

247258
#### `disabled`
259+
248260
This property tells the form field when it should be in the disabled state. In addition to reporting
249261
the right state to the form field, we need to set the disabled state on the individual inputs that
250262
make up our component.
@@ -269,6 +281,7 @@ private _disabled = false;
269281
```
270282

271283
#### `errorState`
284+
272285
This property indicates whether the associated `NgControl` is in an error state. Since we're not
273286
using an `NgControl` in this example, we don't need to do anything other than just set it to `false`.
274287

@@ -277,6 +290,7 @@ errorState = false;
277290
```
278291

279292
#### `controlType`
293+
280294
This property allows us to specify a unique string for the type of control in form field. The
281295
`<mat-form-field>` will add an additional class based on this type that can be used to easily apply
282296
special styles to a `<mat-form-field>` that contains a specific type of control. In this example
@@ -288,6 +302,7 @@ controlType = 'my-tel-input';
288302
```
289303

290304
#### `setAriaDescribedByIds(ids: string[])`
305+
291306
This method is used by the `<mat-form-field>` to specify the IDs that should be used for the
292307
`aria-describedby` attribute of your component. The method has one parameter, the list of IDs, we
293308
just need to apply the given IDs to our host element.
@@ -301,6 +316,7 @@ setDescribedByIds(ids: string[]) {
301316
```
302317

303318
#### `onContainerClick(event: MouseEvent)`
319+
304320
This method will be called when the form field is clicked on. It allows your component to hook in
305321
and handle that click however it wants. The method has one parameter, the `MouseEvent` for the
306322
click. In our case we'll just focus the first `<input>` if the user isn't about to click an
@@ -315,6 +331,7 @@ onContainerClick(event: MouseEvent) {
315331
```
316332

317333
### Trying it out
334+
318335
Now that we've fully implemented the interface, we're ready to try our component out! All we need to
319336
do is place it inside of a `<mat-form-field>`
320337

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The following Angular Material components are designed to work inside a `<mat-fo
1414
<!-- example(form-field-overview) -->
1515

1616
### Floating placeholder
17+
1718
The floating placeholder is a text label displayed on top of the form field control when
1819
the control does not contain any text. By default, when text is present the floating placeholder
1920
floats above the form field control.
@@ -46,6 +47,7 @@ setting can be either `always`, `never`, or `auto`.
4647
```
4748

4849
### Hint labels
50+
4951
Hint labels are additional descriptive text that appears below the form field's underline. A
5052
`<mat-form-field>` can have up to two hint labels; one start-aligned (left in an LTR language, right
5153
in RTL), and one end-aligned.
@@ -60,6 +62,7 @@ raise an error.
6062
<!-- example(form-field-hint) -->
6163

6264
### Error messages
65+
6366
Error messages can be shown under the form field underline by adding `mat-error` elements inside the
6467
form field. Errors are hidden initially and will be displayed on invalid form fields after the user
6568
has interacted with the element or the parent form has been submitted. Since the errors occupy the
@@ -74,6 +77,7 @@ multiple errors is up to the user.
7477
<!-- example(form-field-error) -->
7578

7679
### Prefix & suffix
80+
7781
Custom content can be included before and after the input tag, as a prefix or suffix. It will be
7882
included within the visual container that wraps the form control as per the Material specification.
7983

@@ -83,12 +87,14 @@ the prefix. Similarly, adding `matSuffix` will designate it as the suffix.
8387
<!-- example(form-field-prefix-suffix) -->
8488

8589
### Custom form field controls
90+
8691
In addition to the form field controls that Angular Material provides, it is possible to create
8792
custom form field controls that work with `<mat-form-field>` in the same way. For additional
8893
information on this see the guide on
8994
[Creating Custom mat-form-field Controls](https://material.angular.io/guide/creating-a-custom-form-field-control).
9095

9196
### Theming
97+
9298
`<mat-form-field>` has a `color` property which can be set to `primary`, `accent`, or `warn`. This
9399
will set the color of the form field underline and floating placeholder based on the theme colors
94100
of your app.
@@ -105,6 +111,7 @@ mat-form-field.mat-form-field {
105111
<!-- example(form-field-theming) -->
106112

107113
### Accessibility
114+
108115
If a floating placeholder is specified, it will be automatically used as the label for the form
109116
field control. If no floating placeholder is specified, the user should label the form field control
110117
themselves using `aria-label`, `aria-labelledby` or `<label for=...>`.
@@ -113,16 +120,20 @@ Any errors and hints added to the form field are automatically added to the form
113120
`aria-describedby` set.
114121

115122
### Troubleshooting
123+
116124
#### Error: Placeholder attribute and child element were both specified
125+
117126
This error occurs when you have specified two conflicting placeholders. Make sure that you haven't
118127
included both a `placeholder` property on your form field control and a `<mat-placeholder>`
119128
element.
120129

121130
#### Error: A hint was already declared for align="..."
131+
122132
This error occurs if you have added multiple hints for the same side. Keep in mind that the
123133
`hintLabel` property adds a hint to the start side.
124134

125135
#### Error: mat-form-field must contain a MatFormFieldControl
136+
126137
This error occurs when you have not added a form field control to your form field. If your form
127138
field contains a native `<input>` or `<textarea>` element, make sure you've added the `matInput`
128139
directive to it. Other components that can act as a form field control include `<mat-select>`,

src/lib/input/input.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<!-- example(input-overview) -->
55

66
### `<input>` and `<textarea>` attributes
7+
78
All of the attributes that can be used with normal `<input>` and `<textarea>` elements can be used
89
on elements inside `<mat-form-field>` as well. This includes Angular directives such as `ngModel`
910
and `formControl`.
@@ -13,6 +14,7 @@ The only limitations are that the `type` attribute can only be one of the values
1314
also contains an `<mat-placeholder>` element.
1415

1516
### Supported `<input>` types
17+
1618
The following [input types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input) can
1719
be used with `matInput`:
1820
* date
@@ -29,20 +31,23 @@ be used with `matInput`:
2931
* week
3032

3133
### Form field features
34+
3235
There are a number of `<mat-form-field>` features that can be used with any `<input matInput>` or
3336
`<textarea matInput>`. These include error messages, hint text, prefix & suffix, and theming. For
3437
additional information about these features, see the
3538
[form field documentation](https://material.angular.io/components/form-field/overview).
3639

3740
### Placeholder
41+
3842
A placeholder is a text label displayed in the input area when the input does not contain text.
39-
When text is present, placeholder will float above the input area. The placeholder can be specified
40-
either via a `placeholder` attribute on the input or a `<mat-placeholder>` element in the same
41-
form field at the `matInput`. The `<mat-form-field>` also has additional options for changing the
42-
behavior of the placeholder. For more information see the
43+
When text is present, the placeholder will float above the input area. The placeholder can be
44+
specified either via a `placeholder` attribute on the input or a `<mat-placeholder>` element in the
45+
same form field as the `matInput`. The `<mat-form-field>` also has additional options for changing
46+
the behavior of the placeholder. For more information see the
4347
[form field placeholder documentation](https://material.angular.io/components/form-field/overview#floating-placeholder).
4448

45-
### Custom Error Matcher
49+
### Changing when error messages are shown
50+
4651
The `<mat-form-field>` allows you to
4752
[associate error messages](https://material.angular.io/components/form-field/overview#error-messages)
4853
with your `matInput`. By default, these error messages are shown when the control is invalid and
@@ -56,23 +61,6 @@ indicating that they should be shown, and `false` indicating that they should no
5661

5762
<!-- example(input-error-state-matcher-example) -->
5863

59-
```html
60-
<mat-form-field>
61-
<input matInput [(ngModel)]="myInput" required [errorStateMatcher]="myErrorStateMatcher">
62-
<mat-error>This field is required</mat-error>
63-
</mat-form-field>
64-
```
65-
66-
```ts
67-
class MyErrorStateMatcher implements ErrorStateMatcher {
68-
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
69-
// Error when invalid control is dirty, touched, or submitted
70-
const isSubmitted = form && form.submitted;
71-
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted)));
72-
}
73-
}
74-
```
75-
7664
A global error state matcher can be specified by setting the `ErrorStateMatcher` provider. This
7765
applies to all inputs. For convenience, `ShowOnDirtyErrorStateMatcher` is available in order to
7866
globally cause input errors to show when the input is dirty and invalid.
@@ -86,6 +74,7 @@ globally cause input errors to show when the input is dirty and invalid.
8674
```
8775

8876
### Auto-resizing `<textarea>` elements
77+
8978
`<textarea>` elements can be made to automatically resize to fit their contents by applying the
9079
`matTextareaAutosize` directive. This works with `<textarea matInput>` elements as well as plain
9180
native `<textarea>` elements. The min and max size of the textarea can be specified in rows, using
@@ -94,6 +83,7 @@ the `matAutosizeMinRows` and `matAutosizeMaxRows` properties respectively.
9483
<!-- example(input-autosize-textarea-example) -->
9584

9685
### Accessibility
86+
9787
The `matInput` directive works with native `<input>` to provide an accessible experience.
9888

9989
If a placeholder attribute is added to the input, or a `mat-placeholder` element is added
@@ -105,7 +95,9 @@ Any `mat-error` and `mat-hint` are automatically added to the input's `aria-desc
10595
`aria-invalid` is automatically updated based on the input's validity state.
10696

10797
### Troubleshooting
108-
#### Error: Input type "..." isn't supported by matInput.
98+
99+
#### Error: Input type "..." isn't supported by matInput
100+
109101
This error is thrown when you attempt to set an input's `type` property to a value that isn't
110102
supported by the `matInput` directive. If you need to use an unsupported input type with
111103
`<mat-form-field>` consider writing a

0 commit comments

Comments
 (0)