@ngspot/ngx-errors
Version:
<p align="center"> <img width="20%" height="20%" src="https://github.com/DmitryEfimenko/ngspot/blob/main/packages/ngx-errors/package/assets/logo.png?raw=true"> </p>
296 lines (280 loc) • 13 kB
TypeScript
import * as i0 from '@angular/core';
import { InjectionToken, Provider, AfterViewInit, OnDestroy, Signal, WritableSignal } from '@angular/core';
import * as _angular_forms from '@angular/forms';
import { AbstractControl, FormGroupDirective, NgForm, ControlContainer, ValidatorFn } from '@angular/forms';
import * as rxjs from 'rxjs';
import { Observable, UnaryFunction } from 'rxjs';
import * as _ngspot_ngx_errors from '@ngspot/ngx-errors';
interface IErrorStateMatcher {
isErrorState(control: AbstractControl | null, form: FormGroupDirective | NgForm | null): boolean;
}
type CustomErrorStateMatchers = {
[key: string]: IErrorStateMatcher;
};
/**
* Provides a way to add to available options for when to display an error for
* an invalid control. Options that come by default are
* `'touched'`, `'dirty'`, `'touchedAndDirty'`, `'formIsSubmitted'`.
*/
declare const CUSTOM_ERROR_STATE_MATCHERS: InjectionToken<CustomErrorStateMatchers>;
declare class ShowOnTouchedErrorStateMatcher implements IErrorStateMatcher {
isErrorState(control: AbstractControl | null, form: FormGroupDirective | NgForm | null): boolean;
static ɵfac: i0.ɵɵFactoryDeclaration<ShowOnTouchedErrorStateMatcher, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ShowOnTouchedErrorStateMatcher>;
}
declare class ShowOnDirtyErrorStateMatcher implements IErrorStateMatcher {
isErrorState(control: AbstractControl | null, form: FormGroupDirective | NgForm | null): boolean;
static ɵfac: i0.ɵɵFactoryDeclaration<ShowOnDirtyErrorStateMatcher, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ShowOnDirtyErrorStateMatcher>;
}
declare class ShowOnTouchedAndDirtyErrorStateMatcher implements IErrorStateMatcher {
isErrorState(control: AbstractControl | null, form: FormGroupDirective | NgForm | null): boolean;
static ɵfac: i0.ɵɵFactoryDeclaration<ShowOnTouchedAndDirtyErrorStateMatcher, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ShowOnTouchedAndDirtyErrorStateMatcher>;
}
declare class ShowOnSubmittedErrorStateMatcher implements IErrorStateMatcher {
isErrorState(control: AbstractControl | null, form: FormGroupDirective | NgForm | null): boolean;
static ɵfac: i0.ɵɵFactoryDeclaration<ShowOnSubmittedErrorStateMatcher, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ShowOnSubmittedErrorStateMatcher>;
}
type ProvidedErrorStateMatcherKeys = 'touched' | 'dirty' | 'touchedAndDirty' | 'formIsSubmitted';
type MatchersKeys = ProvidedErrorStateMatcherKeys | string;
declare class ErrorStateMatchers {
private showOnTouchedErrorStateMatcher;
private showOnDirtyErrorStateMatcher;
private showOnTouchedAndDirtyErrorStateMatcher;
private showOnSubmittedErrorStateMatcher;
private customErrorStateMatchers;
private matchers;
constructor();
get(showWhen: string): IErrorStateMatcher | undefined;
validKeys(): string[];
static ɵfac: i0.ɵɵFactoryDeclaration<ErrorStateMatchers, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ErrorStateMatchers>;
}
type LiteralUnionOrString<T> = T | (string & {});
type ShowErrorWhen = LiteralUnionOrString<'touched' | 'dirty' | 'touchedAndDirty' | 'formIsSubmitted'>;
interface IErrorsConfiguration {
/**
* Configures when to display an error for an invalid control. Options that are available by default are listed below. Note, custom options can be provided using CUSTOM_ERROR_STATE_MATCHERS injection token.
*
* `'touched'` - *[default]* shows an error when control is marked as touched. For example, user focused on the input and clicked away or tabbed through the input.
*
* `'dirty'` - shows an error when control is marked as dirty. For example, when user has typed something in.
*
* `'touchedAndDirty'` - shows an error when control is marked as both - touched and dirty.
*
* `'formIsSubmitted'` - shows an error when parent form was submitted.
*/
showErrorsWhenInput?: ShowErrorWhen;
/**
* The maximum amount of errors to display per ngxErrors block.
*/
showMaxErrors?: number | null;
}
type ErrorsConfiguration = Required<IErrorsConfiguration>;
declare const ERROR_CONFIGURATION: InjectionToken<Required<IErrorsConfiguration>>;
declare function provideNgxErrorsConfig(config?: IErrorsConfiguration): Provider;
/**
* Directive to provide a validation error for a specific error name.
* Used as a child of ngxErrors directive.
*
* Example:
* ```html
* <div [ngxErrors]="control">
* <div ngxError="required">This input is required</div>
* </div>
* ```
*/
declare class ErrorDirective implements AfterViewInit, OnDestroy {
private subs;
private config;
private errorStateMatchers;
private errorsDirective;
private templateRef;
private viewContainerRef;
private cdr;
private view;
private errorDirectiveId;
errorName: i0.InputSignal<string>;
showWhen: i0.InputSignal<ShowErrorWhen>;
private computedShowWhen;
private errorStateMatcher;
private controlState$;
/**
* Calculates whether the error could be shown based on the result of
* ErrorStateMatcher and whether there is an error for this particular errorName
* The calculation does not take into account config.showMaxErrors
*
* In addition, it observable produces a side-effect of updating NgxErrorsStateService
* with the information of whether this directive could be shown and a side-effect
* of updating err object in case it was mutated
*/
private couldBeShown$;
private subscribeToCouldBeShown;
/**
* Determines whether the error is shown to the user based on
* the value of couldBeShown and the config.showMaxErrors.
* In addition, this reacts to the changes in visibility for all
* errors associated with the control
*/
private isShown;
private isShownEffect;
hidden: boolean;
err: any;
ngAfterViewInit(): void;
ngOnDestroy(): void;
private toggleVisibility;
private validateDirective;
static ɵfac: i0.ɵɵFactoryDeclaration<ErrorDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ErrorDirective, "[ngxError]", ["ngxError"], { "errorName": { "alias": "ngxError"; "required": true; "isSignal": true; }; "showWhen": { "alias": "ngxErrorShowWhen"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
declare abstract class NgxErrorsBase {
private errorsState;
private formDirective;
parentControlContainer: ControlContainer | null;
showWhen: i0.InputSignal<ShowErrorWhen | undefined>;
abstract resolvedControl: Signal<AbstractControl<any, any> | undefined>;
controlState: Signal<{
control: AbstractControl;
parentForm: _angular_forms.FormGroupDirective | _angular_forms.NgForm | null;
watchedEvents$: rxjs.Observable<any>;
registeredInstancesCount: number;
errors: i0.WritableSignal<Record<number, boolean>>;
} | undefined>;
private registerResolvedControl;
static ɵfac: i0.ɵɵFactoryDeclaration<NgxErrorsBase, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<NgxErrorsBase, never, never, { "showWhen": { "alias": "showWhen"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
type MaybeParentForm = FormGroupDirective | NgForm | null;
declare class NgxErrorsFormDirective {
ngForm: NgForm | null;
formGroupDirective: FormGroupDirective | null;
get form(): MaybeParentForm;
static ɵfac: i0.ɵɵFactoryDeclaration<NgxErrorsFormDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<NgxErrorsFormDirective, "form", ["ngxErrorsForm"], {}, {}, never, never, true, never>;
}
type HasError = boolean;
type DirectiveId = number;
declare class AllErrorsStateService {
private state;
registerControl(control: AbstractControl, parentForm: MaybeParentForm): void;
unregisterControl(control: AbstractControl): void;
getControlState(control: AbstractControl): {
control: AbstractControl;
parentForm: FormGroupDirective | NgForm | null;
watchedEvents$: Observable<any>;
registeredInstancesCount: number;
errors: WritableSignal<Record<DirectiveId, HasError>>;
} | undefined;
static ɵfac: i0.ɵɵFactoryDeclaration<AllErrorsStateService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<AllErrorsStateService>;
}
declare function getErrorStateMatcher(errorStateMatchers: ErrorStateMatchers, showWhen: string): _ngspot_ngx_errors.IErrorStateMatcher;
/**
* Directive to hook into the errors of a given control.
*
* Example:
*
* ```ts
* \@Component({
* template: `
* <div [ngxErrors]="myControl">
* <div ngxError="required">This input is required</div>
* </div>
* `
* })
* export class MyComponent {
* myControl = new FormControl('', Validators.required)
* }
* ```
* In case the `ngxErrors` directive is a child of a [formGroup], you can specify
* the control by the control name similarly how you'd do it with formControlName:
*
* ```ts
* \@Component({
* template: `
* <form [formGroup]="form">
* <div ngxErrors="firstName">
* <div ngxError="required">This input is required</div>
* </div>
* </form>
* `
* })
* export class MyComponent {
* form = this.fb.group({
* firstName: ['', Validators.required]
* });
* constructor(private fb: FormBuilder) {}
* }
* ```
*/
declare class ErrorsDirective extends NgxErrorsBase implements AfterViewInit {
controlInput: i0.InputSignal<string | AbstractControl<any, any, any>>;
resolvedControl: i0.Signal<AbstractControl<any, any, any> | undefined>;
private afterViewInitComplete;
ngAfterViewInit(): void;
private isAbstractControl;
static ɵfac: i0.ɵɵFactoryDeclaration<ErrorsDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ErrorsDirective, "[ngxErrors]", ["ngxErrors"], { "controlInput": { "alias": "ngxErrors"; "required": true; "isSignal": true; }; }, {}, never, never, true, never>;
}
declare const NGX_ERRORS_DECLARATIONS: readonly [typeof ErrorsDirective, typeof ErrorDirective, typeof NgxErrorsFormDirective];
/**
* Extract arguments of function
*/
type ArgumentsType<F> = F extends (...args: infer A) => any ? A : never;
/**
* Marks the provided control as well as all of its children as dirty
* @param options to be passed into control.markAsDirty() call
*/
declare function markDescendantsAsDirty(control: AbstractControl, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
declare function filterOutNullish<T>(): UnaryFunction<Observable<T | null | undefined>, Observable<T>>;
declare class NgxError extends Error {
constructor(message: string);
}
declare class ValueMustBeStringError extends NgxError {
constructor();
}
declare class NoControlError extends NgxError {
constructor();
}
declare class ControlInstanceError extends NgxError {
constructor();
}
declare class ControlNotFoundError extends NgxError {
constructor(name: string);
}
declare class ParentFormGroupNotFoundError extends NgxError {
constructor(name: string);
}
declare class InvalidShowWhenError extends NgxError {
constructor(showWhen: string, keys: string[]);
}
interface DependentValidatorOptions<T> {
/**
* Function that returns AbstractControl to watch
* @param form - the root FormGroup of the control being validated
*/
watchControl: (form?: AbstractControl) => AbstractControl;
/**
* @param watchControlValue - the value of the control being watched
* @returns ValidatorFn. Ex: Validators.required
*/
validator: (watchControlValue?: T) => ValidatorFn;
/**
* If the condition is provided, it must return true in order for the
* validator to be applied.
* @param watchControlValue - the value of the control being watched
*/
condition?: (watchControlValue?: T) => boolean;
}
/**
* Makes it easy to trigger validation on the control, that depends on
* a value of a different control
*/
declare function dependentValidator<T = any>(opts: DependentValidatorOptions<T>): (formControl: AbstractControl) => _angular_forms.ValidationErrors | null;
export { AllErrorsStateService, CUSTOM_ERROR_STATE_MATCHERS, ControlInstanceError, ControlNotFoundError, ERROR_CONFIGURATION, ErrorDirective, ErrorStateMatchers, ErrorsDirective, InvalidShowWhenError, NGX_ERRORS_DECLARATIONS, NgxError, NgxErrorsBase, NgxErrorsFormDirective, NoControlError, ParentFormGroupNotFoundError, ShowOnDirtyErrorStateMatcher, ShowOnSubmittedErrorStateMatcher, ShowOnTouchedAndDirtyErrorStateMatcher, ShowOnTouchedErrorStateMatcher, ValueMustBeStringError, dependentValidator, filterOutNullish, getErrorStateMatcher, markDescendantsAsDirty, provideNgxErrorsConfig };
export type { ArgumentsType, CustomErrorStateMatchers, DependentValidatorOptions, ErrorsConfiguration, IErrorStateMatcher, IErrorsConfiguration, MatchersKeys, MaybeParentForm, ProvidedErrorStateMatcherKeys, ShowErrorWhen };