ion-form-error-messages
Version:
An ionic 4 component that will display a list of error messages for a given reactive form formGroup object.
215 lines (209 loc) • 8.24 kB
JavaScript
import { __values } from 'tslib';
import { Component, Input, NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
/**
* @fileoverview added by tsickle
* Generated from: ion-form-error-messages.component.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var IonFormErrorMessagesComponent = /** @class */ (function () {
function IonFormErrorMessagesComponent() {
this.color = 'danger';
}
Object.defineProperty(IonFormErrorMessagesComponent.prototype, "filteredControls", {
get: /**
* @return {?}
*/
function () {
if (!this.formGroup) {
return [];
}
/** @type {?} */
var controls = this.objectToKeys(this.formGroup.controls);
if (controls && (this.controls || this.control)) {
/** @type {?} */
var filterToUse_1 = [];
if (this.control) {
filterToUse_1.push(this.control);
}
if (Array.isArray(this.controls)) {
filterToUse_1 = filterToUse_1.concat(this.controls);
}
if (filterToUse_1.length) {
controls = controls.filter((/**
* @param {?} control
* @return {?}
*/
function (control) { return filterToUse_1.indexOf(control) >= 0; }));
}
}
return controls;
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
IonFormErrorMessagesComponent.prototype.ngOnInit = /**
* @return {?}
*/
function () { };
/**
* @param {?} controlKey
* @return {?}
*/
IonFormErrorMessagesComponent.prototype.getErrors = /**
* @param {?} controlKey
* @return {?}
*/
function (controlKey) {
if (!this.formGroup || !this.formGroup.controls || !this.formGroup.controls[controlKey]) {
return {};
}
return this.formGroup.controls[controlKey].errors;
};
/**
* @return {?}
*/
IonFormErrorMessagesComponent.prototype.isAnyControlInvalid = /**
* @return {?}
*/
function () {
var e_1, _a;
/** @type {?} */
var controlKeys = Object.keys(this.formGroup.controls);
/** @type {?} */
var isInvalid = false;
try {
for (var controlKeys_1 = __values(controlKeys), controlKeys_1_1 = controlKeys_1.next(); !controlKeys_1_1.done; controlKeys_1_1 = controlKeys_1.next()) {
var controlKey = controlKeys_1_1.value;
if (this.isControlInvalid(controlKey)) {
isInvalid = true;
break;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (controlKeys_1_1 && !controlKeys_1_1.done && (_a = controlKeys_1.return)) _a.call(controlKeys_1);
}
finally { if (e_1) throw e_1.error; }
}
return isInvalid;
};
/**
* @param {?} controlKey
* @return {?}
*/
IonFormErrorMessagesComponent.prototype.isControlInvalid = /**
* @param {?} controlKey
* @return {?}
*/
function (controlKey) {
var e_2, _a;
/** @type {?} */
var control = this.formGroup.controls[controlKey];
if (!control || !control.errors) {
return false;
}
else {
/** @type {?} */
var errorKeys = Object.keys(control.errors);
/** @type {?} */
var isInvalid = false;
try {
for (var errorKeys_1 = __values(errorKeys), errorKeys_1_1 = errorKeys_1.next(); !errorKeys_1_1.done; errorKeys_1_1 = errorKeys_1.next()) {
var errorKey = errorKeys_1_1.value;
if (this.messages[controlKey][errorKey]) {
isInvalid = true;
break;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (errorKeys_1_1 && !errorKeys_1_1.done && (_a = errorKeys_1.return)) _a.call(errorKeys_1);
}
finally { if (e_2) throw e_2.error; }
}
return control.invalid && isInvalid && (control.dirty || control.touched);
}
};
/**
* @param {?} value
* @return {?}
*/
IonFormErrorMessagesComponent.prototype.objectToKeys = /**
* @param {?} value
* @return {?}
*/
function (value) {
if (!value) {
return value;
}
return Object.keys(value);
};
IonFormErrorMessagesComponent.decorators = [
{ type: Component, args: [{
selector: 'ion-form-error-messages',
template: "\n <ion-item *ngIf=\"isAnyControlInvalid()\" lines=\"none\">\n <ion-grid class=\"ion-no-padding\">\n <ng-container *ngFor=\"let controlKey of filteredControls\">\n <ng-container *ngFor=\"let errorKey of objectToKeys(getErrors(controlKey))\">\n <ng-container *ngIf=\"messages[controlKey] || {} as errorMessages\">\n <ion-row *ngIf=\"errorMessages[errorKey]\">\n <ion-col class=\"ion-no-padding ion-text-wrap\">\n <ion-text [color]=\"color\" [innerHtml]=\"errorMessages[errorKey]\"> </ion-text>\n </ion-col>\n </ion-row>\n </ng-container>\n </ng-container>\n </ng-container>\n </ion-grid>\n </ion-item>\n ",
styles: [' ion-item { --min-height: 16px; font-size: 14px;}']
}] }
];
/** @nocollapse */
IonFormErrorMessagesComponent.ctorParameters = function () { return []; };
IonFormErrorMessagesComponent.propDecorators = {
formGroup: [{ type: Input }],
messages: [{ type: Input }],
color: [{ type: Input }],
control: [{ type: Input }],
controls: [{ type: Input }]
};
return IonFormErrorMessagesComponent;
}());
if (false) {
/** @type {?} */
IonFormErrorMessagesComponent.prototype.formGroup;
/** @type {?} */
IonFormErrorMessagesComponent.prototype.messages;
/** @type {?} */
IonFormErrorMessagesComponent.prototype.color;
/** @type {?} */
IonFormErrorMessagesComponent.prototype.control;
/** @type {?} */
IonFormErrorMessagesComponent.prototype.controls;
}
/**
* @fileoverview added by tsickle
* Generated from: ion-form-error-messages.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var IonFormErrorMessagesModule = /** @class */ (function () {
function IonFormErrorMessagesModule() {
}
IonFormErrorMessagesModule.decorators = [
{ type: NgModule, args: [{
declarations: [IonFormErrorMessagesComponent],
imports: [CommonModule, FormsModule, IonicModule],
exports: [IonFormErrorMessagesComponent]
},] }
];
return IonFormErrorMessagesModule;
}());
/**
* @fileoverview added by tsickle
* Generated from: public-api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: ion-form-error-messages.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { IonFormErrorMessagesComponent, IonFormErrorMessagesModule };
//# sourceMappingURL=ion-form-error-messages.js.map