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(module:autocomplete): form related bugs #1451

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions components/auto-complete/nz-autocomplete-trigger.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class NzAutocompleteTriggerDirective implements ControlValueAccessor, OnD
}

private setTriggerValue(value: string | number | null): void {
this._element.nativeElement.value = value;
this._element.nativeElement.value = value || '';
}

private doBackfill(): void {
Expand Down Expand Up @@ -318,7 +318,9 @@ export class NzAutocompleteTriggerDirective implements ControlValueAccessor, OnD
return !element.readOnly && !element.disabled;
}

writeValue(obj: {}): void {
// tslint:disable-next-line:no-any
writeValue(value: any): void {
this.setTriggerValue(value);
}

registerOnChange(fn: (value: {}) => {}): void {
Expand All @@ -329,6 +331,12 @@ export class NzAutocompleteTriggerDirective implements ControlValueAccessor, OnD
this._onTouched = fn;
}

setDisabledState(isDisabled: boolean): void {
const element: HTMLInputElement = this._element.nativeElement;
element.disabled = isDisabled;
this.closePanel();
}

ngOnDestroy(): void {
this.destroyPanel();
}
Expand Down
77 changes: 75 additions & 2 deletions components/auto-complete/nz-autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ViewChildren
} from '@angular/core';
import { async, fakeAsync, flush, inject, tick, TestBed } from '@angular/core/testing';
import { FormsModule, FormControl, ReactiveFormsModule } from '@angular/forms';
import { FormsModule, FormBuilder, FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Subject } from 'rxjs/Subject';
Expand Down Expand Up @@ -48,7 +48,8 @@ describe('auto-complete', () => {
NzTestAutocompletePropertyComponent,
NzTestAutocompleteWithoutPanelComponent,
NzTestAutocompleteGroupComponent,
NzTestAutocompleteWithOnPushDelayComponent
NzTestAutocompleteWithOnPushDelayComponent,
NzTestAutocompleteWithFormComponent
],
providers : [
{ provide: Directionality, useFactory: () => ({ value: dir }) },
Expand Down Expand Up @@ -418,6 +419,58 @@ describe('auto-complete', () => {

});

describe('form', () => {
let fixture;
let input: HTMLInputElement;

beforeEach(() => {
fixture = TestBed.createComponent(NzTestAutocompleteWithFormComponent);
fixture.detectChanges();
input = fixture.debugElement.query(By.css('input')).nativeElement;
});

it('should set the value with form', () => {
const componentInstance = fixture.componentInstance;
fixture.detectChanges();
expect(componentInstance.form.get('formControl').value)
.toContain('Burns');
expect(input.value)
.toContain('Burns');
});

it('should set disabled work', () => {
const componentInstance = fixture.componentInstance;
const formControl = (componentInstance.form as FormGroup).get('formControl');
fixture.detectChanges();

expect(input.disabled).toBe(false);

formControl.disable();
fixture.detectChanges();

expect(input.disabled).toBe(true);

});

it('should close the panel when the input is disabled', () => {
const componentInstance = fixture.componentInstance;
const formControl = (componentInstance.form as FormGroup).get('formControl');
fixture.detectChanges();

componentInstance.trigger.openPanel();
fixture.detectChanges();

expect(componentInstance.trigger.panelOpen).toBe(true);

formControl.disable();
fixture.detectChanges();

expect(input.disabled).toBe(true);
expect(componentInstance.trigger.panelOpen).toBe(false);

});
});

describe('option groups', () => {
let fixture;
let input;
Expand Down Expand Up @@ -950,3 +1003,23 @@ class NzTestAutocompleteGroupComponent {

@ViewChild(NzAutocompleteTriggerDirective) trigger: NzAutocompleteTriggerDirective;
}

@Component({
template: `
<form [formGroup]='form'>
<input formControlName="formControl" [nzAutocomplete]="auto"/>
<nz-autocomplete #auto>
<nz-auto-option *ngFor="let option of options" [nzValue]="option">{{option}}</nz-auto-option>
</nz-autocomplete>
</form>
`
})
class NzTestAutocompleteWithFormComponent {
form: FormGroup;
options = ['Burns Bay Road', 'Downing Street', 'Wall Street'];
@ViewChild(NzAutocompleteTriggerDirective) trigger: NzAutocompleteTriggerDirective;

constructor(private fb: FormBuilder) {
this.form = this.fb.group({ formControl: 'Burns' });
}
}