feat(forms): make 'parent' a public property of 'AbstractControl' (#11855)

This commit is contained in:
Dzmitry Shylovich 2016-10-19 19:55:50 +03:00 committed by Alex Rickabaugh
parent b9fc090143
commit 445e5922ec
2 changed files with 19 additions and 12 deletions

View File

@ -11,7 +11,7 @@ import {fromPromise} from 'rxjs/observable/fromPromise';
import {composeAsyncValidators, composeValidators} from './directives/shared'; import {composeAsyncValidators, composeValidators} from './directives/shared';
import {AsyncValidatorFn, ValidatorFn} from './directives/validators'; import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
import {EventEmitter, Observable} from './facade/async'; import {EventEmitter, Observable} from './facade/async';
import {ListWrapper, StringMapWrapper} from './facade/collection'; import {StringMapWrapper} from './facade/collection';
import {isBlank, isPresent, isStringMap, normalizeBool} from './facade/lang'; import {isBlank, isPresent, isStringMap, normalizeBool} from './facade/lang';
import {isPromise} from './private_import_core'; import {isPromise} from './private_import_core';
@ -48,7 +48,7 @@ function _find(control: AbstractControl, path: Array<string|number>| string, del
if (!(path instanceof Array)) { if (!(path instanceof Array)) {
path = (<string>path).split(delimiter); path = (<string>path).split(delimiter);
} }
if (path instanceof Array && ListWrapper.isEmpty(path)) return null; if (path instanceof Array && (path.length === 0)) return null;
return (<Array<string|number>>path).reduce((v, name) => { return (<Array<string|number>>path).reduce((v, name) => {
if (v instanceof FormGroup) { if (v instanceof FormGroup) {
@ -109,6 +109,11 @@ export abstract class AbstractControl {
*/ */
get value(): any { return this._value; } get value(): any { return this._value; }
/**
* The parent control.
*/
get parent(): FormGroup|FormArray { return this._parent; }
/** /**
* The validation status of the control. There are four possible * The validation status of the control. There are four possible
* validation statuses: * validation statuses:
@ -491,7 +496,7 @@ export abstract class AbstractControl {
* If no path is given, it checks for the error on the present control. * If no path is given, it checks for the error on the present control.
*/ */
getError(errorCode: string, path: string[] = null): any { getError(errorCode: string, path: string[] = null): any {
var control = isPresent(path) && !ListWrapper.isEmpty(path) ? this.get(path) : this; const control = isPresent(path) && (path.length > 0) ? this.get(path) : this;
if (isPresent(control) && isPresent(control._errors)) { if (isPresent(control) && isPresent(control._errors)) {
return control._errors[errorCode]; return control._errors[errorCode];
} else { } else {
@ -564,7 +569,7 @@ export abstract class AbstractControl {
/** @internal */ /** @internal */
_anyControlsHaveStatus(status: string): boolean { _anyControlsHaveStatus(status: string): boolean {
return this._anyControls((control: AbstractControl) => control.status == status); return this._anyControls((control: AbstractControl) => control.status === status);
} }
/** @internal */ /** @internal */
@ -1057,7 +1062,7 @@ export class FormGroup extends AbstractControl {
} }
/** @internal */ /** @internal */
_setUpControls() { _setUpControls(): void {
this._forEachChild((control: AbstractControl) => { this._forEachChild((control: AbstractControl) => {
control.setParent(this); control.setParent(this);
control._registerOnCollectionChange(this._onCollectionChange); control._registerOnCollectionChange(this._onCollectionChange);
@ -1065,11 +1070,11 @@ export class FormGroup extends AbstractControl {
} }
/** @internal */ /** @internal */
_updateValue() { this._value = this._reduceValue(); } _updateValue(): void { this._value = this._reduceValue(); }
/** @internal */ /** @internal */
_anyControls(condition: Function): boolean { _anyControls(condition: Function): boolean {
var res = false; let res = false;
this._forEachChild((control: AbstractControl, name: string) => { this._forEachChild((control: AbstractControl, name: string) => {
res = res || (this.contains(name) && condition(control)); res = res || (this.contains(name) && condition(control));
}); });
@ -1089,7 +1094,7 @@ export class FormGroup extends AbstractControl {
/** @internal */ /** @internal */
_reduceChildren(initValue: any, fn: Function) { _reduceChildren(initValue: any, fn: Function) {
var res = initValue; let res = initValue;
this._forEachChild( this._forEachChild(
(control: AbstractControl, name: string) => { res = fn(res, control, name); }); (control: AbstractControl, name: string) => { res = fn(res, control, name); });
return res; return res;
@ -1188,7 +1193,8 @@ export class FormArray extends AbstractControl {
* Insert a new {@link AbstractControl} at the given `index` in the array. * Insert a new {@link AbstractControl} at the given `index` in the array.
*/ */
insert(index: number, control: AbstractControl): void { insert(index: number, control: AbstractControl): void {
ListWrapper.insert(this.controls, index, control); this.controls.splice(index, 0, control);
this._registerControl(control); this._registerControl(control);
this.updateValueAndValidity(); this.updateValueAndValidity();
this._onCollectionChange(); this._onCollectionChange();
@ -1199,7 +1205,7 @@ export class FormArray extends AbstractControl {
*/ */
removeAt(index: number): void { removeAt(index: number): void {
if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {}); if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {});
ListWrapper.removeAt(this.controls, index); this.controls.splice(index, 1);
this.updateValueAndValidity(); this.updateValueAndValidity();
this._onCollectionChange(); this._onCollectionChange();
} }
@ -1209,10 +1215,10 @@ export class FormArray extends AbstractControl {
*/ */
setControl(index: number, control: AbstractControl): void { setControl(index: number, control: AbstractControl): void {
if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {}); if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {});
ListWrapper.removeAt(this.controls, index); this.controls.splice(index, 1);
if (control) { if (control) {
ListWrapper.insert(this.controls, index, control); this.controls.splice(index, 0, control);
this._registerControl(control); this._registerControl(control);
} }

View File

@ -8,6 +8,7 @@ export declare abstract class AbstractControl {
[key: string]: any; [key: string]: any;
}; };
invalid: boolean; invalid: boolean;
parent: FormGroup | FormArray;
pending: boolean; pending: boolean;
pristine: boolean; pristine: boolean;
root: AbstractControl; root: AbstractControl;