Remove FormBuilder dependency; rname factory methods
* Update factory methods to use the FormGroup(), FormArray(), and FormControl() constructors instead of the FormBuilder service. This avoids needing to pass the service into each of the static factory methods. * Rename the factory methods to `buildForm`. The consistent use of the same name establishes a pattern and makes it clearer that the factory methods have the same purpose -- to create the (sub)form for the specific component.master
parent
cb8144044e
commit
b7870b607b
|
@ -1,5 +1,5 @@
|
|||
import { Component, Input, OnInit } from '@angular/core';
|
||||
import { FormArray, FormBuilder } from '@angular/forms';
|
||||
import { FormArray } from '@angular/forms';
|
||||
import { Address } from '@nested-forms/contact';
|
||||
import { AddressComponent } from '../address/address.component';
|
||||
|
||||
|
@ -11,12 +11,12 @@ import { AddressComponent } from '../address/address.component';
|
|||
export class AddressListComponent implements OnInit {
|
||||
@Input() addressArray: FormArray;
|
||||
|
||||
static createContactAddressListForm(addresses: Address[], fb: FormBuilder): FormArray {
|
||||
const list: FormArray = fb.array([]);
|
||||
static buildForm(addresses: Address[]): FormArray {
|
||||
const list: FormArray = new FormArray([]);
|
||||
|
||||
if (addresses) {
|
||||
addresses.forEach(addr => {
|
||||
list.push(AddressComponent.createContactAddressForm(addr, fb));
|
||||
list.push(AddressComponent.buildForm(addr));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Component, OnInit, Input } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { FormGroup, FormControl } from '@angular/forms';
|
||||
import { Address } from '@nested-forms/contact';
|
||||
|
||||
@Component({
|
||||
|
@ -10,13 +10,13 @@ import { Address } from '@nested-forms/contact';
|
|||
export class AddressComponent implements OnInit {
|
||||
@Input() addressGroup: FormGroup;
|
||||
|
||||
static createContactAddressForm(addr: Address, fb: FormBuilder): FormGroup {
|
||||
return fb.group({
|
||||
line1: [addr ? addr.line1 : ''],
|
||||
line2: [addr ? addr.line2 : ''],
|
||||
city: [addr ? addr.city : ''],
|
||||
state: [addr ? addr.state : ''],
|
||||
postalCode: [addr ? addr.postalCode : ''],
|
||||
static buildForm(addr: Address): FormGroup {
|
||||
return new FormGroup({
|
||||
line1: new FormControl(addr ? addr.line1 : ''),
|
||||
line2: new FormControl(addr ? addr.line2 : ''),
|
||||
city: new FormControl(addr ? addr.city : ''),
|
||||
state: new FormControl(addr ? addr.state : ''),
|
||||
postalCode: new FormControl(addr ? addr.postalCode : ''),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ export class AppComponent implements OnInit, OnDestroy {
|
|||
.subscribe((data: Contact) => {
|
||||
this.contact = data;
|
||||
this.form = this.fb.group({
|
||||
name: NameComponent.createContactNameForm(data.name, this.fb),
|
||||
addresses: AddressListComponent.createContactAddressListForm(data.addresses, this.fb),
|
||||
name: NameComponent.buildForm(data.name),
|
||||
addresses: AddressListComponent.buildForm(data.addresses),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { FormControl, FormGroup } from '@angular/forms';
|
||||
import { Component, OnInit, Input } from '@angular/core';
|
||||
import { Name } from '@nested-forms/contact';
|
||||
|
||||
|
@ -10,14 +10,14 @@ import { Name } from '@nested-forms/contact';
|
|||
export class NameComponent implements OnInit {
|
||||
@Input() nameGroup: FormGroup;
|
||||
|
||||
static createContactNameForm(name: Name, fb: FormBuilder): FormGroup {
|
||||
return fb.group({
|
||||
firstName: [name ? name.firstName : ''],
|
||||
lastName: [name ? name.lastName : ''],
|
||||
middleName: [name ? name.middleName : ''],
|
||||
prefix: [name ? name.prefix : ''],
|
||||
suffix: [name ? name.suffix : ''],
|
||||
})
|
||||
static buildForm(name: Name): FormGroup {
|
||||
return new FormGroup({
|
||||
firstName: new FormControl(name ? name.firstName : ''),
|
||||
lastName: new FormControl(name ? name.lastName : ''),
|
||||
middleName: new FormControl(name ? name.middleName : ''),
|
||||
prefix: new FormControl(name ? name.prefix : ''),
|
||||
suffix: new FormControl(name ? name.suffix : ''),
|
||||
});
|
||||
}
|
||||
|
||||
constructor() {}
|
||||
|
|
Loading…
Reference in New Issue