@@ -11,16 +11,19 @@ import {
1111 Injectable ,
1212 OnDestroy ,
1313 ViewEncapsulation ,
14- ModuleWithProviders
14+ ModuleWithProviders ,
15+ Optional
1516} from '@angular/core' ;
1617import {
1718 NG_VALUE_ACCESSOR ,
1819 ControlValueAccessor ,
19- FormsModule
20+ FormsModule ,
21+ FormGroupName
2022} from '@angular/forms' ;
2123import { CommonModule } from '@angular/common' ;
2224import { BooleanProperty } from '../common/boolean-property' ;
2325
26+
2427const noop = ( ) => { } ;
2528const IS_FOCUSED = 'is-focused' ;
2629
@@ -31,22 +34,30 @@ const IS_FOCUSED = 'is-focused';
3134@Injectable ( )
3235export class MdlRadioGroupRegisty {
3336
34- private radioComponents : any [ ] = [ ] ;
37+ private defaultFormGroup = 'defaultFromGroup' ;
38+ private radioComponents : { radio : MdlRadioComponent , group : FormGroupName | string } [ ] = [ ] ;
3539
36- public add ( radioComponent : MdlRadioComponent ) {
37- this . radioComponents . push ( radioComponent ) ;
40+ public add ( radioComponent : MdlRadioComponent , formGroupName : FormGroupName ) {
41+ this . radioComponents . push ( {
42+ radio : radioComponent ,
43+ group : formGroupName || this . defaultFormGroup
44+ } ) ;
3845 }
3946
4047 public remove ( radioComponent : MdlRadioComponent ) {
41- this . radioComponents . slice ( this . radioComponents . indexOf ( radioComponent ) , 1 ) ;
48+ this . radioComponents = this . radioComponents . filter ( ( radioComponentInArray ) => {
49+ return ( radioComponentInArray . radio !== radioComponent ) ;
50+ } ) ;
4251 }
4352
44- public select ( radioComponent : MdlRadioComponent ) {
45- // unselect evenry radioComponent that is not the provided radiocomponent and has the same name
53+ public select ( radioComponent : MdlRadioComponent , formGroupName : FormGroupName ) {
54+ // unselect every radioComponent that is not the provided radiocomponent
55+ // and has the same name and is in teh same group.
56+ let groupToTest = formGroupName || this . defaultFormGroup ;
4657 this . radioComponents . forEach ( ( component ) => {
47- if ( component . name === radioComponent . name ) {
48- if ( component !== radioComponent ) {
49- component . deselect ( radioComponent . value ) ;
58+ if ( component . radio . name === radioComponent . name && component . group === groupToTest ) {
59+ if ( component . radio !== radioComponent ) {
60+ component . radio . deselect ( radioComponent . value ) ;
5061 }
5162 }
5263 } ) ;
@@ -105,7 +116,8 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
105116 constructor (
106117 private elementRef : ElementRef ,
107118 private renderer : Renderer ,
108- private ragioGroupRegisty : MdlRadioGroupRegisty ) {
119+ private ragioGroupRegisty : MdlRadioGroupRegisty ,
120+ @Optional ( ) private formGroupName : FormGroupName ) {
109121 this . el = elementRef . nativeElement ;
110122 }
111123
@@ -114,8 +126,9 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
114126 // a radio group without name is useless.
115127 this . checkName ( ) ;
116128 // register the radio button - this is the only chance to unselect the
117- // radio button that is no longer active
118- this . ragioGroupRegisty . add ( this ) ;
129+ // radio button that is no longer active - scope the radio button with it's group
130+ // if there is one.
131+ this . ragioGroupRegisty . add ( this , this . formGroupName ) ;
119132 }
120133
121134 public ngOnDestroy ( ) {
@@ -137,7 +150,7 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
137150 // wrap the callback, so that we can call select on the registry
138151 this . onChangeCallback = ( ) => {
139152 fn ( this . value ) ;
140- this . ragioGroupRegisty . select ( this ) ;
153+ this . ragioGroupRegisty . select ( this , this . formGroupName ) ;
141154 } ;
142155 }
143156
0 commit comments