ngx-mat-errors
Version:
NgxMatErrors provides an easy, yet flexible solution for displaying error messages in a MatFormField.
148 lines (137 loc) • 6.25 kB
TypeScript
import * as i0 from '@angular/core';
import { TemplateRef, FactoryProvider, InjectionToken, Signal } from '@angular/core';
import { ValidationErrors, AbstractControl, AbstractControlDirective } from '@angular/forms';
import * as _angular_material_form_field from '@angular/material/form-field';
import { MatFormFieldControl } from '@angular/material/form-field';
import * as _angular_material_datepicker from '@angular/material/datepicker';
import { Observable } from 'rxjs';
type ErrorTemplate = {
template: TemplateRef<any>;
$implicit: ValidationErrors;
} | {
template: undefined;
$implicit: string;
} | undefined;
type FormFieldControl = Pick<MatFormFieldControl<any>, 'ngControl'>;
type NgxMatErrorControls = FormFieldControl | AbstractControl | AbstractControlDirective | (FormFieldControl | AbstractControl | AbstractControlDirective)[] | undefined | null | '';
type ErrorTransform = (error: any) => string;
interface ErrorMessages {
[key: string]: string | ErrorTransform;
}
/**
* For errors: 'min', 'matDatepickerMin'
*/
interface MinError<T = number> {
min: T;
actual: T;
}
/**
* For errors: 'max', 'matDatepickerMax'
*/
interface MaxError<T = number> {
max: T;
actual: T;
}
/**
* For errors: 'minlength', 'maxlength'
*/
interface LengthError {
requiredLength: number;
actualLength: number;
}
/**
* For errors: 'pattern'
*/
interface PatternValidator {
requiredPattern: string;
actualValue: string;
}
/**
* For errors: 'matStartDateInvalid'
*/
interface StartDateError<D> {
end: D;
actual: D;
}
/**
* For errors: 'matEndDateInvalid'
*/
interface EndDateError<D> {
start: D;
actual: D;
}
/**
* For errors: 'matDatepickerParse', 'matTimepickerParse'
*/
interface ParseError {
text: string;
}
declare function errorMessagesEnFactory(locale: string, dateFormat?: string, timeFormat?: string): ErrorMessages;
declare const NGX_MAT_ERROR_CONFIG_EN: FactoryProvider;
declare function errorMessagesHuFactory(locale: string, dateFormat?: string, timeFormat?: string): ErrorMessages;
declare const NGX_MAT_ERROR_CONFIG_HU: FactoryProvider;
declare function errorMessagesPtBtFactory(locale: string, dateFormat?: string, timeFormat?: string): ErrorMessages;
declare const NGX_MAT_ERROR_CONFIG_PT_BR: FactoryProvider;
interface INgxMatErrorDef {
ngxMatErrorDefFor: string;
ngxMatErrorDefWithControl?: AbstractControlDirective | AbstractControl | string | null;
template: TemplateRef<any>;
control?: AbstractControl;
}
/**
* Lightweight injection token. When NgxMatErrorDef is not used, only this token will remain, the directive will be tree-shaken.
*/
declare const NGX_MAT_ERROR_DEF: InjectionToken<INgxMatErrorDef>;
declare class NgxMatErrorDef implements INgxMatErrorDef {
/**
* Specify the error key to be used for error matching.
* @required
*/
ngxMatErrorDefFor: string;
/**
* Specify the control to be used for error matching.
* @optional
*/
ngxMatErrorDefWithControl?: AbstractControlDirective | AbstractControl | string | null;
readonly template: TemplateRef<any>;
private readonly controlContainer;
get control(): AbstractControl | undefined;
static ɵfac: i0.ɵɵFactoryDeclaration<NgxMatErrorDef, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<NgxMatErrorDef, "[ngxMatErrorDef]", never, { "ngxMatErrorDefFor": { "alias": "ngxMatErrorDefFor"; "required": true; }; "ngxMatErrorDefWithControl": { "alias": "ngxMatErrorDefWithControl"; "required": false; }; }, {}, never, never, true, never>;
}
/**
* This class contains the logic of getting the default control of a MatFormField.
* Extend it to implement a custom getter method.
*/
declare class NgxMatErrorControl {
protected readonly matFormField: _angular_material_form_field.MatFormField | null;
readonly control: i0.WritableSignal<FormFieldControl | FormFieldControl[] | undefined>;
get(): undefined | FormFieldControl | FormFieldControl[];
constructor();
static ɵfac: i0.ɵɵFactoryDeclaration<NgxMatErrorControl, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<NgxMatErrorControl>;
}
declare class NgxMatErrorsForDateRangePicker<D> extends NgxMatErrorControl {
/** Returns start and end controls of the date range picker. */
get(): (_angular_material_datepicker.MatStartDate<D> | _angular_material_datepicker.MatEndDate<D>)[] | undefined;
static ɵfac: i0.ɵɵFactoryDeclaration<NgxMatErrorsForDateRangePicker<any>, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<NgxMatErrorsForDateRangePicker<any>, "[ngx-mat-errors][forDateRangePicker]", never, {}, {}, never, never, true, never>;
}
declare const NGX_MAT_ERROR_DEFAULT_OPTIONS: InjectionToken<ErrorMessages | Observable<ErrorMessages> | Signal<ErrorMessages>>;
declare class NgxMatErrors {
private readonly messages;
private readonly customErrorMessages;
private readonly defaultControl;
readonly control: i0.InputSignal<NgxMatErrorControls>;
private readonly controlWithError;
protected readonly error: Signal<ErrorTemplate>;
static ɵfac: i0.ɵɵFactoryDeclaration<NgxMatErrors, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<NgxMatErrors, "ngx-mat-errors, [ngx-mat-errors]", never, { "control": { "alias": "ngx-mat-errors"; "required": false; "isSignal": true; }; }, {}, ["customErrorMessages"], never, true, never>;
}
declare class NgxMatErrorsModule {
static ɵfac: i0.ɵɵFactoryDeclaration<NgxMatErrorsModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<NgxMatErrorsModule, never, [typeof NgxMatErrors, typeof NgxMatErrorDef], [typeof NgxMatErrors, typeof NgxMatErrorDef]>;
static ɵinj: i0.ɵɵInjectorDeclaration<NgxMatErrorsModule>;
}
export { NGX_MAT_ERROR_CONFIG_EN, NGX_MAT_ERROR_CONFIG_HU, NGX_MAT_ERROR_CONFIG_PT_BR, NGX_MAT_ERROR_DEF, NGX_MAT_ERROR_DEFAULT_OPTIONS, NgxMatErrorDef, NgxMatErrors, NgxMatErrorsForDateRangePicker, NgxMatErrorsModule, errorMessagesEnFactory, errorMessagesHuFactory, errorMessagesPtBtFactory };
export type { EndDateError, ErrorMessages, ErrorTemplate, ErrorTransform, FormFieldControl, INgxMatErrorDef, LengthError, MaxError, MinError, NgxMatErrorControls, ParseError, PatternValidator, StartDateError };