Skip to content

Commit c5c96d9

Browse files
josephperrottandrewseguin
authored andcommitted
- Remove extra <md-autocomplete> blocks in example. (#7149)
- Add isOpen attribute to `MdAutocomplete` component.
1 parent f8cd790 commit c5c96d9

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/demo-app/autocomplete/autocomplete-demo.html

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,6 @@
6666
</mat-card>
6767
</div>
6868

69-
<mat-autocomplete #reactiveAuto="matAutocomplete" [displayWith]="displayFn">
70-
<mat-option *ngFor="let state of reactiveStates | async" [value]="state">
71-
<span>{{ state.name }}</span>
72-
<span class="demo-secondary-text"> ({{state.code}}) </span>
73-
</mat-option>
74-
</mat-autocomplete>
75-
76-
<mat-autocomplete #tdAuto="matAutocomplete">
77-
<mat-option *ngFor="let state of tdStates" [value]="state.name">
78-
<span>{{ state.name }}</span>
79-
</mat-option>
80-
</mat-autocomplete>
81-
8269
<mat-autocomplete #groupedAuto="matAutocomplete">
8370
<mat-optgroup *ngFor="let group of filteredGroupedStates"
8471
[label]="'States starting with ' + group.letter">

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
169169
this._resetPlaceholder();
170170

171171
if (this._panelOpen) {
172-
this._panelOpen = false;
172+
this.autocomplete._isOpen = this._panelOpen = false;
173173

174174
// We need to trigger change detection manually, because
175175
// `fromEvent` doesn't seem to do it at the proper time.
@@ -454,7 +454,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
454454
}
455455

456456
this.autocomplete._setVisibility();
457-
this._panelOpen = true;
457+
this.autocomplete._isOpen = this._panelOpen = true;
458458
}
459459

460460
private _getOverlayConfig(): OverlayConfig {

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ describe('MatAutocomplete', () => {
409409
expect(inputContainer._animateAndLockPlaceholder).toHaveBeenCalled();
410410
});
411411

412+
it('should provide the open state of the panel', async(() => {
413+
expect(fixture.componentInstance.panel.isOpen).toBeFalsy(
414+
`Expected the panel to be unopened initially.`);
415+
416+
dispatchFakeEvent(input, 'focusin');
417+
fixture.detectChanges();
418+
fixture.whenStable().then(() => {
419+
expect(fixture.componentInstance.panel.isOpen).toBeTruthy(
420+
`Expected the panel to be opened on focus.`);
421+
});
422+
}));
412423
});
413424

414425
it('should have the correct text direction in RTL', () => {

src/lib/autocomplete/autocomplete.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export class MatAutocomplete implements AfterContentInit {
5858
/** Whether the autocomplete panel should be visible, depending on option length. */
5959
showPanel = false;
6060

61+
/** Whether the autocomplete panel is open. */
62+
get isOpen(): boolean {
63+
return this._isOpen && this.showPanel;
64+
}
65+
_isOpen: boolean = false;
66+
6167
/** @docs-private */
6268
@ViewChild(TemplateRef) template: TemplateRef<any>;
6369

@@ -97,6 +103,8 @@ export class MatAutocomplete implements AfterContentInit {
97103

98104
ngAfterContentInit() {
99105
this._keyManager = new ActiveDescendantKeyManager<MatOption>(this.options).withWrap();
106+
// Set the initial visibiity state.
107+
this._setVisibility();
100108
}
101109

102110
/**

0 commit comments

Comments
 (0)