Skip to content

Commit

Permalink
fix(ui): fix ts of FormGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Feb 2, 2023
1 parent d2e198c commit 7382686
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/ui/src/components/form/FormGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AbstractControl } from './abstract-control';
import type { FormGroup } from './form-group';

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

export interface DFormGroupProps {
children: React.ReactNode;
dFormGroup: FormGroup;
dFormGroup: AbstractControl;
dTitle?: React.ReactNode;
}

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

return (
<DFormGroupContext.Provider value={dFormGroup}>
<DFormGroupContext.Provider value={dFormGroup as any as FormGroup}>
{dTitle}
{children}
</DFormGroupContext.Provider>
Expand Down
9 changes: 7 additions & 2 deletions packages/ui/src/components/form/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type GetFormControlProperty<T, S> = S extends `${infer K}.${infer R}`
? T[S]
: any;

type GetFormGroupT<C extends FormGroup<any>> = C extends FormGroup<infer T> ? T : unknown;
type GetFormGroupValue<T extends { [K in keyof T]: AbstractControl }> = {
[K in keyof T]?: T[K] extends FormGroup ? GetFormGroupValue<GetFormGroupT<T[K]>> : T[K]['value'];
};

function find(control: AbstractControl, path: string[] | string, delimiter: string) {
if (path == null) return null;

Expand Down Expand Up @@ -107,15 +112,15 @@ export class FormGroup<T extends { [K in keyof T]: AbstractControl } = any> exte
});
this.updateValueAndValidity(onlySelf);
}
override patchValue(value: { [K in keyof T]?: T[K]['value'] } & { [K: string]: any }, onlySelf?: boolean): void {
override patchValue(value: GetFormGroupValue<T> & { [K: string]: any }, onlySelf?: boolean): void {
Object.keys(value).forEach((name) => {
if (this.controls[name]) {
this.controls[name].patchValue(value[name], true);
}
});
this.updateValueAndValidity(onlySelf);
}
override reset(value: { [K in keyof T]?: T[K]['value'] } & { [K: string]: any } = {}, onlySelf?: boolean): void {
override reset(value: GetFormGroupValue<T> & { [K: string]: any } = {}, onlySelf?: boolean): void {
this._forEachChild((control, name) => {
control.reset(value[name], true);
});
Expand Down

0 comments on commit 7382686

Please sign in to comment.