Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: expose setFieldTouched #50

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/core/src/composables/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
UseFormRegister,
UseFormReturn,
UseFormSetFieldError,
UseFormsSetFieldTouched,
ValidateField,
} from '../types';

Expand Down Expand Up @@ -252,7 +253,7 @@ export function useForm<
};
};

const setFieldTouched = (name: string, touched = true) => {
const setFieldTouched: UseFormsSetFieldTouched<Values> = (name: string, touched = true) => {
dispatch({
type: ACTION_TYPE.SET_TOUCHED,
payload: {
Expand Down Expand Up @@ -631,6 +632,7 @@ export function useForm<
setFieldValue,
setErrors,
setFieldError,
setFieldTouched,
handleSubmit,
handleReset,
resetForm,
Expand All @@ -647,6 +649,7 @@ export function useForm<
getFieldAttrs,
registerFieldArray,
setFieldArrayValue,
setFieldTouched,
register,
});

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/composables/useInternalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SetFieldArrayValue,
UseFormRegister,
UseFormSetFieldValue,
UseFormsSetFieldTouched
} from '../types';

function injectMaybeSelf<T>(
Expand Down Expand Up @@ -52,6 +53,7 @@ export interface InternalContextValues<Values extends FormValues> {
getFieldAttrs: (name: MaybeRefOrGetter<string>) => ComputedRef<FieldAttrs>;

setFieldArrayValue: SetFieldArrayValue<FormValues>;
setFieldTouched: UseFormsSetFieldTouched<FormValues>;

register: UseFormRegister<FormValues>;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ export type UseFormSetFieldError<Values extends FormValues> = <
error: FormErrors<PathValue<Values, Name>> | string | string[],
) => void;

export type UseFormsSetFieldTouched<Values extends FormValues> = <
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use UseFormSetFieldTouched instead of UseFormSetsFieldTouched

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Name extends Path<Values>,
>(
name: Name,
touched: boolean,
) => Promise<FormErrors<Values>> | Promise<void>

export interface FormResetState<Values extends FormValues = FormValues> {
values: Values;
touched: FormTouched<Values>;
Expand All @@ -138,6 +145,7 @@ export interface UseFormReturn<Values extends FormValues> {
setFieldValue: UseFormSetFieldValue<Values>;
setErrors: (errors: FormErrors<Values>) => void;
setFieldError: UseFormSetFieldError<Values>;
setFieldTouched: UseFormsSetFieldTouched<Values>;
handleSubmit: (event?: Event) => void;
handleReset: (event?: Event) => void;
resetForm: ResetForm<Values>;
Expand Down