Skip to content

Commit c469556

Browse files
mmalerbajosephperrott
authored andcommitted
Add CDK text-field docs (#10536)
1 parent ff2860f commit c469556

15 files changed

+178
-22
lines changed

src/cdk/text-field/autofill.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
ElementRef,
1313
EventEmitter,
1414
Injectable,
15+
NgZone,
1516
OnDestroy,
1617
OnInit,
1718
Output,
18-
NgZone,
1919
} from '@angular/core';
2020
import {Observable, Subject} from 'rxjs';
2121
import {empty as observableEmpty} from 'rxjs';
@@ -46,7 +46,7 @@ const listenerOptions: any = supportsPassiveEventListeners() ? {passive: true} :
4646
* Based on the following blog post:
4747
* https://medium.com/@brunn/detecting-autofilled-fields-in-javascript-aed598d25da7
4848
*/
49-
@Injectable()
49+
@Injectable({providedIn: 'root'})
5050
export class AutofillMonitor implements OnDestroy {
5151
private _monitoredElements = new Map<Element, MonitoredElementInfo>();
5252

src/cdk/text-field/text-field-module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88

99
import {PlatformModule} from '@angular/cdk/platform';
1010
import {NgModule} from '@angular/core';
11-
import {AutofillMonitor, CdkAutofill} from './autofill';
11+
import {CdkAutofill} from './autofill';
1212
import {CdkTextareaAutosize} from './autosize';
1313

1414

1515
@NgModule({
1616
declarations: [CdkAutofill, CdkTextareaAutosize],
1717
imports: [PlatformModule],
1818
exports: [CdkAutofill, CdkTextareaAutosize],
19-
providers: [AutofillMonitor],
2019
})
2120
export class TextFieldModule {}

src/cdk/text-field/text-field.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
The `text-field` package provides useful utilities for working with text input fields such as
2+
`<input>` and `<textarea>`.
3+
4+
## Automatically resizing a `<textarea>`
5+
6+
The `cdkTextareaAutosize` directive can be applied to any `<textarea>` to make it automatically
7+
resize to fit its content. The minimum and maximum number of rows to expand to can be set via the
8+
`cdkAutosizeMinRows` and `cdkAutosizeMaxRows` properties respectively.
9+
10+
The resize logic can be triggered programmatically by calling `resizeToFitContent`. This method
11+
takes an optional boolean parameter `force` that defaults to `false`. Passing true will force the
12+
`<textarea>` to resize even if its text content has not changed, this can be useful if the styles
13+
affecting the `<textarea>` have changed.
14+
15+
<!-- example(text-field-autosize-textarea) -->
16+
17+
## Monitoring the autofill state of an `<input>`
18+
19+
The `AutofillMonitor` is an injectable service that allows the user to monitor the autofill state of
20+
an `<input>`. It has a `monitor` method that takes an element to monitor and returns an
21+
`Observable` of autofill event objects with a `target` and `isAutofilled` property. The observable
22+
emits every time the autofill state of the given `<input>` changes. Any element you monitor should
23+
eventually be unmonitored by calling `stopMonitoring` with the same element.
24+
25+
<!-- example(text-field-autofill-monitor) -->
26+
27+
To simplify this process, there is also a `cdkAutofill` directive that automatically handles
28+
monitoring and unmonitoring and doubles as an `@Output` binding that emits when the autofill state
29+
changes.
30+
31+
<!-- example(text-field-autofill-directive) -->
32+
33+
Note: This service requires some CSS to install animation hooks when the autofill statechanges. If
34+
you are using Angular Material, this CSS is included as part of the `mat-core` mixin. If you are not
35+
using Angular Material, you should include this CSS with the `cdk-text-field` mixin.
36+
37+
```scss
38+
@import '~@angular/cdk/text-field/text-field';
39+
40+
@include cdk-text-field();
41+
```
42+
43+
## Styling the autofill state of an `<input>`
44+
45+
It can be difficult to override the browser default `background` and `color` properties on an
46+
autofilled `<input>`. To make this simpler, the CDK includes a mixin `cdk-text-field-autofill-color`
47+
which can be used to set these properties. It takes a `background` value as the first parameter and
48+
an optional `color` value as the second parameter.
49+
50+
```scss
51+
@import '~@angular/cdk/text-field/text-field';
52+
53+
// Set custom-autofill inputs to have no background and red text.
54+
input.custom-autofill {
55+
@include cdk-text-field-autofill-color(transparent, red);
56+
}
57+
```

src/lib/input/input.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,15 @@ globally cause input errors to show when the input is dirty and invalid.
7676

7777
### Auto-resizing `<textarea>` elements
7878

79-
`<textarea>` elements can be made to automatically resize to fit their contents by applying the
80-
`cdkTextareaAutosize` directive. This works with `<textarea matInput>` elements as well as plain
81-
native `<textarea>` elements. The min and max size of the textarea can be specified in rows, using
82-
the `cdkAutosizeMinRows` and `cdkAutosizeMaxRows` properties respectively.
79+
`<textarea>` elements can be made to automatically resize by using the
80+
[`cdkAutosizeTextarea` directive](https://material.angular.io/cdk/text-field/overview#automatically-resizing-a-textarea)
81+
available in the CDK.
8382

84-
<!-- example(input-autosize-textarea) -->
83+
### Responding to changes in the autofill state of an `<input>`
84+
85+
The CDK provides
86+
[utilities](https://material.angular.io/cdk/text-field/overview#monitoring-the-autofill-state-of-an-input)
87+
for detecting when an input becomes autofilled and changing the appearance of the autofilled state.
8588

8689
### Accessibility
8790

src/material-examples/input-autosize-textarea/input-autosize-textarea-example.html

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/material-examples/input-autosize-textarea/input-autosize-textarea-example.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<form>
2+
<mat-form-field>
3+
<mat-label>First name</mat-label>
4+
<input matInput (cdkAutofill)="firstNameAutofilled = $event.isAutofilled">
5+
<mat-hint *ngIf="firstNameAutofilled">Autofilled!</mat-hint>
6+
</mat-form-field>
7+
<mat-form-field>
8+
<mat-label>Last name</mat-label>
9+
<input matInput (cdkAutofill)="lastNameAutofilled = $event.isAutofilled">
10+
<mat-hint *ngIf="lastNameAutofilled">Autofilled!</mat-hint>
11+
</mat-form-field>
12+
<button mat-raised-button>Submit</button>
13+
</form>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Component} from '@angular/core';
2+
3+
/** @title Monitoring autofill state with cdkAutofill */
4+
@Component({
5+
selector: 'text-field-autofill-directive-example',
6+
templateUrl: './text-field-autofill-directive-example.html',
7+
styleUrls: ['./text-field-autofill-directive-example.css'],
8+
})
9+
export class TextFieldAutofillDirectiveExample {
10+
firstNameAutofilled: boolean;
11+
lastNameAutofilled: boolean;
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */

0 commit comments

Comments
 (0)