feat(forms): formGroupName and formArrayName also accepts a number (#32607)

For consistency, `FormGroupName` and `FormaArrayName` also accepts a
number as input's type like `FormControlName`

Closes https://github.com/angular/angular/issues/32436

PR Close #32607
This commit is contained in:
LASLEDJ 2019-09-10 22:39:47 +02:00 committed by Alex Rickabaugh
parent 3efb060127
commit fee28e20bb
4 changed files with 29 additions and 17 deletions

View File

@ -82,7 +82,9 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
* @description * @description
* The path to this group from the top-level directive. * The path to this group from the top-level directive.
*/ */
get path(): string[] { return controlPath(this.name, this._parent); } get path(): string[] {
return controlPath(this.name == null ? this.name : this.name.toString(), this._parent);
}
/** /**
* @description * @description

View File

@ -23,7 +23,7 @@ export abstract class ControlContainer extends AbstractControlDirective {
* The name for the control * The name for the control
*/ */
// TODO(issue/24571): remove '!'. // TODO(issue/24571): remove '!'.
name !: string; name !: string | number | null;
/** /**
* @description * @description

View File

@ -37,23 +37,23 @@ export const formGroupNameProvider: any = {
* Use nested form groups to validate a sub-group of a * Use nested form groups to validate a sub-group of a
* form separately from the rest or to group the values of certain * form separately from the rest or to group the values of certain
* controls into their own nested object. * controls into their own nested object.
* *
* @see [Reactive Forms Guide](guide/reactive-forms) * @see [Reactive Forms Guide](guide/reactive-forms)
* *
* @usageNotes * @usageNotes
* *
* ### Access the group by name * ### Access the group by name
* *
* The following example uses the {@link AbstractControl#get get} method to access the * The following example uses the {@link AbstractControl#get get} method to access the
* associated `FormGroup` * associated `FormGroup`
* *
* ```ts * ```ts
* this.form.get('name'); * this.form.get('name');
* ``` * ```
* *
* ### Access individual controls in the group * ### Access individual controls in the group
* *
* The following example uses the {@link AbstractControl#get get} method to access * The following example uses the {@link AbstractControl#get get} method to access
* individual controls within the group using dot syntax. * individual controls within the group using dot syntax.
* *
* ```ts * ```ts
@ -61,7 +61,7 @@ export const formGroupNameProvider: any = {
* ``` * ```
* *
* ### Register a nested `FormGroup`. * ### Register a nested `FormGroup`.
* *
* The following example registers a nested *name* `FormGroup` within an existing `FormGroup`, * The following example registers a nested *name* `FormGroup` within an existing `FormGroup`,
* and provides methods to retrieve the nested `FormGroup` and individual controls. * and provides methods to retrieve the nested `FormGroup` and individual controls.
* *
@ -76,9 +76,13 @@ export class FormGroupName extends AbstractFormGroupDirective implements OnInit,
* @description * @description
* Tracks the name of the `FormGroup` bound to the directive. The name corresponds * Tracks the name of the `FormGroup` bound to the directive. The name corresponds
* to a key in the parent `FormGroup` or `FormArray`. * to a key in the parent `FormGroup` or `FormArray`.
* Accepts a name as a string or a number.
* The name in the form of a string is useful for individual forms,
* while the numerical form allows for form groups to be bound
* to indices when iterating over groups in a `FormArray`.
*/ */
// TODO(issue/24571): remove '!'. // TODO(issue/24571): remove '!'.
@Input('formGroupName') name !: string; @Input('formGroupName') name !: string | number | null;
constructor( constructor(
@Optional() @Host() @SkipSelf() parent: ControlContainer, @Optional() @Host() @SkipSelf() parent: ControlContainer,
@ -114,7 +118,7 @@ export const formArrayNameProvider: any = {
* It accepts the string name of the nested `FormArray` you want to link, and * It accepts the string name of the nested `FormArray` you want to link, and
* will look for a `FormArray` registered with that name in the parent * will look for a `FormArray` registered with that name in the parent
* `FormGroup` instance you passed into `FormGroupDirective`. * `FormGroup` instance you passed into `FormGroupDirective`.
* *
* @see [Reactive Forms Guide](guide/reactive-forms) * @see [Reactive Forms Guide](guide/reactive-forms)
* @see `AbstractControl` * @see `AbstractControl`
* *
@ -142,9 +146,13 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
* @description * @description
* Tracks the name of the `FormArray` bound to the directive. The name corresponds * Tracks the name of the `FormArray` bound to the directive. The name corresponds
* to a key in the parent `FormGroup` or `FormArray`. * to a key in the parent `FormGroup` or `FormArray`.
* Accepts a name as a string or a number.
* The name in the form of a string is useful for individual forms,
* while the numerical form allows for form arrays to be bound
* to indices when iterating over arrays in a `FormArray`.
*/ */
// TODO(issue/24571): remove '!'. // TODO(issue/24571): remove '!'.
@Input('formArrayName') name !: string; @Input('formArrayName') name !: string | number | null;
constructor( constructor(
@Optional() @Host() @SkipSelf() parent: ControlContainer, @Optional() @Host() @SkipSelf() parent: ControlContainer,
@ -196,7 +204,9 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
* Returns an array that represents the path from the top-level form to this control. * Returns an array that represents the path from the top-level form to this control.
* Each index is the string name of the control on that level. * Each index is the string name of the control on that level.
*/ */
get path(): string[] { return controlPath(this.name, this._parent); } get path(): string[] {
return controlPath(this.name == null ? this.name : this.name.toString(), this._parent);
}
/** /**
* @description * @description

View File

@ -128,7 +128,7 @@ export declare const COMPOSITION_BUFFER_MODE: InjectionToken<boolean>;
export declare abstract class ControlContainer extends AbstractControlDirective { export declare abstract class ControlContainer extends AbstractControlDirective {
readonly formDirective: Form | null; readonly formDirective: Form | null;
name: string; name: string | number | null;
readonly path: string[] | null; readonly path: string[] | null;
} }
@ -194,7 +194,7 @@ export declare class FormArrayName extends ControlContainer implements OnInit, O
readonly asyncValidator: AsyncValidatorFn | null; readonly asyncValidator: AsyncValidatorFn | null;
readonly control: FormArray; readonly control: FormArray;
readonly formDirective: FormGroupDirective | null; readonly formDirective: FormGroupDirective | null;
name: string; name: string | number | null;
readonly path: string[]; readonly path: string[];
readonly validator: ValidatorFn | null; readonly validator: ValidatorFn | null;
constructor(parent: ControlContainer, validators: any[], asyncValidators: any[]); constructor(parent: ControlContainer, validators: any[], asyncValidators: any[]);
@ -322,7 +322,7 @@ export declare class FormGroupDirective extends ControlContainer implements Form
} }
export declare class FormGroupName extends AbstractFormGroupDirective implements OnInit, OnDestroy { export declare class FormGroupName extends AbstractFormGroupDirective implements OnInit, OnDestroy {
name: string; name: string | number | null;
constructor(parent: ControlContainer, validators: any[], asyncValidators: any[]); constructor(parent: ControlContainer, validators: any[], asyncValidators: any[]);
} }