Skip to content

Commit 4ee2980

Browse files
karatinayuangao
authored andcommitted
fix(autocomplete): allow basic use without forms directives (#2958)
1 parent bc52298 commit 4ee2980

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class MdAutocompleteTrigger implements AfterContentInit, ControlValueAcce
8484
private _blurStream = new Subject<any>();
8585

8686
/** View -> model callback called when value changes */
87-
_onChange: (value: any) => {};
87+
_onChange = (value: any) => {};
8888

8989
/** View -> model callback called when autocomplete has been touched */
9090
_onTouched = () => {};

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('MdAutocomplete', () => {
2424
imports: [
2525
MdAutocompleteModule.forRoot(), MdInputModule.forRoot(), ReactiveFormsModule
2626
],
27-
declarations: [SimpleAutocomplete],
27+
declarations: [SimpleAutocomplete, AutocompleteWithoutForms],
2828
providers: [
2929
{provide: OverlayContainer, useFactory: () => {
3030
overlayContainerElement = document.createElement('div');
@@ -790,6 +790,25 @@ describe('MdAutocomplete', () => {
790790

791791
});
792792

793+
describe('misc', () => {
794+
795+
it('should allow basic use without any forms directives', () => {
796+
expect(() => {
797+
const fixture = TestBed.createComponent(AutocompleteWithoutForms);
798+
fixture.detectChanges();
799+
800+
const input = fixture.debugElement.query(By.css('input')).nativeElement;
801+
input.value = 'd';
802+
dispatchEvent('input', input);
803+
fixture.detectChanges();
804+
805+
const options =
806+
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
807+
expect(options.length).toBe(1);
808+
}).not.toThrowError();
809+
});
810+
811+
});
793812
});
794813

795814
@Component({
@@ -849,6 +868,33 @@ class SimpleAutocomplete implements OnDestroy {
849868
}
850869

851870

871+
@Component({
872+
template: `
873+
<md-input-container>
874+
<input mdInput placeholder="State" [mdAutocomplete]="auto"
875+
(input)="onInput($event.target?.value)">
876+
</md-input-container>
877+
878+
<md-autocomplete #auto="mdAutocomplete">
879+
<md-option *ngFor="let state of filteredStates" [value]="state">
880+
<span> {{ state }} </span>
881+
</md-option>
882+
</md-autocomplete>
883+
`
884+
})
885+
class AutocompleteWithoutForms {
886+
filteredStates: any[];
887+
states = ['Alabama', 'California', 'Florida'];
888+
889+
constructor() {
890+
this.filteredStates = this.states.slice();
891+
}
892+
893+
onInput(value: any) {
894+
this.filteredStates = this.states.filter(s => new RegExp(value, 'gi').test(s));
895+
}
896+
897+
}
852898

853899
/**
854900
* TODO: Move this to core testing utility until Angular has event faking

0 commit comments

Comments
 (0)