Skip to content

Commit 7382686

Browse files
committed
fix(ui): fix ts of FormGroup
1 parent d2e198c commit 7382686

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

packages/ui/src/components/form/FormGroup.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AbstractControl } from './abstract-control';
12
import type { FormGroup } from './form-group';
23

34
import React from 'react';
@@ -9,7 +10,7 @@ export const DFormGroupContext = React.createContext<FormGroup | null>(null);
910

1011
export interface DFormGroupProps {
1112
children: React.ReactNode;
12-
dFormGroup: FormGroup;
13+
dFormGroup: AbstractControl;
1314
dTitle?: React.ReactNode;
1415
}
1516

@@ -18,7 +19,7 @@ export function DFormGroup(props: DFormGroupProps): JSX.Element | null {
1819
const { children, dFormGroup, dTitle } = useComponentConfig(COMPONENT_NAME, props);
1920

2021
return (
21-
<DFormGroupContext.Provider value={dFormGroup}>
22+
<DFormGroupContext.Provider value={dFormGroup as any as FormGroup}>
2223
{dTitle}
2324
{children}
2425
</DFormGroupContext.Provider>

packages/ui/src/components/form/form-group.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type GetFormControlProperty<T, S> = S extends `${infer K}.${infer R}`
2121
? T[S]
2222
: any;
2323

24+
type GetFormGroupT<C extends FormGroup<any>> = C extends FormGroup<infer T> ? T : unknown;
25+
type GetFormGroupValue<T extends { [K in keyof T]: AbstractControl }> = {
26+
[K in keyof T]?: T[K] extends FormGroup ? GetFormGroupValue<GetFormGroupT<T[K]>> : T[K]['value'];
27+
};
28+
2429
function find(control: AbstractControl, path: string[] | string, delimiter: string) {
2530
if (path == null) return null;
2631

@@ -107,15 +112,15 @@ export class FormGroup<T extends { [K in keyof T]: AbstractControl } = any> exte
107112
});
108113
this.updateValueAndValidity(onlySelf);
109114
}
110-
override patchValue(value: { [K in keyof T]?: T[K]['value'] } & { [K: string]: any }, onlySelf?: boolean): void {
115+
override patchValue(value: GetFormGroupValue<T> & { [K: string]: any }, onlySelf?: boolean): void {
111116
Object.keys(value).forEach((name) => {
112117
if (this.controls[name]) {
113118
this.controls[name].patchValue(value[name], true);
114119
}
115120
});
116121
this.updateValueAndValidity(onlySelf);
117122
}
118-
override reset(value: { [K in keyof T]?: T[K]['value'] } & { [K: string]: any } = {}, onlySelf?: boolean): void {
123+
override reset(value: GetFormGroupValue<T> & { [K: string]: any } = {}, onlySelf?: boolean): void {
119124
this._forEachChild((control, name) => {
120125
control.reset(value[name], true);
121126
});

0 commit comments

Comments
 (0)