Skip to content

Commit ad76115

Browse files
committed
wip: proposel solution
1 parent 315a101 commit ad76115

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

src/demo-app/app/reactiveforms/reactiveform.component.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,18 @@ <h4>Reactive Forms Example</h4>
137137
</pre>
138138
<a href="https://gist.github.com/mseemann/39d50d07a191be1df2c2d8f5ea244f7d" target="gist_angular2-mdl">Complete SourceCode</a>
139139
<br/>
140+
141+
<div>
142+
GroupTest:
143+
<form [formGroup]="testForm">
144+
<div formGroupName="group1" mdl-radio-group>
145+
<mdl-radio formControlName="type" value="type1"></mdl-radio>
146+
<mdl-radio formControlName="type" value="type2"></mdl-radio>
147+
</div>
148+
<div formGroupName="group2">
149+
<mdl-radio formControlName="type" value="type1"></mdl-radio>
150+
<mdl-radio formControlName="type" value="type2"></mdl-radio>
151+
</div>
152+
</form>
153+
<br/>
154+
</div>

src/demo-app/app/reactiveforms/reactiveform.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
FormGroup,
99
FormControl,
1010
Validators,
11-
FormBuilder
11+
FormBuilder, Form
1212
} from '@angular/forms';
1313
import 'rxjs/add/operator/filter';
1414
import 'rxjs/add/operator/map';
@@ -40,6 +40,7 @@ export class ReactiveFormsDemo extends AbstractDemoComponent implements OnInit {
4040
public breakfast = new FormControl('Continental');
4141
public toDrink = new FormControl('Tea');
4242

43+
public testForm: FormGroup;
4344

4445
constructor(router: Router, route: ActivatedRoute, titleService: Title, private fb: FormBuilder) {
4546
super(router, route, titleService);
@@ -64,6 +65,16 @@ export class ReactiveFormsDemo extends AbstractDemoComponent implements OnInit {
6465
.subscribe((formValues) => {
6566
console.log(`Model Driven Form valid: ${this.form.valid} value:`, JSON.stringify(formValues));
6667
});
68+
69+
// testform radio buttons inside groups
70+
this.testForm = new FormGroup({
71+
group1: new FormGroup({
72+
type: new FormControl('')
73+
}),
74+
group2: new FormGroup({
75+
type: new FormControl('')
76+
})
77+
});
6778
}
6879

6980
public onSubmit() {

src/lib/components/radio/mdl-radio.component.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
1618
import {
1719
NG_VALUE_ACCESSOR,
@@ -21,6 +23,7 @@ import {
2123
import { CommonModule } from '@angular/common';
2224
import { BooleanProperty } from '../common/boolean-property';
2325

26+
2427
const noop = () => {};
2528
const IS_FOCUSED = 'is-focused';
2629

@@ -31,28 +34,42 @@ const IS_FOCUSED = 'is-focused';
3134
@Injectable()
3235
export 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
})
197215
export class MdlRadioModule {
198216
public static forRoot(): ModuleWithProviders {

0 commit comments

Comments
 (0)