Skip to content

Commit cbad345

Browse files
crisbetojelbourn
authored andcommitted
docs(autocomplete): document that mat-autocomplete can be used without mat-form-field (#15928)
Based on the discussion in #15684, it looks like we haven't communicated very well that `mat-autocomplete` can be used with any input, not just `mat-form-field`. These changes add some docs and a live example to showcase the functionality.
1 parent 7774ae1 commit cbad345

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<form class="example-form">
2+
<input type="text" placeholder="Search for a street" [formControl]="control" [matAutocomplete]="auto">
3+
<mat-autocomplete #auto="matAutocomplete">
4+
<mat-option *ngFor="let street of filteredStreets | async" [value]="street">
5+
{{street}}
6+
</mat-option>
7+
</mat-autocomplete>
8+
</form>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {Component, OnInit} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
import {Observable} from 'rxjs';
4+
import {startWith, map} from 'rxjs/operators';
5+
6+
/**
7+
* @title Plain input autocomplete
8+
*/
9+
@Component({
10+
selector: 'autocomplete-plain-input-example',
11+
templateUrl: 'autocomplete-plain-input-example.html',
12+
styleUrls: ['autocomplete-plain-input-example.css'],
13+
})
14+
export class PlainInputAutocompleteExample implements OnInit {
15+
control = new FormControl();
16+
streets: string[] = ['Champs-Élysées', 'Lombard Street', 'Abbey Road', 'Fifth Avenue'];
17+
filteredStreets: Observable<string[]>;
18+
19+
ngOnInit() {
20+
this.filteredStreets = this.control.valueChanges.pipe(
21+
startWith(''),
22+
map(value => this._filter(value))
23+
);
24+
}
25+
26+
private _filter(value: string): string[] {
27+
const filterValue = this._normalizeValue(value);
28+
return this.streets.filter(street => this._normalizeValue(street).includes(filterValue));
29+
}
30+
31+
private _normalizeValue(value: string): string {
32+
return value.toLowerCase().replace(/\s/g, '');
33+
}
34+
}

src/material/autocomplete/autocomplete.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ injection token.
9090

9191
<!-- example(autocomplete-auto-active-first-option) -->
9292

93+
### Autocomplete on a custom input element
94+
95+
While `mat-autocomplete` supports attaching itself to a `mat-form-field`, you can also set it on
96+
any other `input` element using the `matAutocomplete` attribute. This allows you to customize what
97+
the input looks like without having to bring in the extra functionality from `mat-form-field`.
98+
99+
<!-- example(autocomplete-plain-input) -->
100+
93101
### Attaching the autocomplete panel to a different element
94102

95103
By default the autocomplete panel will be attached to your input element, however in some cases you

0 commit comments

Comments
 (0)