-
Notifications
You must be signed in to change notification settings - Fork 27.2k
feat(forms): add support for radio buttons #6877
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
126 changes: 126 additions & 0 deletions
126
modules/angular2/src/common/forms/directives/radio_control_value_accessor.ts
This file contains hidden or 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,126 @@ | ||
| import { | ||
| Directive, | ||
| ElementRef, | ||
| Renderer, | ||
| Self, | ||
| forwardRef, | ||
| Provider, | ||
| Attribute, | ||
| Input, | ||
| OnInit, | ||
| OnDestroy, | ||
| Injector, | ||
| Injectable | ||
| } from 'angular2/core'; | ||
| import { | ||
| NG_VALUE_ACCESSOR, | ||
| ControlValueAccessor | ||
| } from 'angular2/src/common/forms/directives/control_value_accessor'; | ||
| import {NgControl} from 'angular2/src/common/forms/directives/ng_control'; | ||
| import {CONST_EXPR, looseIdentical, isPresent} from 'angular2/src/facade/lang'; | ||
| import {ListWrapper} from 'angular2/src/facade/collection'; | ||
|
|
||
| const RADIO_VALUE_ACCESSOR = CONST_EXPR(new Provider( | ||
| NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => RadioControlValueAccessor), multi: true})); | ||
|
|
||
|
|
||
| /** | ||
| * Internal class used by Angular to uncheck radio buttons with the matching name. | ||
| */ | ||
| @Injectable() | ||
| export class RadioControlRegistry { | ||
| private _accessors: any[] = []; | ||
|
|
||
| add(control: NgControl, accessor: RadioControlValueAccessor) { | ||
| this._accessors.push([control, accessor]); | ||
| } | ||
|
|
||
| remove(accessor: RadioControlValueAccessor) { | ||
| var indexToRemove = -1; | ||
| for (var i = 0; i < this._accessors.length; ++i) { | ||
| if (this._accessors[i][1] === accessor) { | ||
| indexToRemove = i; | ||
| } | ||
| } | ||
| ListWrapper.removeAt(this._accessors, indexToRemove); | ||
| } | ||
|
|
||
| select(accessor: RadioControlValueAccessor) { | ||
| this._accessors.forEach((c) => { | ||
| if (c[0].control.root === accessor._control.control.root && c[1] !== accessor) { | ||
| c[1].fireUncheck(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The value provided by the forms API for radio buttons. | ||
| */ | ||
| export class RadioButtonState { | ||
| constructor(public checked: boolean, public value: string) {} | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * The accessor for writing a radio control value and listening to changes that is used by the | ||
| * {@link NgModel}, {@link NgFormControl}, and {@link NgControlName} directives. | ||
| * | ||
| * ### Example | ||
| * ``` | ||
| * @Component({ | ||
| * template: ` | ||
| * <input type="radio" name="food" [(ngModel)]="foodChicken"> | ||
| * <input type="radio" name="food" [(ngModel)]="foodFish"> | ||
| * ` | ||
| * }) | ||
| * class FoodCmp { | ||
| * foodChicken = new RadioButtonState(true, "chicken"); | ||
| * foodFish = new RadioButtonState(false, "fish"); | ||
| * } | ||
| * ``` | ||
| */ | ||
| @Directive({ | ||
| selector: | ||
| 'input[type=radio][ngControl],input[type=radio][ngFormControl],input[type=radio][ngModel]', | ||
| host: {'(change)': 'onChange()', '(blur)': 'onTouched()'}, | ||
| providers: [RADIO_VALUE_ACCESSOR] | ||
| }) | ||
| export class RadioControlValueAccessor implements ControlValueAccessor, | ||
| OnDestroy, OnInit { | ||
| _state: RadioButtonState; | ||
| _control: NgControl; | ||
| @Input() name: string; | ||
| _fn: Function; | ||
| onChange = () => {}; | ||
| onTouched = () => {}; | ||
|
|
||
| constructor(private _renderer: Renderer, private _elementRef: ElementRef, | ||
| private _registry: RadioControlRegistry, private _injector: Injector) {} | ||
|
|
||
| ngOnInit(): void { | ||
| this._control = this._injector.get(NgControl); | ||
| this._registry.add(this._control, this); | ||
| } | ||
|
|
||
| ngOnDestroy(): void { this._registry.remove(this); } | ||
|
|
||
| writeValue(value: any): void { | ||
| this._state = value; | ||
| if (isPresent(value) && value.checked) { | ||
| this._renderer.setElementProperty(this._elementRef.nativeElement, 'checked', true); | ||
| } | ||
| } | ||
|
|
||
| registerOnChange(fn: (_: any) => {}): void { | ||
| this._fn = fn; | ||
| this.onChange = () => { | ||
| fn(new RadioButtonState(true, this._state.value)); | ||
| this._registry.select(this); | ||
| }; | ||
| } | ||
|
|
||
| fireUncheck(): void { this._fn(new RadioButtonState(false, this._state.value)); } | ||
|
|
||
| registerOnTouched(fn: () => {}): void { this.onTouched = fn; } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ var NG_COMMON = [ | |
| 'AbstractControl.validator', | ||
| 'AbstractControl.validator=', | ||
| 'AbstractControl.value', | ||
| 'AbstractControl.root', | ||
| 'AbstractControl.valueChanges', | ||
| 'AbstractControlDirective', | ||
| 'AbstractControlDirective.control', | ||
|
|
@@ -102,6 +103,7 @@ var NG_COMMON = [ | |
| 'Control.validator', | ||
| 'Control.validator=', | ||
| 'Control.value', | ||
| 'Control.root', | ||
| 'Control.valueChanges', | ||
| 'ControlArray', | ||
| 'ControlArray.asyncValidator', | ||
|
|
@@ -134,6 +136,7 @@ var NG_COMMON = [ | |
| 'ControlArray.validator', | ||
| 'ControlArray.validator=', | ||
| 'ControlArray.value', | ||
| 'ControlArray.root', | ||
| 'ControlArray.valueChanges', | ||
| 'ControlContainer', | ||
| 'ControlContainer.control', | ||
|
|
@@ -179,6 +182,7 @@ var NG_COMMON = [ | |
| 'ControlGroup.validator', | ||
| 'ControlGroup.validator=', | ||
| 'ControlGroup.value', | ||
| 'ControlGroup.root', | ||
| 'ControlGroup.valueChanges', | ||
| 'ControlValueAccessor:dart', | ||
| 'CurrencyPipe', | ||
|
|
@@ -447,7 +451,12 @@ var NG_COMMON = [ | |
| 'Validators#maxLength()', | ||
| 'Validators#minLength()', | ||
| 'Validators#nullValidator()', | ||
| 'Validators#required()' | ||
| 'Validators#required()', | ||
| 'RadioButtonState', | ||
| 'RadioButtonState.checked', | ||
| 'RadioButtonState.checked=', | ||
| 'RadioButtonState.value', | ||
| 'RadioButtonState.value=' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add a trailing comma, it'll reduce diff noise in the future. |
||
| ]; | ||
|
|
||
| var NG_COMPILER = [ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to this case where you use the
"change"event, you should also have a case where one radio button becomes deselected by clicking another with the samename(because the deselection won't have an associated event).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added tests covering it