fix(forms): typed argument for FormBuilder group (#26985)

PR Close #26985
This commit is contained in:
cexbrayat 2018-11-07 20:38:07 +01:00 committed by Alex Rickabaugh
parent 2a35471abe
commit b0c75611d6
2 changed files with 23 additions and 14 deletions

View File

@ -11,6 +11,13 @@ import {Injectable} from '@angular/core';
import {AsyncValidatorFn, ValidatorFn} from './directives/validators'; import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
import {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormGroup, FormHooks} from './model'; import {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormGroup, FormHooks} from './model';
function isAbstractControlOptions(options: AbstractControlOptions | {[key: string]: any}):
options is AbstractControlOptions {
return (<AbstractControlOptions>options).asyncValidators !== undefined ||
(<AbstractControlOptions>options).validators !== undefined ||
(<AbstractControlOptions>options).updateOn !== undefined;
}
/** /**
* @description * @description
* Creates an `AbstractControl` from a user-specified configuration. * Creates an `AbstractControl` from a user-specified configuration.
@ -32,7 +39,7 @@ export class FormBuilder {
* @param controlsConfig A collection of child controls. The key for each child is the name * @param controlsConfig A collection of child controls. The key for each child is the name
* under which it is registered. * under which it is registered.
* *
* @param legacyOrOpts Configuration options object for the `FormGroup`. The object can * @param options Configuration options object for the `FormGroup`. The object can
* have two shapes: * have two shapes:
* *
* 1) `AbstractControlOptions` object (preferred), which consists of: * 1) `AbstractControlOptions` object (preferred), which consists of:
@ -46,24 +53,26 @@ export class FormBuilder {
* * `asyncValidator`: A single async validator or array of async validator functions * * `asyncValidator`: A single async validator or array of async validator functions
* *
*/ */
group(controlsConfig: {[key: string]: any}, legacyOrOpts: {[key: string]: any}|null = null): group(
FormGroup { controlsConfig: {[key: string]: any},
options: AbstractControlOptions|{[key: string]: any}|null = null): FormGroup {
const controls = this._reduceControls(controlsConfig); const controls = this._reduceControls(controlsConfig);
let validators: ValidatorFn|ValidatorFn[]|null = null; let validators: ValidatorFn|ValidatorFn[]|null = null;
let asyncValidators: AsyncValidatorFn|AsyncValidatorFn[]|null = null; let asyncValidators: AsyncValidatorFn|AsyncValidatorFn[]|null = null;
let updateOn: FormHooks|undefined = undefined; let updateOn: FormHooks|undefined = undefined;
if (legacyOrOpts != null && if (options != null) {
(legacyOrOpts.asyncValidator !== undefined || legacyOrOpts.validator !== undefined)) { if (isAbstractControlOptions(options)) {
// `legacyOrOpts` are legacy form group options // `options` are `AbstractControlOptions`
validators = legacyOrOpts.validator != null ? legacyOrOpts.validator : null; validators = options.validators != null ? options.validators : null;
asyncValidators = legacyOrOpts.asyncValidator != null ? legacyOrOpts.asyncValidator : null; asyncValidators = options.asyncValidators != null ? options.asyncValidators : null;
} else if (legacyOrOpts != null) { updateOn = options.updateOn != null ? options.updateOn : undefined;
// `legacyOrOpts` are `AbstractControlOptions` } else {
validators = legacyOrOpts.validators != null ? legacyOrOpts.validators : null; // `options` are legacy form group options
asyncValidators = legacyOrOpts.asyncValidators != null ? legacyOrOpts.asyncValidators : null; validators = options.validator != null ? options.validator : null;
updateOn = legacyOrOpts.updateOn != null ? legacyOrOpts.updateOn : undefined; asyncValidators = options.asyncValidator != null ? options.asyncValidator : null;
}
} }
return new FormGroup(controls, {asyncValidators, updateOn, validators}); return new FormGroup(controls, {asyncValidators, updateOn, validators});

View File

@ -205,7 +205,7 @@ export declare class FormBuilder {
control(formState: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl; control(formState: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl;
group(controlsConfig: { group(controlsConfig: {
[key: string]: any; [key: string]: any;
}, legacyOrOpts?: { }, options?: AbstractControlOptions | {
[key: string]: any; [key: string]: any;
} | null): FormGroup; } | null): FormGroup;
} }