I'd like to rewrite some of my forms using FormGroups to enable the easier bulk validation of inputs, etc. My problem is that I have been using RxJs to gather value changes and start to get data from the server according to the new values as these input fields are mostly datatable column filters.
What I have so far:
FormGroup:
private inputGroup = new FormGroup({
fieldA: new FormControl(),
fieldB: new FormControl(),
fieldC1: new FormControl(),
fieldC2: new FormControl(),
});
Inputs:
<input class="form-control" type="text" name="fieldA" formControlName="fieldA" />
<input class="form-control" type="text" name="fieldB" formControlName="fieldB" />
<input class="form-control" type="text" name="fieldC1" formControlName="fieldC1" />
<input class="form-control" type="text" name="fieldC2" formControlName="fieldC2" />
An Observable:
private search$ = new Subject<string[] | undefined>();
A method that gathers all parameters into the needed Observable:
protected getCombinedParams(): Observable<GetParams> {
return combineLatest([
...this.combineDefaultsBase,
this.search$,
]).pipe(
map(([PageIndex, PageSize, OrderColumns, searchValues]) => ({
PageIndex, PageSize, OrderColumns,
FieldA: searchValues['fieldA'],
FieldB: searchValues['fieldA'],
FieldC: [searchValues['fieldC1'], searchValues['fieldC2']]
} as GetParams)));
}
this.combineDefaultsBase is in a base class and contains basic parameters for sorting, number of rows per page and page number.
I have the inputGroup valuechanges trigger my observable:
this.inputGroup.valueChanges.pipe(
debounceTime(defaultInputDebounceTime)
).subscribe(newValue => {
this.search$.next(newValue);
});
The GetParams class:
export interface GetParams {
FieldA?: string;
FieldB?: string;
FieldC?: string[];
}
What I want to be able to do is to map the values from the FormGroup array got from valuechanges to the desired GetParams values. Ts is complaining right now on the FieldA: searchValues['fieldA']
line saying
(TS) Element implicitly has an 'any' type because expression of type '"fieldA"' can't be used to index type 'number | string[]'. Property 'fieldA' does not exist on type 'number | string[]'.
How said that it's a number? Where is it coming from?