Skip to content

Commit

Permalink
refactor(forms): inherit ngOnChanges hooks from the base class (#43945
Browse files Browse the repository at this point in the history
)

This commit updates the code of the min/max and minlength/maxlength validator directives to inherit `ngOnChanges` hooks from the base class (the `AbstractValidatorDirective` one), rather than implementing the hooks on the child classes. This was needed to avoid issues with hooks inheritance in ViewEngine, but since it's deprecated, the code can be cleaned up.

PR Close #43945
  • Loading branch information
AndrewKushnir committed Nov 17, 2021
1 parent 6474c3d commit 65597d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 48 deletions.
14 changes: 4 additions & 10 deletions goldens/public-api/forms/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,41 +454,35 @@ export class FormsModule {
}

// @public
export class MaxLengthValidator extends AbstractValidatorDirective implements OnChanges {
export class MaxLengthValidator extends AbstractValidatorDirective {
maxlength: string | number | null;
// (undocumented)
ngOnChanges(changes: SimpleChanges): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MaxLengthValidator, "[maxlength][formControlName],[maxlength][formControl],[maxlength][ngModel]", never, { "maxlength": "maxlength"; }, {}, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MaxLengthValidator, never>;
}

// @public
export class MaxValidator extends AbstractValidatorDirective implements OnChanges {
export class MaxValidator extends AbstractValidatorDirective {
max: string | number | null;
ngOnChanges(changes: SimpleChanges): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MaxValidator, "input[type=number][max][formControlName],input[type=number][max][formControl],input[type=number][max][ngModel]", never, { "max": "max"; }, {}, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MaxValidator, never>;
}

// @public
export class MinLengthValidator extends AbstractValidatorDirective implements OnChanges {
export class MinLengthValidator extends AbstractValidatorDirective {
minlength: string | number | null;
// (undocumented)
ngOnChanges(changes: SimpleChanges): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MinLengthValidator, "[minlength][formControlName],[minlength][formControl],[minlength][ngModel]", never, { "minlength": "minlength"; }, {}, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MinLengthValidator, never>;
}

// @public
export class MinValidator extends AbstractValidatorDirective implements OnChanges {
export class MinValidator extends AbstractValidatorDirective {
min: string | number | null;
ngOnChanges(changes: SimpleChanges): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MinValidator, "input[type=number][min][formControlName],input[type=number][min][formControl],input[type=number][min][ngModel]", never, { "min": "min"; }, {}, never>;
// (undocumented)
Expand Down
47 changes: 9 additions & 38 deletions packages/forms/src/directives/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface Validator {
* For internal use only, this class is not intended for use outside of the Forms package.
*/
@Directive()
abstract class AbstractValidatorDirective implements Validator {
abstract class AbstractValidatorDirective implements Validator, OnChanges {
private _validator: ValidatorFn = nullValidator;
private _onChange!: () => void;

Expand Down Expand Up @@ -125,11 +125,8 @@ abstract class AbstractValidatorDirective implements Validator {
*/
abstract normalizeInput(input: unknown): unknown;

/**
* Helper function invoked from child classes to process changes (from `ngOnChanges` hook).
* @nodoc
*/
handleChanges(changes: SimpleChanges): void {
/** @nodoc */
ngOnChanges(changes: SimpleChanges): void {
if (this.inputName in changes) {
const input = this.normalizeInput(changes[this.inputName].currentValue);
this._validator = this.enabled() ? this.createValidator(input) : nullValidator;
Expand Down Expand Up @@ -199,7 +196,7 @@ export const MAX_VALIDATOR: StaticProvider = {
providers: [MAX_VALIDATOR],
host: {'[attr.max]': 'enabled() ? max : null'}
})
export class MaxValidator extends AbstractValidatorDirective implements OnChanges {
export class MaxValidator extends AbstractValidatorDirective {
/**
* @description
* Tracks changes to the max bound to this directive.
Expand All @@ -211,15 +208,6 @@ export class MaxValidator extends AbstractValidatorDirective implements OnChange
override normalizeInput = (input: string|number): number => toFloat(input);
/** @internal */
override createValidator = (max: number): ValidatorFn => maxValidator(max);
/**
* Declare `ngOnChanges` lifecycle hook at the main directive level (vs keeping it in base class)
* to avoid differences in handling inheritance of lifecycle hooks between Ivy and ViewEngine in
* AOT mode. This could be refactored once ViewEngine is removed.
* @nodoc
*/
ngOnChanges(changes: SimpleChanges): void {
this.handleChanges(changes);
}
}

/**
Expand Down Expand Up @@ -259,7 +247,7 @@ export const MIN_VALIDATOR: StaticProvider = {
providers: [MIN_VALIDATOR],
host: {'[attr.min]': 'enabled() ? min : null'}
})
export class MinValidator extends AbstractValidatorDirective implements OnChanges {
export class MinValidator extends AbstractValidatorDirective {
/**
* @description
* Tracks changes to the min bound to this directive.
Expand All @@ -271,15 +259,6 @@ export class MinValidator extends AbstractValidatorDirective implements OnChange
override normalizeInput = (input: string|number): number => toFloat(input);
/** @internal */
override createValidator = (min: number): ValidatorFn => minValidator(min);
/**
* Declare `ngOnChanges` lifecycle hook at the main directive level (vs keeping it in base class)
* to avoid differences in handling inheritance of lifecycle hooks between Ivy and ViewEngine in
* AOT mode. This could be refactored once ViewEngine is removed.
* @nodoc
*/
ngOnChanges(changes: SimpleChanges): void {
this.handleChanges(changes);
}
}

/**
Expand Down Expand Up @@ -572,12 +551,13 @@ export const MIN_LENGTH_VALIDATOR: any = {
providers: [MIN_LENGTH_VALIDATOR],
host: {'[attr.minlength]': 'enabled() ? minlength : null'}
})
export class MinLengthValidator extends AbstractValidatorDirective implements OnChanges {
export class MinLengthValidator extends AbstractValidatorDirective {
/**
* @description
* Tracks changes to the minimum length bound to this directive.
*/
@Input() minlength!: string|number|null;

/** @internal */
override inputName = 'minlength';

Expand All @@ -586,11 +566,6 @@ export class MinLengthValidator extends AbstractValidatorDirective implements On

/** @internal */
override createValidator = (minlength: number): ValidatorFn => minLengthValidator(minlength);

/** @nodoc */
ngOnChanges(changes: SimpleChanges): void {
this.handleChanges(changes);
}
}

/**
Expand Down Expand Up @@ -629,12 +604,13 @@ export const MAX_LENGTH_VALIDATOR: any = {
providers: [MAX_LENGTH_VALIDATOR],
host: {'[attr.maxlength]': 'enabled() ? maxlength : null'}
})
export class MaxLengthValidator extends AbstractValidatorDirective implements OnChanges {
export class MaxLengthValidator extends AbstractValidatorDirective {
/**
* @description
* Tracks changes to the minimum length bound to this directive.
*/
@Input() maxlength!: string|number|null;

/** @internal */
override inputName = 'maxlength';

Expand All @@ -643,11 +619,6 @@ export class MaxLengthValidator extends AbstractValidatorDirective implements On

/** @internal */
override createValidator = (maxlength: number): ValidatorFn => maxLengthValidator(maxlength);

/** @nodoc */
ngOnChanges(changes: SimpleChanges): void {
this.handleChanges(changes);
}
}

/**
Expand Down

0 comments on commit 65597d6

Please sign in to comment.