Skip to content

Commit a763fd6

Browse files
committed
fix(autocomplete): reopening when clicking an option in IE
Fixes an issue that caused IE to reopen the autocomplete panel if the user clicks to select an item. It seems that IE can delay refocusing the input after the panel has been closed, which causes the focus handler to reopen it. Fixes #5165.
1 parent f579ea1 commit a763fd6

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ export const MD_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
6363
*/
6464
export function getMdAutocompleteMissingPanelError(): Error {
6565
return Error('Attempting to open an undefined instance of `md-autocomplete`. ' +
66-
'Make sure that the id passed to the `mdAutocomplete` is correct and that ' +
67-
'you\'re attempting to open it after the ngAfterContentInit hook.');
66+
'Make sure that the id passed to the `mdAutocomplete` is correct and that ' +
67+
'you\'re attempting to open it after the ngAfterContentInit hook.');
6868
}
6969

7070
@Directive({
7171
selector: 'input[mdAutocomplete], input[matAutocomplete],' +
72-
'textarea[mdAutocomplete], textarea[matAutocomplete]',
72+
'textarea[mdAutocomplete], textarea[matAutocomplete]',
7373
host: {
7474
'role': 'combobox',
7575
'autocomplete': 'off',
@@ -78,7 +78,9 @@ export function getMdAutocompleteMissingPanelError(): Error {
7878
'[attr.aria-activedescendant]': 'activeOption?.id',
7979
'[attr.aria-expanded]': 'panelOpen.toString()',
8080
'[attr.aria-owns]': 'autocomplete?.id',
81-
'(focus)': 'openPanel()',
81+
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
82+
// a little earlier. This avoids issues where IE delays the focusing of the input.
83+
'(focusin)': 'openPanel()',
8284
'(input)': '_handleInput($event)',
8385
'(blur)': '_onTouched()',
8486
'(keydown)': '_handleKeydown($event)',

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('MdAutocomplete', () => {
101101
expect(fixture.componentInstance.trigger.panelOpen)
102102
.toBe(false, `Expected panel state to start out closed.`);
103103

104-
dispatchFakeEvent(input, 'focus');
104+
dispatchFakeEvent(input, 'focusin');
105105
fixture.whenStable().then(() => {
106106
fixture.detectChanges();
107107

@@ -147,7 +147,7 @@ describe('MdAutocomplete', () => {
147147
}));
148148

149149
it('should close the panel when input loses focus', async(() => {
150-
dispatchFakeEvent(input, 'focus');
150+
dispatchFakeEvent(input, 'focusin');
151151
fixture.detectChanges();
152152

153153
fixture.whenStable().then(() => {
@@ -161,7 +161,7 @@ describe('MdAutocomplete', () => {
161161
}));
162162

163163
it('should close the panel when an option is clicked', async(() => {
164-
dispatchFakeEvent(input, 'focus');
164+
dispatchFakeEvent(input, 'focusin');
165165
fixture.detectChanges();
166166

167167
fixture.whenStable().then(() => {
@@ -177,7 +177,7 @@ describe('MdAutocomplete', () => {
177177
}));
178178

179179
it('should close the panel when a newly created option is clicked', async(() => {
180-
dispatchFakeEvent(input, 'focus');
180+
dispatchFakeEvent(input, 'focusin');
181181
fixture.detectChanges();
182182

183183
fixture.whenStable().then(() => {
@@ -222,7 +222,7 @@ describe('MdAutocomplete', () => {
222222
});
223223

224224
it('should hide the panel when the options list is empty', async(() => {
225-
dispatchFakeEvent(input, 'focus');
225+
dispatchFakeEvent(input, 'focusin');
226226

227227
fixture.whenStable().then(() => {
228228
fixture.detectChanges();
@@ -1127,7 +1127,7 @@ describe('MdAutocomplete', () => {
11271127
fixture.detectChanges();
11281128

11291129
const input = fixture.debugElement.query(By.css('input')).nativeElement;
1130-
dispatchFakeEvent(input, 'focus');
1130+
dispatchFakeEvent(input, 'focusin');
11311131

11321132
fixture.whenStable().then(() => {
11331133
fixture.detectChanges();
@@ -1226,7 +1226,7 @@ describe('MdAutocomplete', () => {
12261226
let fixture = TestBed.createComponent(AutocompleteWithOnPushDelay);
12271227

12281228
fixture.detectChanges();
1229-
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focus');
1229+
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focusin');
12301230
tick(1000);
12311231
fixture.detectChanges();
12321232

0 commit comments

Comments
 (0)