From fecbae761e88c3777e47abd933096d4179556d09 Mon Sep 17 00:00:00 2001 From: waterplea Date: Thu, 27 Apr 2023 14:48:34 +0800 Subject: [PATCH] chore: properly type pattern error --- .../form-control/form-control.component.ts | 20 ++------------- .../form-control/form-control.providers.ts | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 frontend/projects/ui/src/app/components/form/form-control/form-control.providers.ts diff --git a/frontend/projects/ui/src/app/components/form/form-control/form-control.component.ts b/frontend/projects/ui/src/app/components/form/form-control/form-control.component.ts index d661b8347..472fa947d 100644 --- a/frontend/projects/ui/src/app/components/form/form-control/form-control.component.ts +++ b/frontend/projects/ui/src/app/components/form/form-control/form-control.component.ts @@ -19,30 +19,14 @@ import { TUI_VALIDATION_ERRORS } from '@taiga-ui/kit' import { filter, takeUntil } from 'rxjs' import { ValueSpec, ValueSpecText } from 'start-sdk/lib/config/configTypes' import { ERRORS } from '../form-group/form-group.component' - -interface ValidatorsPatternError { - actualValue: string - requiredPattern: string | RegExp -} +import { FORM_CONTROL_PROVIDERS } from './form-control.providers' @Component({ selector: 'form-control', templateUrl: './form-control.component.html', styleUrls: ['./form-control.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, - providers: [ - { - provide: TUI_VALIDATION_ERRORS, - deps: [FormControlComponent], - useFactory: (control: FormControlComponent) => ({ - required: 'Required', - pattern: ({ requiredPattern }: ValidatorsPatternError) => - control.spec.patterns.find( - ({ regex }) => String(regex) === String(requiredPattern), - )?.description || 'Invalid format', - }), - }, - ], + providers: FORM_CONTROL_PROVIDERS, }) export class FormControlComponent< T extends ValueSpec, diff --git a/frontend/projects/ui/src/app/components/form/form-control/form-control.providers.ts b/frontend/projects/ui/src/app/components/form/form-control/form-control.providers.ts new file mode 100644 index 000000000..ca85ef7f3 --- /dev/null +++ b/frontend/projects/ui/src/app/components/form/form-control/form-control.providers.ts @@ -0,0 +1,25 @@ +import { forwardRef, Provider } from '@angular/core' +import { TUI_VALIDATION_ERRORS } from '@taiga-ui/kit' +import { ValueSpec } from 'start-sdk/lib/config/configTypes' +import { FormControlComponent } from './form-control.component' + +interface ValidatorsPatternError { + actualValue: string + requiredPattern: string | RegExp +} + +export const FORM_CONTROL_PROVIDERS: Provider[] = [ + { + provide: TUI_VALIDATION_ERRORS, + deps: [forwardRef(() => FormControlComponent)], + useFactory: (control: FormControlComponent) => ({ + required: 'Required', + pattern: ({ requiredPattern }: ValidatorsPatternError) => + ('patterns' in control.spec && + control.spec.patterns.find( + ({ regex }) => String(regex) === String(requiredPattern), + )?.description) || + 'Invalid format', + }), + }, +]