@edersondev/ngx-form-error-message
Version:
Este pacote serve para controlar as mensagens de erro do formulário reativo do angular
144 lines (133 loc) • 8.47 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs'), require('@angular/forms'), require('@angular/core'), require('rxjs/operators')) :
typeof define === 'function' && define.amd ? define('@edersondev/ngx-form-error-message', ['exports', 'rxjs', '@angular/forms', '@angular/core', 'rxjs/operators'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.edersondev = global.edersondev || {}, global.edersondev['ngx-form-error-message'] = {}), global.rxjs, global.ng.forms, global.ng.core, global.rxjs.operators));
}(this, (function (exports, rxjs, forms, i0, operators) { 'use strict';
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var i0__namespace = /*#__PURE__*/_interopNamespace(i0);
var defaultErrors = {
min: function (objMin) { return "O valor m\u00EDnimo \u00E9 " + objMin.min + " e o valor informado \u00E9 " + objMin.actual; },
max: function (objMax) { return "O valor m\u00E1ximo \u00E9 " + objMax.max + " e o valor informado \u00E9 " + objMax.actual; },
required: function () { return "Campo obrigat\u00F3rio"; },
maxCheckbox: function (value) { return "Selecione no m\u00E1ximo " + value + " op\u00E7\u00F5es."; },
email: function () { return "Informe um e-mail v\u00E1lido"; },
matDatepickerParse: function (matDatepickerParse) { return (matDatepickerParse.text == 0 ? 'Informe uma data válida' : "O valor " + matDatepickerParse.text + " \u00E9 inv\u00E1lido"); },
maxlength: function (obj) { return "A quantidade m\u00E1xima de caracteres \u00E9 " + obj.requiredLength + " mas o campo possui " + obj.actualLength; },
minlength: function (obj) { return "A quantidade m\u00EDnima de caracteres \u00E9 " + obj.requiredLength + " mas o campo possui " + obj.actualLength; },
pattern: function (objPattern) { return "O valor '" + objPattern.actualValue + "' n\u00E3o \u00E9 compat\u00EDvel com o formato " + objPattern.requiredPattern; },
mustMatch: function () { return "Os valores n\u00E3o conferem"; },
fileSize: function (objFilesize) { return "O tamanho m\u00E1ximo permitido por arquivo \u00E9 de " + objFilesize.limitSize + ", o arquivo atual possui " + objFilesize.sizeFile; },
requiredFileType: function (obj) { return "Tipo de arquivo inv\u00E1lido, este campo aceita arquivos do tipo: " + obj.types; },
notMatch: function () { return "Os valores n\u00E3o devem ser iguais"; },
selectedCheckbox: function (selectItens) { return "Selecione " + selectItens + " op\u00E7\u00F5es"; }
};
var FORM_MSG_ERRORS = new i0.InjectionToken('FORM_MSG_ERRORS', {
providedIn: 'root',
factory: function () { return defaultErrors; }
});
var NgxErrorMessageDirective = /** @class */ (function () {
function NgxErrorMessageDirective(errors, _elementRef) {
this.errors = errors;
this._elementRef = _elementRef;
this.customMessage = {};
this.destroy$ = new rxjs.Subject();
this._elementRef.nativeElement.textContent = '';
}
NgxErrorMessageDirective.prototype.ngOnInit = function () {
var _this = this;
if (this.formControl instanceof forms.FormControl) {
this._elementRef.nativeElement.textContent = this.setTextError();
this.formControl.valueChanges
.pipe(operators.takeUntil(this.destroy$))
.subscribe(function (response) {
_this._elementRef.nativeElement.textContent = _this.setTextError();
});
}
else {
throw Error("You need to pass a FormControl.Example:\n\n <div [errorMessage]=\"form.get('input')\"></div>\n");
}
};
NgxErrorMessageDirective.prototype.ngOnDestroy = function () {
this.destroy$.next();
this.destroy$.complete();
};
NgxErrorMessageDirective.prototype.setTextError = function () {
var objErrors = (this.formControl != undefined ? this.formControl.errors : null);
if (objErrors != null) {
var key = Object.keys(objErrors).length - 1;
var firstElement = Object.keys(objErrors)[key];
var getError = this.errors[firstElement];
if (this.customMessage != undefined && this.customMessage[firstElement]) {
return this.customMessage[firstElement];
}
return getError(objErrors[firstElement]);
}
return '';
};
return NgxErrorMessageDirective;
}());
NgxErrorMessageDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.6", ngImport: i0__namespace, type: NgxErrorMessageDirective, deps: [{ token: FORM_MSG_ERRORS }, { token: i0__namespace.ElementRef }], target: i0__namespace.ɵɵFactoryTarget.Directive });
NgxErrorMessageDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.2.6", type: NgxErrorMessageDirective, selector: "[errorMessage]", inputs: { formControl: ["errorMessage", "formControl"], customMessage: "customMessage" }, ngImport: i0__namespace });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.6", ngImport: i0__namespace, type: NgxErrorMessageDirective, decorators: [{
type: i0.Directive,
args: [{
selector: '[errorMessage]'
}]
}], ctorParameters: function () {
return [{ type: undefined, decorators: [{
type: i0.Inject,
args: [FORM_MSG_ERRORS]
}] }, { type: i0__namespace.ElementRef }];
}, propDecorators: { formControl: [{
type: i0.Input,
args: ['errorMessage']
}], customMessage: [{
type: i0.Input
}] } });
var NgxFormErrorMessageModule = /** @class */ (function () {
function NgxFormErrorMessageModule() {
}
return NgxFormErrorMessageModule;
}());
NgxFormErrorMessageModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.6", ngImport: i0__namespace, type: NgxFormErrorMessageModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule });
NgxFormErrorMessageModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.6", ngImport: i0__namespace, type: NgxFormErrorMessageModule, declarations: [NgxErrorMessageDirective], exports: [NgxErrorMessageDirective] });
NgxFormErrorMessageModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.6", ngImport: i0__namespace, type: NgxFormErrorMessageModule, imports: [[]] });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.6", ngImport: i0__namespace, type: NgxFormErrorMessageModule, decorators: [{
type: i0.NgModule,
args: [{
declarations: [
NgxErrorMessageDirective
],
imports: [],
exports: [NgxErrorMessageDirective]
}]
}] });
/*
* Public API Surface of ngx-form-error-message
*/
/**
* Generated bundle index. Do not edit.
*/
exports.NgxErrorMessageDirective = NgxErrorMessageDirective;
exports.NgxFormErrorMessageModule = NgxFormErrorMessageModule;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=edersondev-ngx-form-error-message.umd.js.map