Skip to content

Commit cbb5f49

Browse files
committed
fix(autocomplete): unable to click to select items in IE
* Fixes being unable to select autocomplete items by clicking in IE. This was due to IE not setting the `event.relatedTarget` for blur events. * Fixes potential issue if the user uses `mat-option`, instead of `md-option`.
1 parent a4da08b commit cbb5f49

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ export const MD_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
6060
'[attr.aria-expanded]': 'panelOpen.toString()',
6161
'[attr.aria-owns]': 'autocomplete?.id',
6262
'(focus)': 'openPanel()',
63-
'(blur)': '_handleBlur($event.relatedTarget?.tagName)',
6463
'(input)': '_handleInput($event)',
64+
'(focusout)': '_handleFocusOut($event)',
6565
'(keydown)': '_handleKeydown($event)',
6666
},
6767
providers: [MD_AUTOCOMPLETE_VALUE_ACCESSOR]
@@ -223,11 +223,17 @@ export class MdAutocompleteTrigger implements AfterContentInit, ControlValueAcce
223223
}
224224
}
225225

226-
_handleBlur(newlyFocusedTag: string): void {
226+
/**
227+
* Handles the input element losing focus. Note that this should be handled on `focusout`,
228+
* instead of `blur`, because IE doesn't set the `relatedTarget` for blur events.
229+
*/
230+
_handleFocusOut(event: FocusEvent): void {
231+
const relatedTarget = event.relatedTarget as HTMLElement;
232+
227233
this._onTouched();
228234

229-
// Only emit blur event if the new focus is *not* on an option.
230-
if (newlyFocusedTag !== 'MD-OPTION') {
235+
// Only emit blur event if the new focus is *not* on an element inside the panel.
236+
if (relatedTarget && !this._overlayRef.overlayElement.contains(relatedTarget)) {
231237
this._blurStream.next(null);
232238
}
233239
}

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('MdAutocomplete', () => {
9292
fixture.detectChanges();
9393

9494
fixture.whenStable().then(() => {
95-
dispatchEvent('blur', input);
95+
dispatchEvent('focusout', input);
9696
fixture.detectChanges();
9797

9898
expect(fixture.componentInstance.trigger.panelOpen)
@@ -423,7 +423,7 @@ describe('MdAutocomplete', () => {
423423
expect(fixture.componentInstance.stateCtrl.touched)
424424
.toBe(false, `Expected control to start out untouched.`);
425425

426-
dispatchEvent('blur', input);
426+
dispatchEvent('focusout', input);
427427
fixture.detectChanges();
428428

429429
expect(fixture.componentInstance.stateCtrl.touched)

0 commit comments

Comments
 (0)