Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch import to import type where only type is used #67

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions projects/ngx-mat-errors/src/lib/error-messages.ts

This file was deleted.

6 changes: 3 additions & 3 deletions projects/ngx-mat-errors/src/lib/locales/en.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { formatDate } from '@angular/common';
import { FactoryProvider, LOCALE_ID } from '@angular/core';
import {
import { type FactoryProvider, LOCALE_ID } from '@angular/core';
import type {
DatepickerParseError,
EndDateError,
ErrorMessages,
LengthError,
MaxError,
MinError,
StartDateError,
} from '../error-messages';
} from '../types';
import { NGX_MAT_ERROR_DEFAULT_OPTIONS } from '../ngx-mat-errors.component';

export function errorMessagesEnFactory(
Expand Down
6 changes: 3 additions & 3 deletions projects/ngx-mat-errors/src/lib/locales/hu.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { formatDate } from '@angular/common';
import { FactoryProvider, LOCALE_ID } from '@angular/core';
import {
import { type FactoryProvider, LOCALE_ID } from '@angular/core';
import type {
DatepickerParseError,
EndDateError,
ErrorMessages,
LengthError,
MaxError,
MinError,
StartDateError,
} from '../error-messages';
} from '../types';
import { NGX_MAT_ERROR_DEFAULT_OPTIONS } from '../ngx-mat-errors.component';

export function errorMessagesHuFactory(
Expand Down
26 changes: 14 additions & 12 deletions projects/ngx-mat-errors/src/lib/locales/pt-br.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { formatDate } from '@angular/common';
import { FactoryProvider, LOCALE_ID } from '@angular/core';
import { DatepickerParseError, EndDateError, ErrorMessages, LengthError, MaxError, MinError, StartDateError } from '../error-messages';
import { type FactoryProvider, LOCALE_ID } from '@angular/core';
import type {
DatepickerParseError,
EndDateError,
ErrorMessages,
LengthError,
MaxError,
MinError,
StartDateError,
} from '../types';
import { NGX_MAT_ERROR_DEFAULT_OPTIONS } from '../ngx-mat-errors.component';

export function errorMessagesPtBtFactory(
locale: string,
format = 'shortDate'
): ErrorMessages {
return {
min: (error: MinError) =>
`Informe um valor igual ou maior a ${error.min}.`,
max: (error: MaxError) =>
`Informe um valor igual ou menor a ${error.max}.`,
min: (error: MinError) => `Informe um valor igual ou maior a ${error.min}.`,
max: (error: MaxError) => `Informe um valor igual ou menor a ${error.max}.`,
required: `Campo obrigatório.`,
email: `Informe um endereço de email válido.`,
minlength: (error: LengthError) =>
Expand All @@ -20,15 +26,11 @@ export function errorMessagesPtBtFactory(
`O campo não pode ter mais que ${error.requiredLength} caracteres.`,
matDatepickerMin: (error: MinError<Date>) => {
const formatted = formatDate(error.min, format, locale);
return `Informe uma data maior ou igual a ${
formatted ?? error.min
}.`;
return `Informe uma data maior ou igual a ${formatted ?? error.min}.`;
},
matDatepickerMax: (error: MaxError<Date>) => {
const formatted = formatDate(error.max, format, locale);
return `Informe uma data menor ou igual a ${
formatted ?? error.max
}.`;
return `Informe uma data menor ou igual a ${formatted ?? error.max}.`;
},
matDatepickerParse: (error: DatepickerParseError) => `Invalid date format.`,
matStartDateInvalid: (error: StartDateError<Date>) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AfterContentInit, Directive, inject } from '@angular/core';
import { MatDateRangeInput } from '@angular/material/datepicker';
import { type AfterContentInit, Directive, inject } from '@angular/core';
import type { MatDateRangeInput } from '@angular/material/datepicker';
import { MAT_FORM_FIELD } from '@angular/material/form-field';
import { NgxMatErrors } from './ngx-mat-errors.component';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ChangeDetectionStrategy,
Component,
Input,
Provider,
type Provider,
} from '@angular/core';
import {
ComponentFixture,
Expand All @@ -20,12 +20,12 @@ import { MatInputModule } from '@angular/material/input';
import { MatInputHarness } from '@angular/material/input/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import {
ErrorMessages,
type ErrorMessages,
NGX_MAT_ERROR_DEFAULT_OPTIONS,
NgxMatErrorsModule,
} from 'ngx-mat-errors';
import { delay, from, interval, map, of, take, zip } from 'rxjs';
import { LengthError } from './error-messages';
import type { LengthError } from './types';

const defaultProviders: Provider[] = [
{
Expand Down
18 changes: 12 additions & 6 deletions projects/ngx-mat-errors/src/lib/ngx-mat-errors.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@ import {
ContentChildren,
InjectionToken,
Input,
OnDestroy,
ViewEncapsulation,
inject,
type OnDestroy,
type QueryList,
} from '@angular/core';
import { ValidationErrors } from '@angular/forms';
import { MAT_FORM_FIELD } from '@angular/material/form-field';
import { Observable, ReplaySubject, combineLatest, of } from 'rxjs';
import {
ReplaySubject,
combineLatest,
distinctUntilChanged,
map,
of,
startWith,
switchMap,
} from 'rxjs/operators';
import { ErrorMessages } from './error-messages';
type Observable,
} from 'rxjs';
import {
INgxMatErrorDef,
NGX_MAT_ERROR_DEF,
type INgxMatErrorDef,
} from './ngx-mat-error-def.directive';
import { ErrorTemplate, NgxMatErrorControls } from './types';
import type {
ErrorMessages,
ErrorTemplate,
NgxMatErrorControls,
} from './types';
import { coerceToObservable } from './utils/coerce-to-observable';
import { distinctUntilErrorChanged } from './utils/distinct-until-error-changed';
import { findCustomErrorForControl } from './utils/find-custom-error-for-control';
Expand Down
70 changes: 65 additions & 5 deletions projects/ngx-mat-errors/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { TemplateRef } from '@angular/core';
import {
import type { TemplateRef } from '@angular/core';
import type {
AbstractControl,
AbstractControlDirective,
ValidationErrors,
} from '@angular/forms';
import { MatFormFieldControl } from '@angular/material/form-field';
import type { MatFormFieldControl } from '@angular/material/form-field';

export type ErrorTemplate =
| {
template: TemplateRef<any>;
$implicit: ValidationErrors;
control?: AbstractControlDirective | AbstractControl;
}
| {
template: undefined;
Expand All @@ -21,8 +20,69 @@ export type ErrorTemplate =
export type NgxMatErrorControls =
| MatFormFieldControl<any>
| AbstractControl
| (AbstractControl | AbstractControlDirective)[]
| AbstractControlDirective
| (AbstractControl | AbstractControlDirective)[]
| undefined
| null
| '';

export type ErrorTransform = (error: any) => string;

export interface ErrorMessages {
[key: string]: string | ErrorTransform;
}

/**
* For errors: 'min', 'matDatepickerMin'
*/
export interface MinError<T = number> {
min: T;
actual: T;
}

/**
* For errors: 'max', 'matDatepickerMax'
*/
export interface MaxError<T = number> {
max: T;
actual: T;
}

/**
* For errors: 'minlength', 'maxlength'
*/
export interface LengthError {
requiredLength: number;
actualLength: number;
}

/**
* For errors: 'pattern'
*/
export interface PatternValidator {
requiredPattern: string;
actualValue: string;
}

/**
* For errors: 'matStartDateInvalid'
*/
export interface StartDateError<D> {
end: D;
actual: D;
}

/**
* For errors: 'matEndDateInvalid'
*/
export interface EndDateError<D> {
start: D;
actual: D;
}

/**
* For errors: 'matDatepickerParse'
*/
export interface DatepickerParseError {
text: string;
}
4 changes: 2 additions & 2 deletions projects/ngx-mat-errors/src/lib/utils/coerce-to-observable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable, isObservable, of, } from 'rxjs';
import type { ErrorMessages } from '../error-messages';
import { type Observable, isObservable, of, } from 'rxjs';
import type { ErrorMessages } from '../types';

export function coerceToObservable(
errorMessages: ErrorMessages | Observable<ErrorMessages>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TemplateRef } from '@angular/core';
import { MaxError, MinError } from '../error-messages';
import { ErrorTemplate } from '../types';
import type { TemplateRef } from '@angular/core';
import type { MaxError, MinError, ErrorTemplate } from '../types';
import { distinctUntilErrorChanged } from './distinct-until-error-changed';

describe('distinctUntilErrorChanged', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorTemplate } from '../types';
import type { ErrorTemplate } from '../types';

export function distinctUntilErrorChanged<P extends ErrorTemplate>(
prev: P,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { type AbstractControl } from '@angular/forms';
import { type INgxMatErrorDef } from '../ngx-mat-error-def.directive';
import type { AbstractControl } from '@angular/forms';
import type { INgxMatErrorDef } from '../ngx-mat-error-def.directive';

/**
* Find the custom error for a control.
*
* @returns INgxMatErrorDef | undefined
*/
export function findCustomErrorForControl(
errorKeys: string[],
customErrorMessages: INgxMatErrorDef[],
errorKeys: readonly string[],
customErrorMessages: readonly INgxMatErrorDef[],
control: AbstractControl
) {
return customErrorMessages.find((customErrorMessage) => {
return errorKeys.some((error) => {
if (error !== customErrorMessage.ngxMatErrorDefFor) return false;
return customErrorMessages.find((customErrorMessage) =>
errorKeys.some((error) => {
if (error !== customErrorMessage.ngxMatErrorDefFor) {
return false;
}
return (
!customErrorMessage.control || customErrorMessage.control === control
);
});
});
})
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { coerceArray } from '@angular/cdk/coercion';
import { AbstractControl, AbstractControlDirective } from '@angular/forms';
import { NgxMatErrorControls } from '../types';
import type { NgxMatErrorControls } from '../types';

export function getAbstractControls(
controls: NgxMatErrorControls
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractControl } from '@angular/forms';
import type { AbstractControl } from '@angular/forms';
import {
Observable,
type Observable,
pairwise,
filter,
merge,
Expand Down
3 changes: 1 addition & 2 deletions projects/ngx-mat-errors/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
* Public API Surface of ngx-mat-errors
*/

export * from './lib/error-messages';
export * from './lib/locales';
export * from './lib/ngx-mat-error-def.directive';
export * from './lib/ngx-mat-errors-for-date-range-picker.directive';
export * from './lib/ngx-mat-errors.component';
export * from './lib/ngx-mat-errors.module';

export type * from './lib/types';