Skip to content

Commit a028e81

Browse files
committed
input examples
1 parent ecb5507 commit a028e81

13 files changed

+93
-18
lines changed

src/lib/input/input.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ must implement a single method `isErrorState` which takes the `FormControl` for
5454
well as the parent form and returns a boolean indicating whether errors should be shown. (`true`
5555
indicating that they should be shown, and `false` indicating that they should not.)
5656

57-
<!-- TODO(mmalerba): examplify -->
57+
<!-- example(input-error-state-matcher-example) -->
5858

5959
```html
6060
<mat-form-field>
@@ -91,7 +91,7 @@ globally cause input errors to show when the input is dirty and invalid.
9191
native `<textarea>` elements. The min and max size of the textarea can be specified in rows, using
9292
the `matAutosizeMinRows` and `matAutosizeMaxRows` properties respectively.
9393

94-
<!-- TODO(mmalerba): example -->
94+
<!-- example(input-autosize-textarea-example) -->
9595

9696
### Accessibility
9797
The `matInput` directive works with native `<input>` to provide an accessible experience.

src/material-examples/example-module.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ import {GridListDynamicExample} from './grid-list-dynamic/grid-list-dynamic-exam
5252
import {GridListOverviewExample} from './grid-list-overview/grid-list-overview-example';
5353
import {IconOverviewExample} from './icon-overview/icon-overview-example';
5454
import {IconSvgExample} from './icon-svg-example/icon-svg-example';
55+
import {InputAutosizeTextareaExample} from './input-autosize-textarea/input-autosize-textarea-example';
5556
import {InputClearableExample} from './input-clearable/input-clearable-example';
57+
import {InputErrorStateMatcherExample} from './input-error-state-matcher/input-error-state-matcher-example';
5658
import {InputErrorsExample} from './input-errors/input-errors-example';
5759
import {InputFormExample} from './input-form/input-form-example';
5860
import {InputHintExample} from './input-hint/input-hint-example';
@@ -333,14 +335,26 @@ export const EXAMPLE_COMPONENTS = {
333335
additionalFiles: null,
334336
selectorName: null
335337
},
338+
'input-autosize-textarea': {
339+
title: 'Auto-resizing textarea',
340+
component: InputAutosizeTextareaExample,
341+
additionalFiles: null,
342+
selectorName: null
343+
},
336344
'input-clearable': {
337-
title: 'Input Clearable',
345+
title: 'Input with a clear button',
338346
component: InputClearableExample,
339347
additionalFiles: null,
340348
selectorName: null
341349
},
350+
'input-error-state-matcher': {
351+
title: 'Input with a custom ErrorStateMatcher',
352+
component: InputErrorStateMatcherExample,
353+
additionalFiles: null,
354+
selectorName: null
355+
},
342356
'input-errors': {
343-
title: 'Input Errors',
357+
title: 'Input with error messages',
344358
component: InputErrorsExample,
345359
additionalFiles: null,
346360
selectorName: null
@@ -352,7 +366,7 @@ export const EXAMPLE_COMPONENTS = {
352366
selectorName: null
353367
},
354368
'input-hint': {
355-
title: 'Input hints',
369+
title: 'Input with hints',
356370
component: InputHintExample,
357371
additionalFiles: null,
358372
selectorName: null
@@ -364,7 +378,7 @@ export const EXAMPLE_COMPONENTS = {
364378
selectorName: null
365379
},
366380
'input-prefix-suffix': {
367-
title: 'Input Prefixes and Suffixes',
381+
title: 'Inputs with prefixes and suffixes',
368382
component: InputPrefixSuffixExample,
369383
additionalFiles: null,
370384
selectorName: null
@@ -645,7 +659,9 @@ export const EXAMPLE_LIST = [
645659
GridListOverviewExample,
646660
IconOverviewExample,
647661
IconSvgExample,
662+
InputAutosizeTextareaExample,
648663
InputClearableExample,
664+
InputErrorStateMatcherExample,
649665
InputErrorsExample,
650666
InputFormExample,
651667
InputHintExample,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<mat-form-field>
2+
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize matAutosizeMinRows="2"
3+
matAutosizeMaxRows="5"></textarea>
4+
</mat-form-field>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {Component} from '@angular/core';
2+
3+
/** @title Auto-resizing textarea */
4+
@Component({
5+
selector: 'input-autosize-textarea-example',
6+
templateUrl: './input-autosize-textarea-example.html',
7+
styleUrls: ['./input-autosize-textarea-example.css'],
8+
})
9+
export class InputAutosizeTextareaExample {}

src/material-examples/input-clearable/input-clearable-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Component} from '@angular/core';
22

33
/**
4-
* @title Input Clearable
4+
* @title Input with a clear button
55
*/
66
@Component({
77
selector: 'input-clearable-example',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.example-form {
2+
min-width: 150px;
3+
max-width: 500px;
4+
width: 100%;
5+
}
6+
7+
.example-full-width {
8+
width: 100%;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<form class="example-form">
2+
<mat-form-field class="example-full-width">
3+
<input matInput placeholder="Email" [formControl]="emailFormControl"
4+
[errorStateMatcher]="matcher">
5+
<mat-hint>Errors appear instantly!</mat-hint>
6+
<mat-error *ngIf="emailFormControl.hasError('email') && !emailFormControl.hasError('required')">
7+
Please enter a valid email address
8+
</mat-error>
9+
<mat-error *ngIf="emailFormControl.hasError('required')">
10+
Email is <strong>required</strong>
11+
</mat-error>
12+
</mat-form-field>
13+
</form>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl, FormGroupDirective, NgForm, Validators} from '@angular/forms';
3+
import {ErrorStateMatcher} from '@angular/material';
4+
5+
/** Error when invalid control is dirty, touched, or submitted. */
6+
export class MyErrorStateMatcher implements ErrorStateMatcher {
7+
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
8+
const isSubmitted = form && form.submitted;
9+
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
10+
}
11+
}
12+
13+
/** @title Input with a custom ErrorStateMatcher */
14+
@Component({
15+
selector: 'input-error-state-matcher-example',
16+
templateUrl: './input-error-state-matcher-example.html',
17+
styleUrls: ['./input-error-state-matcher-example.css'],
18+
})
19+
export class InputErrorStateMatcherExample {
20+
emailFormControl = new FormControl('', [
21+
Validators.required,
22+
Validators.email,
23+
]);
24+
25+
matcher = new MyErrorStateMatcher();
26+
}

src/material-examples/input-errors/input-errors-example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<form class="example-form">
22
<mat-form-field class="example-full-width">
33
<input matInput placeholder="Email" [formControl]="emailFormControl">
4-
<mat-error *ngIf="emailFormControl.hasError('pattern')">
4+
<mat-error *ngIf="emailFormControl.hasError('email') && !emailFormControl.hasError('required')">
55
Please enter a valid email address
66
</mat-error>
77
<mat-error *ngIf="emailFormControl.hasError('required')">
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import {Component} from '@angular/core';
22
import {FormControl, Validators} from '@angular/forms';
33

4-
const EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
5-
64
/**
7-
* @title Input Errors
5+
* @title Input with error messages
86
*/
97
@Component({
108
selector: 'input-errors-example',
119
templateUrl: 'input-errors-example.html',
1210
styleUrls: ['input-errors-example.css'],
1311
})
1412
export class InputErrorsExample {
15-
1613
emailFormControl = new FormControl('', [
1714
Validators.required,
18-
Validators.pattern(EMAIL_REGEX)]);
19-
15+
Validators.email,
16+
]);
2017
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {Component} from '@angular/core';
22

33
/**
4-
* @title Input hints
4+
* @title Input with hints
55
*/
66
@Component({
77
selector: 'input-hint-example',
88
templateUrl: 'input-hint-example.html',
99
styleUrls: ['input-hint-example.css'],
1010
})
11-
export class InputHintExample { }
11+
export class InputHintExample {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {Component} from '@angular/core';
22

33
/**
4-
* @title Input Prefixes and Suffixes
4+
* @title Inputs with prefixes and suffixes
55
*/
66
@Component({
77
selector: 'input-prefix-suffix-example',
88
templateUrl: 'input-prefix-suffix-example.html',
99
styleUrls: ['input-prefix-suffix-example.css'],
1010
})
11-
export class InputPrefixSuffixExample { }
11+
export class InputPrefixSuffixExample {}

0 commit comments

Comments
 (0)