Skip to content

Commit

Permalink
refactor(forms): Move FormControl to an overridden constructor.
Browse files Browse the repository at this point in the history
This implementation change was originally proposed as part of Typed Forms, and will have major consequences for that project as described in the design doc. Submitting it separately will greatly simplify the risk of landing Typed Forms. This change should have no visible impact on normal users of FormControl.

See the Typed Forms design doc here: https://docs.google.com/document/d/1cWuBE-oo5WLtwkLFxbNTiaVQGNk8ipgbekZcKBeyxxo.
  • Loading branch information
dylhunn committed Nov 30, 2021
1 parent 24baa5e commit 789c9aa
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 5 deletions.
6 changes: 4 additions & 2 deletions goldens/public-api/forms/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ export class FormBuilder {
}

// @public
export class FormControl extends AbstractControl {
constructor(formState?: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null);
export interface FormControl extends AbstractControl {
patchValue(value: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
Expand All @@ -315,6 +314,9 @@ export class FormControl extends AbstractControl {
}): void;
}

// @public (undocumented)
export const FormControl: FormControlCtor;

// @public
export class FormControlDirective extends NgControl implements OnChanges, OnDestroy {
constructor(validators: (Validator | ValidatorFn)[], asyncValidators: (AsyncValidator | AsyncValidatorFn)[], valueAccessors: ControlValueAccessor[], _ngModelWarningConfig: string | null);
Expand Down
137 changes: 135 additions & 2 deletions packages/forms/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,6 @@ export abstract class AbstractControl {
this._updateOn = opts.updateOn!;
}
}

/**
* Check to see if parent has been marked artificially dirty.
*
Expand Down Expand Up @@ -1227,7 +1226,139 @@ export abstract class AbstractControl {
*
* @publicApi
*/
export class FormControl extends AbstractControl {
export declare interface FormControl extends AbstractControl {
/** @internal */
_onChange: Function[];

/** @internal */
_pendingValue: any;

/** @internal */
_pendingChange: any;

/**
* Sets a new value for the form control.
*
* @param value The new value for the control.
* @param options Configuration options that determine how the control propagates changes
* and emits events when the value changes.
* The configuration options are passed to the {@link AbstractControl#updateValueAndValidity
* updateValueAndValidity} method.
*
* * `onlySelf`: When true, each change only affects this control, and not its parent. Default is
* false.
* * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
* `valueChanges`
* observables emit events with the latest status and value when the control value is updated.
* When false, no events are emitted.
* * `emitModelToViewChange`: When true or not supplied (the default), each change triggers an
* `onChange` event to
* update the view.
* * `emitViewToModelChange`: When true or not supplied (the default), each change triggers an
* `ngModelChange`
* event to update the model.
*
*/
setValue(value: any, options?: {
onlySelf?: boolean,
emitEvent?: boolean,
emitModelToViewChange?: boolean,
emitViewToModelChange?: boolean
}): void;

/**
* Patches the value of a control.
*
* This function is functionally the same as {@link FormControl#setValue setValue} at this level.
* It exists for symmetry with {@link FormGroup#patchValue patchValue} on `FormGroups` and
* `FormArrays`, where it does behave differently.
*
* @see `setValue` for options
*/
patchValue(value: any, options?: {
onlySelf?: boolean,
emitEvent?: boolean,
emitModelToViewChange?: boolean,
emitViewToModelChange?: boolean
}): void;

/**
* Resets the form control, marking it `pristine` and `untouched`, and setting
* the value to the provided default, or null if no value is provided.
*
* @param formState Resets the control with an initial value,
* or an object that defines the initial value and disabled state.
*
* @param options Configuration options that determine how the control propagates changes
* and emits events after the value changes.
*
* * `onlySelf`: When true, each change only affects this control, and not its parent. Default is
* false.
* * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
* `valueChanges`
* observables emit events with the latest status and value when the control is reset.
* When false, no events are emitted.
*
*/
reset(formState?: any, options?: {onlySelf?: boolean, emitEvent?: boolean}): void;

/**
* @internal
*/
_updateValue(): void;

/**
* @internal
*/
_anyControls(condition: Function): boolean;

/**
* @internal
*/
_allControlsDisabled(): boolean;

/**
* Register a listener for change events.
*
* @param fn The method that is called when the value changes
*/
registerOnChange(fn: Function): void;

/**
* Internal function to unregister a change events listener.
* @internal
*/
_unregisterOnChange(fn: Function): void;

/**
* Register a listener for disabled events.
*
* @param fn The method that is called when the disabled status changes.
*/
registerOnDisabledChange(fn: (isDisabled: boolean) => void): void;

/**
* Internal function to unregister a disabled event listener.
* @internal
*/
_unregisterOnDisabledChange(fn: (isDisabled: boolean) => void): void;

/**
* @internal
*/
_forEachChild(cb: Function): void;

/** @internal */
_syncPendingControls(): boolean;
}

interface FormControlCtor {
new(): FormControl;
new(value: any, validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl;
}

export class FormControlImpl extends AbstractControl {
/** @internal */
_onChange: Function[] = [];

Expand Down Expand Up @@ -1432,6 +1563,8 @@ export class FormControl extends AbstractControl {
}
}

export const FormControl: FormControlCtor = FormControlImpl as FormControlCtor;

/**
* Tracks the value and validity state of a group of `FormControl` instances.
*
Expand Down
21 changes: 20 additions & 1 deletion packages/forms/test/form_control_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import {fakeAsync, tick} from '@angular/core/testing';
import {FormControl, FormGroup, Validators} from '@angular/forms';

import {FormArray} from '@angular/forms/src/model';

import {asyncValidator, asyncValidatorReturningObservable} from './util';

(function() {
Expand Down Expand Up @@ -1469,5 +1469,24 @@ describe('FormControl', () => {
});
});
});

describe('can be extended', () => {
// We don't technically support extending Forms classes, but people do it anyway.
// We need to make sure that there is some way to extend them to avoid causing breakage.

class FCExt extends FormControl {
constructor(formState?: any|{
value?: any;
disabled?: boolean;
}, ...args: any) {
super(formState, ...args);
}
}

it('should perform basic FormControl operations', () => {
const nc = new FCExt({value: 'foo'});
nc.setValue('bar');
});
});
});
})();

0 comments on commit 789c9aa

Please sign in to comment.