Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autocomplete): error when clicking outside instance without mdInput #4573

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,13 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
private get _outsideClickStream(): Observable<any> {
if (this._document) {
return Observable.fromEvent(this._document, 'click').filter((event: MouseEvent) => {
let clickTarget = event.target as HTMLElement;
const clickTarget = event.target as HTMLElement;
const inputContainer = this._inputContainer ?
this._inputContainer._elementRef.nativeElement : null;

return this._panelOpen &&
!this._inputContainer._elementRef.nativeElement.contains(clickTarget) &&
clickTarget !== this._element.nativeElement &&
(!inputContainer || !inputContainer.contains(clickTarget)) &&
!this._overlayRef.overlayElement.contains(clickTarget);
});
}
Expand Down
48 changes: 47 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('MdAutocomplete', () => {
AutocompleteWithoutForms,
NgIfAutocomplete,
AutocompleteWithNgModel,
AutocompleteWithOnPushDelay
AutocompleteWithOnPushDelay,
AutocompleteWithNativeInput
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -1065,6 +1066,24 @@ describe('MdAutocomplete', () => {
}));
});

describe('without mdInput', () => {
let fixture: ComponentFixture<AutocompleteWithNativeInput>;

beforeEach(() => {
fixture = TestBed.createComponent(AutocompleteWithNativeInput);
fixture.detectChanges();
});

it('should not throw when clicking outside', async(() => {
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focus');
fixture.detectChanges();

fixture.whenStable().then(() => {
expect(() => dispatchFakeEvent(document, 'click')).not.toThrow();
});
}));
});

describe('misc', () => {

it('should allow basic use without any forms directives', () => {
Expand Down Expand Up @@ -1373,6 +1392,33 @@ class AutocompleteWithOnPushDelay implements OnInit {
}
}

@Component({
template: `
<input mdInput placeholder="Choose" [mdAutocomplete]="auto" [formControl]="optionCtrl">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove mdInput here?


<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option}}
</md-option>
</md-autocomplete>
`
})
class AutocompleteWithNativeInput {
optionCtrl = new FormControl();
filteredOptions: Observable<any>;
options = ['En', 'To', 'Tre', 'Fire', 'Fem'];

@ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger;
@ViewChildren(MdOption) mdOptions: QueryList<MdOption>;

constructor() {
this.filteredOptions = this.optionCtrl.valueChanges.startWith(null).map((val) => {
return val ? this.options.filter(option => new RegExp(val, 'gi').test(option))
: this.options.slice();
});
}
}


/** This is a mock keyboard event to test keyboard events in the autocomplete. */
class MockKeyboardEvent {
Expand Down