X Tutup
Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)); };
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the value is 0 then value == '' will also be true. Using === instead would fix it.

Copy link
Copy Markdown
Contributor Author

@kara kara Apr 20, 2016

Choose a reason for hiding this comment

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

The value reported comes from the HTML element, so it will always be a string ('0' not 0). Because of that, '0' won't be == to ''. We could use looseIdentity from the Dart facade (=== won't work with Dart), but not sure it's necessary in this case.

That said, I should probably add the '0' case to one of the tests. Thanks for pointing it out :-)

}
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}
47 changes: 47 additions & 0 deletions modules/angular2/test/common/forms/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,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">
Expand Down
X Tutup