Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): Allow NonNullableFormBuilder to be injected. #45904

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions goldens/public-api/forms/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,16 @@ export class NgSelectOption implements OnDestroy {
}

// @public
export interface NonNullableFormBuilder {
array<T>(controls: Array<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArrayElement<T, never>>;
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl<T>;
group<T extends {}>(controls: T, options?: AbstractControlOptions | null): FormGroup<{
export abstract class NonNullableFormBuilder {
abstract array<T>(controls: Array<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArrayElement<T, never>>;
abstract control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl<T>;
abstract group<T extends {}>(controls: T, options?: AbstractControlOptions | null): FormGroup<{
[K in keyof T]: ɵElement<T[K], never>;
}>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<NonNullableFormBuilder, never>;
// (undocumented)
static ɵprov: i0.ɵɵInjectableDeclaration<NonNullableFormBuilder>;
}

// @public
Expand Down
14 changes: 9 additions & 5 deletions packages/forms/src/form_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable} from '@angular/core';
import {inject, Injectable, InjectionToken} from '@angular/core';

import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
import {ReactiveFormsModule} from './form_providers';
Expand Down Expand Up @@ -315,13 +315,17 @@ export class FormBuilder {
*
* @publicApi
*/
export interface NonNullableFormBuilder {
@Injectable({
providedIn: ReactiveFormsModule,
useFactory: () => inject(FormBuilder).nonNullable,
})
export abstract class NonNullableFormBuilder {
/**
* Similar to {@see FormBuilder#group}, except any implicitly constructed `FormControl`
* will be non-nullable (i.e. it will have `initialValueIsDefault` set to true). Note
* that already-constructed controls will not be altered.
*/
group<T extends {}>(
abstract group<T extends {}>(
controls: T,
options?: AbstractControlOptions|null,
): FormGroup<{[K in keyof T]: ɵElement<T[K], never>}>;
Expand All @@ -331,15 +335,15 @@ export interface NonNullableFormBuilder {
* will be non-nullable (i.e. it will have `initialValueIsDefault` set to true). Note
* that already-constructed controls will not be altered.
*/
array<T>(
abstract array<T>(
controls: Array<T>, validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormArray<ɵElement<T, never>>;

/**
* Similar to {@see FormBuilder#control}, except this overridden version of `control` forces
* `initialValueIsDefault` to be `true`, resulting in the control always being non-nullable.
*/
control<T>(
abstract control<T>(
formState: T|FormControlState<T>,
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl<T>;
Expand Down
59 changes: 57 additions & 2 deletions packages/forms/test/form_builder_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {fakeAsync, tick} from '@angular/core/testing';
import {FormBuilder, UntypedFormBuilder, Validators} from '@angular/forms';
import {Component} from '@angular/core';
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
import {FormBuilder, NonNullableFormBuilder, ReactiveFormsModule, UntypedFormBuilder, Validators} from '@angular/forms';
import {of} from 'rxjs';

(function() {
Expand Down Expand Up @@ -197,6 +198,60 @@ describe('Form Builder', () => {
expect(a.errors).toEqual({'sync1': true, 'sync2': true});
});

it('should be injectable', () => {
@Component({
standalone: true,
template: '...',
})
class MyComp {
constructor(public fb: FormBuilder) {}
}

TestBed.configureTestingModule({imports: [ReactiveFormsModule]});
const fixture = TestBed.createComponent(MyComp);

fixture.detectChanges();
expect(fixture.componentInstance.fb).toBeInstanceOf(FormBuilder);

const fc = fixture.componentInstance.fb.control('foo');
{
// Check the type of the value by assigning in each direction
type ValueType = string|null;
let t: ValueType = fc.value;
let t1 = fc.value;
t1 = null as unknown as ValueType;
}
fc.reset();
expect(fc.value).toEqual(null);
});

it('should be injectable as NonNullableFormBuilder', () => {
@Component({
standalone: true,
template: '...',
})
class MyComp {
constructor(public fb: NonNullableFormBuilder) {}
}

TestBed.configureTestingModule({imports: [ReactiveFormsModule]});

const fixture = TestBed.createComponent(MyComp);
fixture.detectChanges();
expect(fixture.componentInstance.fb).toBeInstanceOf(FormBuilder);

const fc = fixture.componentInstance.fb.control('foo');
{
// Check the type of the value by assigning in each direction
type ValueType = string;
let t: ValueType = fc.value;
let t1 = fc.value;
t1 = null as unknown as ValueType;
}
fc.reset();
expect(fc.value).toEqual('foo');
});

describe('updateOn', () => {
it('should default to on change', () => {
const c = b.control('');
Expand Down