Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forms): add input[type=number] value accessor
Closes #4014
- Loading branch information
Showing
4 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
modules/angular2/src/core/forms/directives/number_value_accessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {Directive} from 'angular2/src/core/metadata'; | ||
import {ElementRef} from 'angular2/src/core/linker'; | ||
import {Renderer} from 'angular2/src/core/render'; | ||
import {Self, forwardRef, Provider} from 'angular2/src/core/di'; | ||
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from './control_value_accessor'; | ||
import {isBlank, CONST_EXPR, NumberWrapper} from 'angular2/src/core/facade/lang'; | ||
import {setProperty} from './shared'; | ||
|
||
const NUMBER_VALUE_ACCESSOR = CONST_EXPR(new Provider( | ||
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => NumberValueAccessor), multi: true})); | ||
|
||
/** | ||
* The accessor for writing a number value and listening to changes that is used by the | ||
* {@link NgModel}, {@link NgFormControl}, and {@link NgControlName} directives. | ||
* | ||
* # Example | ||
* ``` | ||
* <input type="number" [(ng-model)]="age"> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: | ||
'input[type=number][ng-control],input[type=number][ng-form-control],input[type=number][ng-model]', | ||
host: { | ||
'(change)': 'onChange($event.target.value)', | ||
'(input)': 'onChange($event.target.value)', | ||
'(blur)': 'onTouched()' | ||
}, | ||
bindings: [NUMBER_VALUE_ACCESSOR] | ||
}) | ||
export class NumberValueAccessor implements ControlValueAccessor { | ||
onChange = (_) => {}; | ||
onTouched = () => {}; | ||
|
||
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {} | ||
|
||
writeValue(value: number): void { setProperty(this._renderer, this._elementRef, 'value', value); } | ||
|
||
registerOnChange(fn: (_: number) => void): void { | ||
this.onChange = (value) => { fn(NumberWrapper.parseFloat(value)); }; | ||
} | ||
registerOnTouched(fn: () => void): void { this.onTouched = fn; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters