fix(forms): number input should report null when blank

Closes #6932

Closes #8141
This commit is contained in:
Kara Erickson 2016-04-20 07:13:19 -07:00 committed by Kara
parent 12837e1c17
commit e69cb40de3
2 changed files with 48 additions and 1 deletions

View File

@ -35,7 +35,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
}
registerOnChange(fn: (_: number) => void): void {
this.onChange = (value) => { fn(NumberWrapper.parseFloat(value)); };
this.onChange = (value) => { fn(value == '' ? null : NumberWrapper.parseFloat(value)); };
}
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}

View File

@ -343,6 +343,53 @@ export function main() {
});
}));
it("should support <type=number> when value is cleared in the UI",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `<div [ngFormModel]="form">
<input type="number" ngControl="num" required>
</div>`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((fixture) => {
fixture.debugElement.componentInstance.form =
new ControlGroup({"num": new Control(10)});
fixture.detectChanges();
var input = fixture.debugElement.query(By.css("input"));
input.nativeElement.value = "";
dispatchEvent(input.nativeElement, "input");
expect(fixture.debugElement.componentInstance.form.valid).toBe(false);
expect(fixture.debugElement.componentInstance.form.value).toEqual({"num": null});
input.nativeElement.value = "0";
dispatchEvent(input.nativeElement, "input");
expect(fixture.debugElement.componentInstance.form.valid).toBe(true);
expect(fixture.debugElement.componentInstance.form.value).toEqual({"num": 0});
async.done();
});
}));
it("should support <type=number> when value is cleared programmatically",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var form = new ControlGroup({"num": new Control(10)});
var t = `<div [ngFormModel]="form">
<input type="number" ngControl="num" [(ngModel)]="data">
</div>`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((fixture) => {
fixture.debugElement.componentInstance.form = form;
fixture.debugElement.componentInstance.data = null;
fixture.detectChanges();
var input = fixture.debugElement.query(By.css("input"));
expect(input.nativeElement.value).toEqual("");
async.done();
});
}));
it("should support <type=radio>",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `<form [ngFormModel]="form">