Skip to content

Commit

Permalink
Revert "🔧 fix data param in handleSubmit when TTransformedValues is a…
Browse files Browse the repository at this point in the history
… union type (#11420)"

This reverts commit 6fee03c.
  • Loading branch information
bluebill1049 committed Feb 16, 2024
1 parent 73d7c10 commit 8b12f99
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
18 changes: 9 additions & 9 deletions reports/api-extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type ChangeHandler = (event: {
}) => Promise<void | boolean>;

// @public (undocumented)
export type Control<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues = TFieldValues> = {
export type Control<TFieldValues extends FieldValues = FieldValues, TContext = any> = {
_subjects: Subjects<TFieldValues>;
_removeUnmounted: Noop;
_names: Names;
Expand Down Expand Up @@ -76,7 +76,7 @@ export type Control<TFieldValues extends FieldValues = FieldValues, TContext = a
errors: FieldErrors;
}>;
register: UseFormRegister<TFieldValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues>;
_disableForm: (disabled?: boolean) => void;
unregister: UseFormUnregister<TFieldValues>;
getFieldState: UseFormGetFieldState<TFieldValues>;
Expand Down Expand Up @@ -286,10 +286,10 @@ export type FormProps<TFieldValues extends FieldValues, TTransformedValues exten
}>;

// @public
export const FormProvider: <TFieldValues extends FieldValues, TContext = any, TTransformedValues extends FieldValues = TFieldValues>(props: FormProviderProps<TFieldValues, TContext, TTransformedValues>) => React_2.JSX.Element;
export const FormProvider: <TFieldValues extends FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props: FormProviderProps<TFieldValues, TContext, TTransformedValues>) => React_2.JSX.Element;

// @public (undocumented)
export type FormProviderProps<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues = TFieldValues> = {
export type FormProviderProps<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined> = {
children: React_2.ReactNode | React_2.ReactNode[];
} & UseFormReturn<TFieldValues, TContext, TTransformedValues>;

Expand Down Expand Up @@ -647,13 +647,13 @@ export type UseFieldArraySwap = (indexA: number, indexB: number) => void;
export type UseFieldArrayUpdate<TFieldValues extends FieldValues, TFieldArrayName extends FieldArrayPath<TFieldValues> = FieldArrayPath<TFieldValues>> = (index: number, value: FieldArray<TFieldValues, TFieldArrayName>) => void;

// @public
export function useForm<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues = TFieldValues>(props?: UseFormProps<TFieldValues, TContext>): UseFormReturn<TFieldValues, TContext, TTransformedValues>;
export function useForm<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseFormProps<TFieldValues, TContext>): UseFormReturn<TFieldValues, TContext, TTransformedValues>;

// @public
export type UseFormClearErrors<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[] | `root.${string}` | 'root') => void;

// @public
export const useFormContext: <TFieldValues extends FieldValues, TContext = any, TransformedValues extends FieldValues = TFieldValues>() => UseFormReturn<TFieldValues, TContext, TransformedValues>;
export const useFormContext: <TFieldValues extends FieldValues, TContext = any, TransformedValues extends FieldValues | undefined = undefined>() => UseFormReturn<TFieldValues, TContext, TransformedValues>;

// @public
export type UseFormGetFieldState<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName, formState?: FormState<TFieldValues>) => {
Expand All @@ -671,7 +671,7 @@ export type UseFormGetValues<TFieldValues extends FieldValues> = {
};

// @public
export type UseFormHandleSubmit<TFieldValues extends FieldValues, TTransformedValues extends FieldValues = TFieldValues> = (onValid: SubmitHandler<TTransformedValues>, onInvalid?: SubmitErrorHandler<TFieldValues>) => (e?: React_2.BaseSyntheticEvent) => Promise<void>;
export type UseFormHandleSubmit<TFieldValues extends FieldValues, TTransformedValues extends FieldValues | undefined = undefined> = (onValid: TTransformedValues extends undefined ? SubmitHandler<TFieldValues> : TTransformedValues extends FieldValues ? SubmitHandler<TTransformedValues> : never, onInvalid?: SubmitErrorHandler<TFieldValues>) => (e?: React_2.BaseSyntheticEvent) => Promise<void>;

// @public (undocumented)
export type UseFormProps<TFieldValues extends FieldValues = FieldValues, TContext = any> = Partial<{
Expand Down Expand Up @@ -724,7 +724,7 @@ export type UseFormResetField<TFieldValues extends FieldValues> = <TFieldName ex
}>) => void;

// @public (undocumented)
export type UseFormReturn<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues = TFieldValues> = {
export type UseFormReturn<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined> = {
watch: UseFormWatch<TFieldValues>;
getValues: UseFormGetValues<TFieldValues>;
getFieldState: UseFormGetFieldState<TFieldValues>;
Expand All @@ -737,7 +737,7 @@ export type UseFormReturn<TFieldValues extends FieldValues = FieldValues, TConte
reset: UseFormReset<TFieldValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues>;
unregister: UseFormUnregister<TFieldValues>;
control: Control<TFieldValues, TContext, TTransformedValues>;
control: Control<TFieldValues, TContext>;
register: UseFormRegister<TFieldValues>;
setFocus: UseFormSetFocus<TFieldValues>;
};
Expand Down
17 changes: 0 additions & 17 deletions src/__typetest__/form.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ import { useForm } from '../useForm';

handleSubmit((data) => expectType<{ test: string; test1: number }>(data));
}

