Skip to content

Commit

Permalink
Update to angular 13
Browse files Browse the repository at this point in the history
  • Loading branch information
ciml committed Apr 4, 2023
1 parent f6eda55 commit 201d5f5
Show file tree
Hide file tree
Showing 7 changed files with 2,457 additions and 4,235 deletions.
4 changes: 2 additions & 2 deletions dist/formcontrol.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormControl, ValidatorFn, AsyncValidatorFn } from '@angular/forms';
import { FormControl, ValidatorFn, AsyncValidatorFn, FormControlStatus } from '@angular/forms';
import { Observable } from 'rxjs';
/**
* Validator options to have mapping key -> validator function.
Expand All @@ -23,7 +23,7 @@ export declare class TypedFormControl<T> extends FormControl {
/** @inheritdoc */
readonly valueChanges: Observable<T>;
/** @inheritdoc */
readonly statusChanges: Observable<T>;
readonly statusChanges: Observable<FormControlStatus>;
/** holds all possible validator names extracted by the given validators */
readonly registeredValidators: string[];
/** @inheritdoc */
Expand Down
181 changes: 72 additions & 109 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,79 @@
import { FormControl, FormGroup } from '@angular/forms';

/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */

var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};

function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

/** @inheritdoc */
var TypedFormControl = /** @class */ (function (_super) {
__extends(TypedFormControl, _super);
class TypedFormControl extends FormControl {
/** @inheritdoc */
function TypedFormControl(formState, opts) {
var _this = _super.call(this, formState, {
validators: opts ? opts.validators.map(function (validator) { return validator && validator[1]; }) : null,
asyncValidators: opts && opts.asyncValidators ? opts.asyncValidators.map(function (validator) { return validator && validator[1]; }) : null,
constructor(formState, opts) {
super(formState, {
validators: opts ? opts.validators.map(validator => validator && validator[1]) : null,
asyncValidators: opts && opts.asyncValidators ? opts.asyncValidators.map(validator => validator && validator[1]) : null,
updateOn: opts && opts.updateOn ? opts.updateOn : 'change',
}) || this;
_this.registeredValidators = _this.generateValidatorNames(opts);
return _this;
});
this.registeredValidators = this.generateValidatorNames(opts);
}
/** @inheritdoc */
TypedFormControl.prototype.patchValue = function (value, options) {
_super.prototype.patchValue.call(this, value, options);
};
patchValue(value, options) {
super.patchValue(value, options);
}
/** @inheritdoc */
TypedFormControl.prototype.setValue = function (value, options) {
_super.prototype.setValue.call(this, value, options);
};
setValue(value, options) {
super.setValue(value, options);
}
/** @inheritdoc */
TypedFormControl.prototype.reset = function (formState, options) {
_super.prototype.reset.call(this, formState, options);
};
reset(formState, options) {
super.reset(formState, options);
}
/**
* Sets new validators and updates possible error keys list.
*
* @param newValidators new validators
*/
TypedFormControl.prototype.setNewValidators = function (newValidators) {
_super.prototype.setValidators.call(this, newValidators ? newValidators.validators.map(function (validator) { return validator && validator[1]; }) : null);
_super.prototype.setAsyncValidators.call(this, newValidators && newValidators.asyncValidators
? newValidators.asyncValidators.map(function (validator) { return validator && validator[1]; })
setNewValidators(newValidators) {
super.setValidators(newValidators ? newValidators.validators.map(validator => validator && validator[1]) : null);
super.setAsyncValidators(newValidators && newValidators.asyncValidators
? newValidators.asyncValidators.map(validator => validator && validator[1])
: null);
this.registeredValidators = this.generateValidatorNames(newValidators);
};
}
/**
* Generates validator name list.
*
* @param validatorOpts options to handle
*/
TypedFormControl.prototype.generateValidatorNames = function (validatorOpts) {
var keys = [];
generateValidatorNames(validatorOpts) {
let keys = [];
if (validatorOpts) {
var validatorsList = [validatorOpts.validators];
let validatorsList = [validatorOpts.validators];
if (validatorOpts.asyncValidators) {
validatorsList.push(validatorOpts.asyncValidators);
}
validatorsList.forEach(function (validators) {
keys.push.apply(keys, validators
.map(function (validator) { return validator && validator[0]; })
validatorsList.forEach((validators) => {
keys.push(...validators
.map(validator => validator && validator[0])
// filter duplicates
.filter(function (key, index, array) { return array.indexOf(key) === index; }));
.filter((key, index, array) => array.indexOf(key) === index));
});
}
return keys;
};
return TypedFormControl;
}(FormControl));
}
}

/**
* This is the base from control factory for each model. Based on this class, each model is implementing it's own
* FormControlFactory.
* Main purpose of this factory is to provide an easy way of creating form-controls with the correct validators.
* The factory also ensures that model and validator match one another.
*/
var BaseFormControlFactory = /** @class */ (function () {
class BaseFormControlFactory {
/**
* Constructor.
*
* @param model The model object.
* @param validators properties validators map
*/
function BaseFormControlFactory(model, validators) {
constructor(model, validators) {
this.map = new Map();
for (var property in model) {
for (const property in model) {
if (!model.hasOwnProperty(property)) {
continue;
}
Expand All @@ -122,92 +90,89 @@ var BaseFormControlFactory = /** @class */ (function () {
* @param controlOpts add custom validators to the default ones given in the constructor, optional async validators
* and update mode.
*/
BaseFormControlFactory.prototype.createFormControl = function (property, controlOpts) {
var model = this.map.get(property);
createFormControl(property, controlOpts) {
const model = this.map.get(property);
if (model) {
return new TypedFormControl(model.value, {
validators: model.validators.concat((controlOpts ? controlOpts.validators : [])),
validators: [...model.validators, ...(controlOpts ? controlOpts.validators : [])],
asyncValidators: controlOpts ? controlOpts.asyncValidators : undefined,
updateOn: controlOpts ? controlOpts.updateOn : undefined,
});
}
return new TypedFormControl();
};
return BaseFormControlFactory;
}());
}
}

/**
* Typed FormGroup.
*/
var TypedFormGroup = /** @class */ (function (_super) {
__extends(TypedFormGroup, _super);
class TypedFormGroup extends FormGroup {
/** @inheritdoc */
function TypedFormGroup(controls, validatorOrOpts, asyncValidator) {
var _this = _super.call(this, controls, validatorOrOpts, asyncValidator) || this;
var map = {};
Object.keys(controls).forEach(function (controlName) {
var control = controls[controlName];
constructor(controls, validatorOrOpts, asyncValidator) {
super(controls, validatorOrOpts, asyncValidator);
const map = {};
Object.keys(controls).forEach(controlName => {
const control = controls[controlName];
if (control instanceof TypedFormControl) {
Object.defineProperty(map, controlName, {
get: function () {
get: () => {
return control.registeredValidators;
},
});
}
});
_this.registeredValidatorsMap = map;
return _this;
this.registeredValidatorsMap = map;
}
/** @inheritdoc */
TypedFormGroup.prototype.patchValue = function (value, options) {
_super.prototype.patchValue.call(this, value, options);
};
patchValue(value, options) {
super.patchValue(value, options);
}
/** @inheritdoc */
TypedFormGroup.prototype.get = function (path) {
return _super.prototype.get.call(this, path);
};
get(path) {
return super.get(path);
}
/**
* Returns group if available.
*
* @param path path to group
*/
TypedFormGroup.prototype.getNestedGroup = function (path) {
var control = this.get(path);
getNestedGroup(path) {
const control = this.get(path);
if (control instanceof TypedFormGroup) {
return control;
}
return null;
};
}
/**
* Detects if an error is present for given control name.
*
* @param name control name of the form group
*/
TypedFormGroup.prototype.hasControlErrors = function (name) {
var control = this.get(name);
hasControlErrors(name) {
const control = this.get(name);
return !!(control && control.errors);
};
}
/**
* Detects if control has validator for given control name and validator name.
*
* @param name control name of the form group
* @param validatorName validator name
*/
TypedFormGroup.prototype.isValidatorRegistered = function (name, validatorName) {
isValidatorRegistered(name, validatorName) {
return (this.registeredValidatorsMap[name] &&
this.registeredValidatorsMap[name].some(function (errorKey) { return errorKey === validatorName; }));
};
this.registeredValidatorsMap[name].some(errorKey => errorKey === validatorName));
}
/**
* Returns an error key for the next error.
*
* @param name control key of the form group
*/
TypedFormGroup.prototype.nextControlErrorKey = function (name) {
var control = this.get(name);
nextControlErrorKey(name) {
const control = this.get(name);
if (control && control.errors) {
// try client side keys first for correct order
var error = this.registeredValidatorsMap[name] &&
this.registeredValidatorsMap[name].find(function (validatorKey) { return control.hasError(validatorKey); });
let error = this.registeredValidatorsMap[name] &&
this.registeredValidatorsMap[name].find(validatorKey => control.hasError(validatorKey));
if (!error) {
// fallback to all errors including custom errors set after backend calls
error = Object.keys(control.errors).shift();
Expand All @@ -217,27 +182,25 @@ var TypedFormGroup = /** @class */ (function (_super) {
}
}
return '';
};
}
/**
* Dispatches errors to this control and to child controls using given error map.
*
* @param errors error map
* @param contextPath optional context path to errors set to
*/
TypedFormGroup.prototype.dispatchErrors = function (errors, contextPath) {
var _this = this;
var paths = Object.keys(errors);
paths.forEach(function (path) {
var control = _this.get((contextPath ? contextPath + "." + path : path));
dispatchErrors(errors, contextPath) {
const paths = Object.keys(errors);
paths.forEach(path => {
const control = this.get((contextPath ? `${contextPath}.${path}` : path));
if (control) {
// enables showing errors in view
control.enable();
control.markAsTouched();
control.setErrors(errors[path]);
}
});
};
return TypedFormGroup;
}(FormGroup));
}
}

export { BaseFormControlFactory, TypedFormControl, TypedFormGroup };
Loading

0 comments on commit 201d5f5

Please sign in to comment.