I have this string field component "import { ChangeDetectionStrategy, Component,…
- User
I have this string field component "import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject, input, output, signal, } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { FormGroup, ReactiveFormsModule, FormControl } from '@angular/forms'; import { fieldGeneratorMap, GeneratorType } from '../schema-config.model'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatSelectModule } from '@angular/material/select'; import { MatInputModule } from '@angular/material/input'; import { SchemaFormFieldGeneratorService } from '../../shared/services/schema-form-field-generator.service'; import { compareToOtherFieldValidator } from '../../shared/validators/compare-to-other-field-validator.validator'; import { startWith } from 'rxjs'; import { matchingArrayLengthsValidator } from '../../shared/validators/matching-array-lenghts-validator.validator'; import { TitleCasePipe } from '@angular/common'; import { RepeatMaskDirective } from '../../shared/directives/repeat-mask.directive'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { ErrorMessagePipe } from '../../shared/pipes/error-message.pipe'; import { uniqueCommaValuesValidator } from '../../shared/validators/unique-comma-values-validator.validator';
type StringFieldGenerator = | { type: GeneratorType.Enum; values: string[]; weights: number[] } | { type: GeneratorType.Values; values: string[] } | { type: GeneratorType.Text; min_chars: number; max_chars: number; } | { type: GeneratorType.GptText; description: string; min_chars: number; max_chars: number; } | { type: GeneratorType.GptEnum; values: string[]; weights: number[] };
interface StringFieldForm { type: FormControl; minChars?: FormControl; maxChars?: FormControl; values?: FormControl; weights?: FormControl; description?: FormControl; }
@Component({ selector: 'app-string-field', standalone: true, imports: [ ReactiveFormsModule, MatFormFieldModule, MatSelectModule, MatInputModule, TitleCasePipe, RepeatMaskDirective, MatIconModule, MatButtonModule, ErrorMessagePipe, ], templateUrl: './string-field.component.html', styleUrl: './string-field.component.scss', changeDetection: ChangeDetectionStrategy.OnPush, }) export class StringFieldComponent implements OnInit { private readonly schemaFormFieldGeneratorSvc = inject( SchemaFormFieldGeneratorService, ); private readonly destroyRef = inject(DestroyRef);
private matchingValuesWeightsValidator = matchingArrayLengthsValidator([ 'weights', ]); private matchingWeightsValuesValidator = matchingArrayLengthsValidator([ 'values', ]);
public readonly autoRenew = signal(true);
public readonly generatorTypes = input.required();
public readonly changeField = output<{ schema: StringFieldGenerator | undefined; valid: boolean; }>();
public uniqueCommaValuesValidator = uniqueCommaValuesValidator;
public readonly type = this.schemaFormFieldGeneratorSvc.getTypeField({ value: GeneratorType.Enum, }); public readonly minChars = this.schemaFormFieldGeneratorSvc.getMinField({ validators: [ compareToOtherFieldValidator( 'maxChars', (min: number, max: number) => +min < +max, 'minGreaterThanMax', ), ], }); public readonly maxChars = this.schemaFormFieldGeneratorSvc.getMaxField({ validators: [ compareToOtherFieldValidator( 'minChars', (max: number, min: number) => +min < +max, 'MaxSmallerThanMin', ), ], });
- Assistant