@@ -11,7 +11,9 @@ import {
1111 Injectable ,
1212 OnDestroy ,
1313 ViewEncapsulation ,
14- ModuleWithProviders
14+ ModuleWithProviders ,
15+ Optional ,
16+ Inject , SkipSelf , Directive
1517} from '@angular/core' ;
1618import {
1719 NG_VALUE_ACCESSOR ,
@@ -21,6 +23,7 @@ import {
2123import { CommonModule } from '@angular/common' ;
2224import { BooleanProperty } from '../common/boolean-property' ;
2325
26+
2427const noop = ( ) => { } ;
2528const IS_FOCUSED = 'is-focused' ;
2629
@@ -31,28 +34,42 @@ const IS_FOCUSED = 'is-focused';
3134@Injectable ( )
3235export class MdlRadioGroupRegisty {
3336
34- private radioComponents : any [ ] = [ ] ;
37+ private defaultRadioGroup = new MdlRadioGroup ( ) ;
38+ private radioComponents : { radio : MdlRadioComponent , group :MdlRadioGroup } [ ] = [ ] ;
3539
36- public add ( radioComponent : MdlRadioComponent ) {
37- this . radioComponents . push ( radioComponent ) ;
40+ public add ( radioComponent : MdlRadioComponent , mdlRadioGroup : MdlRadioGroup ) {
41+ this . radioComponents . push ( {
42+ radio : radioComponent ,
43+ group : mdlRadioGroup || this . defaultRadioGroup
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 , mdlRadioGroup : MdlRadioGroup ) {
54+ // unselect every radioComponent that is not the provided radiocomponent
55+ // and has the same name and is in teh same group.
56+ let testGroup = mdlRadioGroup || this . defaultRadioGroup ;
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 === testGroup ) {
59+ if ( component . radio !== radioComponent ) {
60+ component . radio . deselect ( radioComponent . value ) ;
5061 }
5162 }
5263 } ) ;
5364 }
5465}
5566
67+ @Directive ( {
68+ selector : '[formGroupName][mdl-radio-group]'
69+ } )
70+ export class MdlRadioGroup {
71+ }
72+
5673/*
5774 <mdl-radio name="group1" value="1" [(ngModel)]="radioOption">Value 1</mdl-radio>
5875 */
@@ -105,7 +122,8 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
105122 constructor (
106123 private elementRef : ElementRef ,
107124 private renderer : Renderer ,
108- private ragioGroupRegisty : MdlRadioGroupRegisty ) {
125+ private ragioGroupRegisty : MdlRadioGroupRegisty ,
126+ @Optional ( ) private mdlRadioGroup : MdlRadioGroup ) {
109127 this . el = elementRef . nativeElement ;
110128 }
111129
@@ -115,7 +133,7 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
115133 this . checkName ( ) ;
116134 // register the radio button - this is the only chance to unselect the
117135 // radio button that is no longer active
118- this . ragioGroupRegisty . add ( this ) ;
136+ this . ragioGroupRegisty . add ( this , this . mdlRadioGroup ) ;
119137 }
120138
121139 public ngOnDestroy ( ) {
@@ -137,7 +155,7 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
137155 // wrap the callback, so that we can call select on the registry
138156 this . onChangeCallback = ( ) => {
139157 fn ( this . value ) ;
140- this . ragioGroupRegisty . select ( this ) ;
158+ this . ragioGroupRegisty . select ( this , this . mdlRadioGroup ) ;
141159 } ;
142160 }
143161
@@ -191,8 +209,8 @@ export class MdlRadioComponent implements ControlValueAccessor, OnInit, OnDestro
191209
192210@NgModule ( {
193211 imports : [ CommonModule , FormsModule ] ,
194- exports : [ MdlRadioComponent ] ,
195- declarations : [ MdlRadioComponent ]
212+ exports : [ MdlRadioComponent , MdlRadioGroup ] ,
213+ declarations : [ MdlRadioComponent , MdlRadioGroup ]
196214} )
197215export class MdlRadioModule {
198216 public static forRoot ( ) : ModuleWithProviders {
0 commit comments