Skip to content

Commit

Permalink
fix(autocomplete): reopening when clicking an option in IE (#5172)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and jelbourn committed Jun 22, 2017
1 parent bdf0286 commit fe31210
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/lib/autocomplete/autocomplete-trigger.ts
Expand Up @@ -64,13 +64,13 @@ export const MD_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
*/
export function getMdAutocompleteMissingPanelError(): Error {
return Error('Attempting to open an undefined instance of `md-autocomplete`. ' +
'Make sure that the id passed to the `mdAutocomplete` is correct and that ' +
'you\'re attempting to open it after the ngAfterContentInit hook.');
'Make sure that the id passed to the `mdAutocomplete` is correct and that ' +
'you\'re attempting to open it after the ngAfterContentInit hook.');
}

@Directive({
selector: 'input[mdAutocomplete], input[matAutocomplete],' +
'textarea[mdAutocomplete], textarea[matAutocomplete]',
'textarea[mdAutocomplete], textarea[matAutocomplete]',
host: {
'role': 'combobox',
'autocomplete': 'off',
Expand All @@ -79,7 +79,9 @@ export function getMdAutocompleteMissingPanelError(): Error {
'[attr.aria-activedescendant]': 'activeOption?.id',
'[attr.aria-expanded]': 'panelOpen.toString()',
'[attr.aria-owns]': 'autocomplete?.id',
'(focus)': 'openPanel()',
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
// a little earlier. This avoids issues where IE delays the focusing of the input.
'(focusin)': 'openPanel()',
'(input)': '_handleInput($event)',
'(blur)': '_onTouched()',
'(keydown)': '_handleKeydown($event)',
Expand Down
14 changes: 7 additions & 7 deletions src/lib/autocomplete/autocomplete.spec.ts
Expand Up @@ -101,7 +101,7 @@ describe('MdAutocomplete', () => {
expect(fixture.componentInstance.trigger.panelOpen)
.toBe(false, `Expected panel state to start out closed.`);

dispatchFakeEvent(input, 'focus');
dispatchFakeEvent(input, 'focusin');
fixture.whenStable().then(() => {
fixture.detectChanges();

Expand Down Expand Up @@ -147,7 +147,7 @@ describe('MdAutocomplete', () => {
}));

it('should close the panel when input loses focus', async(() => {
dispatchFakeEvent(input, 'focus');
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();

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

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

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

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

fixture.whenStable().then(() => {
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('MdAutocomplete', () => {
});

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

fixture.whenStable().then(() => {
fixture.detectChanges();
Expand Down Expand Up @@ -1127,7 +1127,7 @@ describe('MdAutocomplete', () => {
fixture.detectChanges();

const input = fixture.debugElement.query(By.css('input')).nativeElement;
dispatchFakeEvent(input, 'focus');
dispatchFakeEvent(input, 'focusin');

fixture.whenStable().then(() => {
fixture.detectChanges();
Expand Down Expand Up @@ -1226,7 +1226,7 @@ describe('MdAutocomplete', () => {
let fixture = TestBed.createComponent(AutocompleteWithOnPushDelay);

fixture.detectChanges();
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focus');
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focusin');
tick(1000);
fixture.detectChanges();

Expand Down

0 comments on commit fe31210

Please sign in to comment.