Skip to content

Commit

Permalink
Add missing hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer committed Nov 19, 2020
1 parent a7ecaed commit 90fd693
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-olives-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': patch
---

Add hooks `useSetValue`, `useSetErrors`, `useSetTouched`, `useSetStatus`, `useSubmitForm`, `useResetForm`, `useIsSubmitting`, `useIsValid`, `useIsDirty`, `useValidateForm`, `useValidateField`
2 changes: 1 addition & 1 deletion packages/formik/src/ErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useFieldError, useFieldTouched } from './useField';
import { useFieldError, useFieldTouched } from './hooks';
import { isFunction } from './utils';

export interface ErrorMessageProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/formik/src/FastField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GenericFieldHTMLAttributes,
SharedFieldProps,
} from './types';
import { useField, UseFieldProps } from './useField';
import { useField, UseFieldProps } from './hooks';
import { isEmptyChildren, isFunction } from './utils';

export type FastFieldProps<FieldValue = any> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/formik/src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
GenericFieldHTMLAttributes,
SharedFieldProps,
} from './types';
import { useField, UseFieldProps } from './useField';
import { useField, UseFieldProps } from './hooks';
import { isEmptyChildren, isFunction } from './utils';

export interface FieldProps<V = any, FormValues = any> {
Expand Down
129 changes: 128 additions & 1 deletion packages/formik/src/useField.tsx → packages/formik/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,43 @@ export function useTouched<Values>() {
}

/**
* Returns Formik status and updater function
* Returns Formik touched updater function
* @public
*/
export function useSetTouched<Values>() {
const update = useFormikContextSelector<
Values,
FormikContextType<Values>['setTouched']
>(ctx => ctx.setTouched);
return update;
}

/**
* Returns Formik values updater function
* @public
*/
export function useSetValues<Values>() {
const update = useFormikContextSelector<
Values,
FormikContextType<Values>['setValues']
>(ctx => ctx.setValues);
return update;
}

/**
* Returns Formik errors updater function
* @public
*/
export function useSetErrors<Values>() {
const update = useFormikContextSelector<
Values,
FormikContextType<Values>['setErrors']
>(ctx => ctx.setErrors);
return update;
}

/**
* Returns Formik status state and updater function
* @public
*/
export function useStatus<T>() {
Expand All @@ -405,6 +441,97 @@ export function useStatus<T>() {
return [state, update];
}

/**
* Returns Formik status updater function
* @public
*/
export function useSetStatus() {
return useFormikContextSelector<
unknown,
FormikContextType<unknown>['setStatus']
>(ctx => ctx.setStatus);
}

/**
* Returns a function to imperatively submit the form
* @public
*/
export function useSubmitForm() {
return useFormikContextSelector<
unknown,
FormikContextType<unknown>['submitForm']
>(ctx => ctx.submitForm);
}

/**
* Returns whether the form submission is currently being attempted
* @public
*/
export function useIsSubmitting() {
return useFormikContextSelector<
unknown,
FormikContextType<unknown>['isSubmitting']
>(ctx => ctx.isSubmitting);
}

/**
* Returns function to reset the form
* @public
*/
export function useResetForm() {
return useFormikContextSelector<
unknown,
FormikContextType<unknown>['resetForm']
>(ctx => ctx.resetForm);
}

/**
*
* Returns whether the form submission is currently being attempted
* @public
*/
export function useIsValid() {
return useFormikContextSelector<
unknown,
FormikContextType<unknown>['isValid']
>(ctx => ctx.isValid);
}

/**
* Returns whether the form is dirty
* @public
*/
export function useIsDirty() {
return useFormikContextSelector<unknown, FormikContextType<unknown>['dirty']>(
ctx => ctx.dirty
);
}

/**
* Returns a function to imperatively validate the entire form
* @public
*/
export function useValidateForm() {
return useFormikContextSelector<
unknown,
FormikContextType<unknown>['validateForm']
>(ctx => ctx.validateForm);
}

/**
* Returns a function to imperatively validate a field
* @public
*/
export function useValidateField(fieldName?: string) {
const validateField = useFormikContextSelector<
unknown,
FormikContextType<unknown>['validateField']
>(ctx => ctx.validateField);
return React.useCallback(() => {
return fieldName ? validateField(fieldName) : validateField;
}, [fieldName]);
}

function useFieldMeta<Values>(name: string) {
const [value] = useFieldValue<Values>(name);
const [touched] = useFieldTouched<Values>(name);
Expand Down
2 changes: 1 addition & 1 deletion packages/formik/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export * from './connect';
export * from './ErrorMessage';
export * from './FormikContext';
export * from './FastField';
export * from './useField';
export * from './hooks';

0 comments on commit 90fd693

Please sign in to comment.