Skip to content

Commit fe31210

Browse files
crisbetojelbourn
authored andcommitted
fix(autocomplete): reopening when clicking an option in IE (#5172)
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 bdf0286 commit fe31210

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
@@ -64,13 +64,13 @@ export const MD_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
6464
*/
6565
export function getMdAutocompleteMissingPanelError(): Error {
6666
return Error('Attempting to open an undefined instance of `md-autocomplete`. ' +
67-
'Make sure that the id passed to the `mdAutocomplete` is correct and that ' +
68-
'you\'re attempting to open it after the ngAfterContentInit hook.');
67+
'Make sure that the id passed to the `mdAutocomplete` is correct and that ' +
68+
'you\'re attempting to open it after the ngAfterContentInit hook.');
6969
}
7070

7171
@Directive({
7272
selector: 'input[mdAutocomplete], input[matAutocomplete],' +
73-
'textarea[mdAutocomplete], textarea[matAutocomplete]',
73+
'textarea[mdAutocomplete], textarea[matAutocomplete]',
7474
host: {
7575
'role': 'combobox',
7676
'autocomplete': 'off',
@@ -79,7 +79,9 @@ export function getMdAutocompleteMissingPanelError(): Error {
7979
'[attr.aria-activedescendant]': 'activeOption?.id',
8080
'[attr.aria-expanded]': 'panelOpen.toString()',
8181
'[attr.aria-owns]': 'autocomplete?.id',
82-
'(focus)': 'openPanel()',
82+
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
83+
// a little earlier. This avoids issues where IE delays the focusing of the input.
84+
'(focusin)': 'openPanel()',
8385
'(input)': '_handleInput($event)',
8486
'(blur)': '_onTouched()',
8587
'(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)