/** it should infer the correct TTransformedValues from useForm generic */ {
/* eslint-disable react-hooks/rules-of-hooks */
const { handleSubmit } = useForm<
{ test: string },
unknown,
{ test: string } | { test1: number }
>();

handleSubmit((data) => {
// @ts-expect-error `data` should be union and thus should not be assignable to `{ test: string }`
expectType<{ test: string }>(data);
// @ts-expect-error `data` should be union and thus should not be assignable to `{ test1: number }`
expectType<{ test1: number }>(data);
expectType<{ test: string } | { test1: number }>(data);
});
}
}

/** {@link UseFormGetFieldState} */ {
Expand Down
10 changes: 3 additions & 7 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,10 @@ const defaultOptions = {
export function createFormControl<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
>(
props: UseFormProps<TFieldValues, TContext> = {},
flushRootRender: () => void,
): Omit<
UseFormReturn<TFieldValues, TContext, TTransformedValues>,
'formState'
> {
): Omit<UseFormReturn<TFieldValues, TContext>, 'formState'> {
let _options = {
...defaultOptions,
...props,
Expand Down Expand Up @@ -1099,7 +1095,7 @@ export function createFormControl<
}
};

const handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues> =
const handleSubmit: UseFormHandleSubmit<TFieldValues> =
(onValid, onInvalid) => async (e) => {
let onValidError = undefined;
if (e) {
Expand Down Expand Up @@ -1127,7 +1123,7 @@ export function createFormControl<
errors: {},
});
try {
await onValid(fieldValues as TFieldValues & TTransformedValues, e);
await onValid(fieldValues as TFieldValues, e);
} catch (error) {
onValidError = error;
}
Expand Down
8 changes: 2 additions & 6 deletions src/logic/getProxyFormState.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { VALIDATION_MODE } from '../constants';
import { Control, FieldValues, FormState, ReadFormState } from '../types';

export default <
TFieldValues extends FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
>(
export default <TFieldValues extends FieldValues, TContext = any>(
formState: FormState<TFieldValues>,
control: Control<TFieldValues, TContext, TTransformedValues>,
control: Control<TFieldValues, TContext>,
localProxyFormState?: ReadFormState,
isRoot = true,
) => {
Expand Down
17 changes: 10 additions & 7 deletions src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,13 @@ export type UseFormUnregister<TFieldValues extends FieldValues> = (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type UseFormHandleSubmit<
TFieldValues extends FieldValues,
TTransformedValues extends FieldValues = TFieldValues,
TTransformedValues extends FieldValues | undefined = undefined,
> = (
onValid: SubmitHandler<TTransformedValues>,
onValid: TTransformedValues extends undefined
? SubmitHandler<TFieldValues>
: TTransformedValues extends FieldValues
? SubmitHandler<TTransformedValues>
: never,
onInvalid?: SubmitErrorHandler<TFieldValues>,
) => (e?: React.BaseSyntheticEvent) => Promise<void>;

Expand Down Expand Up @@ -759,7 +763,6 @@ export type BatchFieldArrayUpdate = <
export type Control<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
> = {
_subjects: Subjects<TFieldValues>;
_removeUnmounted: Noop;
Expand Down Expand Up @@ -806,7 +809,7 @@ export type Control<
names: InternalFieldName[],
) => Promise<{ errors: FieldErrors }>;
register: UseFormRegister<TFieldValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues>;
_disableForm: (disabled?: boolean) => void;
unregister: UseFormUnregister<TFieldValues>;
getFieldState: UseFormGetFieldState<TFieldValues>;
Expand All @@ -824,7 +827,7 @@ export type WatchObserver<TFieldValues extends FieldValues> = (
export type UseFormReturn<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
TTransformedValues extends FieldValues | undefined = undefined,
> = {
watch: UseFormWatch<TFieldValues>;
getValues: UseFormGetValues<TFieldValues>;
Expand All @@ -838,7 +841,7 @@ export type UseFormReturn<
reset: UseFormReset<TFieldValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues>;
unregister: UseFormUnregister<TFieldValues>;
control: Control<TFieldValues, TContext, TTransformedValues>;
control: Control<TFieldValues, TContext>;
register: UseFormRegister<TFieldValues>;
setFocus: UseFormSetFocus<TFieldValues>;
};
Expand Down Expand Up @@ -870,7 +873,7 @@ export type UseWatchProps<TFieldValues extends FieldValues = FieldValues> = {
export type FormProviderProps<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
TTransformedValues extends FieldValues | undefined = undefined,
> = {
children: React.ReactNode | React.ReactNode[];
} & UseFormReturn<TFieldValues, TContext, TTransformedValues>;
Expand Down
2 changes: 1 addition & 1 deletion src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { useSubscribe } from './useSubscribe';
export function useForm<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
TTransformedValues extends FieldValues | undefined = undefined,
>(
props: UseFormProps<TFieldValues, TContext> = {},
): UseFormReturn<TFieldValues, TContext, TTransformedValues> {
Expand Down
4 changes: 2 additions & 2 deletions src/useFormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const HookFormContext = React.createContext<UseFormReturn | null>(null);
export const useFormContext = <
TFieldValues extends FieldValues,
TContext = any,
TransformedValues extends FieldValues = TFieldValues,
TransformedValues extends FieldValues | undefined = undefined,
>(): UseFormReturn<TFieldValues, TContext, TransformedValues> =>
React.useContext(HookFormContext) as UseFormReturn<
TFieldValues,
Expand Down Expand Up @@ -78,7 +78,7 @@ export const useFormContext = <
export const FormProvider = <
TFieldValues extends FieldValues,
TContext = any,
TTransformedValues extends FieldValues = TFieldValues,
TTransformedValues extends FieldValues | undefined = undefined,
>(
props: FormProviderProps<TFieldValues, TContext, TTransformedValues>,
) => {
Expand Down

0 comments on commit 8b12f99

Please sign in to comment.