chore: properly type pattern error

This commit is contained in:
waterplea
2023-04-27 14:48:34 +08:00
committed by Aiden McClelland
parent e0ee89bdd9
commit fecbae761e
2 changed files with 27 additions and 18 deletions

View File

@@ -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<ValueSpecText, string>) => ({
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,

View File

@@ -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<ValueSpec, string>) => ({
required: 'Required',
pattern: ({ requiredPattern }: ValidatorsPatternError) =>
('patterns' in control.spec &&
control.spec.patterns.find(
({ regex }) => String(regex) === String(requiredPattern),
)?.description) ||
'Invalid format',
}),
},
]