Skip to content

Commit

Permalink
Revert "refactor(forms): Move FormControl to an overridden exported c…
Browse files Browse the repository at this point in the history
…onstructor. (#44316)" (#44750)

This reverts commit cd5200e.

PR Close #44750
  • Loading branch information
dylhunn committed Jan 19, 2022
1 parent bc03a2e commit fb27867
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 143 deletions.
44 changes: 3 additions & 41 deletions goldens/public-api/forms/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,27 +297,22 @@ export class FormBuilder {
static ɵprov: i0.ɵɵInjectableDeclaration<FormBuilder>;
}

// @public (undocumented)
export interface FormControl extends AbstractControl {
// (undocumented)
// @public
export class FormControl extends AbstractControl {
constructor(formState?: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null);
readonly defaultValue: any;
// (undocumented)
patchValue(value: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
}): void;
// (undocumented)
registerOnChange(fn: Function): void;
// (undocumented)
registerOnDisabledChange(fn: (isDisabled: boolean) => void): void;
// (undocumented)
reset(formState?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
// (undocumented)
setValue(value: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
Expand All @@ -326,15 +321,6 @@ export interface FormControl extends AbstractControl {
}): void;
}

// @public
export const FormControl: FormControlCtor;

// @public
export interface FormControlCtor {
new (): FormControl;
new (value: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl;
}

// @public
export class FormControlDirective extends NgControl implements OnChanges, OnDestroy {
constructor(validators: (Validator | ValidatorFn)[], asyncValidators: (AsyncValidator | AsyncValidatorFn)[], valueAccessors: ControlValueAccessor[], _ngModelWarningConfig: string | null);
Expand All @@ -358,30 +344,6 @@ export class FormControlDirective extends NgControl implements OnChanges, OnDest
static ɵfac: i0.ɵɵFactoryDeclaration<FormControlDirective, [{ optional: true; self: true; }, { optional: true; self: true; }, { optional: true; self: true; }, { optional: true; }]>;
}

// @public
export class FormControlImpl extends AbstractControl {
constructor(formState?: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null);
readonly defaultValue: any;
patchValue(value: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
}): void;
registerOnChange(fn: Function): void;
registerOnDisabledChange(fn: (isDisabled: boolean) => void): void;
reset(formState?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
setValue(value: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
}): void;
}

// @public
export class FormControlName extends NgControl implements OnChanges, OnDestroy {
constructor(parent: ControlContainer, validators: (Validator | ValidatorFn)[], asyncValidators: (AsyncValidator | AsyncValidatorFn)[], valueAccessors: ControlValueAccessor[], _ngModelWarningConfig: string | null);
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export {NgSelectOption, SelectControlValueAccessor} from './directives/select_co
export {SelectMultipleControlValueAccessor, ɵNgSelectMultipleOption} from './directives/select_multiple_control_value_accessor';
export {AsyncValidator, AsyncValidatorFn, CheckboxRequiredValidator, EmailValidator, MaxLengthValidator, MaxValidator, MinLengthValidator, MinValidator, PatternValidator, RequiredValidator, ValidationErrors, Validator, ValidatorFn} from './directives/validators';
export {FormBuilder} from './form_builder';
export {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormControlCtor, FormControlImpl, FormControlOptions, FormControlStatus, FormGroup} from './model';
export {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormControlOptions, FormControlStatus, FormGroup} from './model';
export {NG_ASYNC_VALIDATORS, NG_VALIDATORS, Validators} from './validators';
export {VERSION} from './version';

Expand Down
93 changes: 11 additions & 82 deletions packages/forms/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export interface AbstractControlOptions {
export interface FormControlOptions extends AbstractControlOptions {
/**
* @description
* Whether to use the initial value used to construct the {@link FormControl} as its default value
* as well. If this option is false or not provided, the default value of a FormControl is `null`.
* When a FormControl is reset without an explicit value, its value reverts to
* Whether to use the initial value used to construct the FormControl as its default value as
* well. If this option is false or not provided, the default value of a FormControl is `null`.
* When a FormControl is {@link FormControl#reset} without an explicit value, its value reverts to
* its default value.
*/
initialValueIsDefault?: boolean;
Expand Down Expand Up @@ -1177,6 +1177,7 @@ export abstract class AbstractControl {
this._updateOn = opts.updateOn!;
}
}

/**
* Check to see if parent has been marked artificially dirty.
*
Expand All @@ -1188,74 +1189,6 @@ export abstract class AbstractControl {
}
}

export interface FormControl extends AbstractControl {
readonly defaultValue: any;
/** @internal */
_onChange: Function[];
/** @internal */
_pendingValue: any;
/** @internal */
_pendingChange: boolean;
setValue(value: any, options?: {
onlySelf?: boolean,
emitEvent?: boolean,
emitModelToViewChange?: boolean,
emitViewToModelChange?: boolean
}): void;
patchValue(value: any, options?: {
onlySelf?: boolean,
emitEvent?: boolean,
emitModelToViewChange?: boolean,
emitViewToModelChange?: boolean
}): void;
reset(formState?: any, options?: {onlySelf?: boolean, emitEvent?: boolean}): void;
/** @internal */
_updateValue(): void;
/** @internal */
_anyControls(condition: Function): boolean;
/** @internal */
_allControlsDisabled(): boolean;
registerOnChange(fn: Function): void;
/** @internal */
_unregisterOnChange(fn: Function): void;
registerOnDisabledChange(fn: (isDisabled: boolean) => void): void;
/** @internal */
_unregisterOnDisabledChange(fn: (isDisabled: boolean) => void): void;
/** @internal */
_forEachChild(cb: Function): void;
/** @internal */
_syncPendingControls(): boolean;
}

/**
* Various available constructors for `FormControl`.
* Do not use this interface directly. Instead, use `FormControl`:
* ```
* const fc = new FormControl('foo');
* ```
*/
export interface FormControlCtor {
/**
* Construct a FormControl with no initial value or validators.
*/
new(): FormControl;

/**
* Creates a new `FormControl` instance.
*
* @param formState Initializes the control with an initial value,
* or an object that defines the initial value and disabled state.
*
* @param validatorOrOpts A synchronous validator function, or an array of
* such functions, or an `FormControlOptions` object that contains validation functions
* and a validation trigger.
*
* @param asyncValidator A single async validator or array of async validator functions
*/
new(value: any, validatorOrOpts?: ValidatorFn|ValidatorFn[]|FormControlOptions|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl;
}

/**
* Tracks the value and validation status of an individual form control.
*
Expand All @@ -1277,7 +1210,7 @@ export interface FormControlCtor {
* ```ts
* const control = new FormControl('some value');
* console.log(control.value); // 'some value'
* ```
*```
*
* The following example initializes the control with a form state object. The `value`
* and `disabled` keys are required in this case.
Expand Down Expand Up @@ -1350,8 +1283,10 @@ export interface FormControlCtor {
* console.log(control.value); // 'Drew'
* console.log(control.status); // 'DISABLED'
* ```
*
* @publicApi
*/
export class FormControlImpl extends AbstractControl {
export class FormControl extends AbstractControl {
/**
* The default value of this FormControl, used whenever the control is reset without an explicit
* value. See {@link FormControlOptions#initialValueIsDefault} for more information on configuring
Expand All @@ -1361,14 +1296,14 @@ export class FormControlImpl extends AbstractControl {
public readonly defaultValue: any = null;

/** @internal */
_onChange: Function[] = [];
_onChange: Array<Function> = [];

/**
* This field holds a pending value that has not yet been applied to the form's value.
* It is `any` because the value is untyped.
* @internal
*/
_pendingValue!: any;
_pendingValue: any;

/** @internal */
_pendingChange: boolean = false;
Expand All @@ -1380,7 +1315,7 @@ export class FormControlImpl extends AbstractControl {
* or an object that defines the initial value and disabled state.
*
* @param validatorOrOpts A synchronous validator function, or an array of
* such functions, or a `FormControlOptions` object that contains validation functions
* such functions, or an `AbstractControlOptions` object that contains validation functions
* and a validation trigger.
*
* @param asyncValidator A single async validator or array of async validator functions
Expand Down Expand Up @@ -1591,12 +1526,6 @@ export class FormControlImpl extends AbstractControl {
}
}

/**
* @overriddenImplementation The constructor for FormControl is decoupled from its implementation.
* This allows us to provide multiple constructor signatures.
*/
export const FormControl: FormControlCtor = FormControlImpl as FormControlCtor;

/**
* Tracks the value and validity state of a group of `FormControl` instances.
*
Expand Down
19 changes: 0 additions & 19 deletions packages/forms/test/form_control_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,24 +1526,5 @@ 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 fb27867

Please sign in to comment.