From 5d031d5eb89b8bcc68a61adf73473cd1a741937d Mon Sep 17 00:00:00 2001 From: Pedro Bini Date: Wed, 1 Jul 2020 12:56:42 -0300 Subject: [PATCH 1/2] Add mapInitialTouched util --- packages/core/src/utils/mapInitialTouched.ts | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 packages/core/src/utils/mapInitialTouched.ts diff --git a/packages/core/src/utils/mapInitialTouched.ts b/packages/core/src/utils/mapInitialTouched.ts new file mode 100644 index 0000000..669aa62 --- /dev/null +++ b/packages/core/src/utils/mapInitialTouched.ts @@ -0,0 +1,52 @@ +/** + * Recursively maps touched fields in an object. + * @param obj The object + */ +const recursiveMapTouched = (obj: object): object => { + const result = { + ...(obj || {}), + }; + + Object + .keys(result) + .forEach((key) => { + const value = result[key]; + + if (typeof value === 'object') { + recursiveMapTouched(value); + return; + } + + // eslint-disable-next-line no-param-reassign + result[key] = true; + }); + + return result; +}; + +/** + * Recursively maps initial touched fields according to custom initial values. + * Any value initialized with initialValues will be marked as touched. + * + * You can override the touched values by passing an object as the second parameter, + * which is optional. + * + * @param initialValues Value of initialValues + * @param initialTouched Value of initialTouched + */ +const mapInitialTouched = ( + initialValues?: object, + initialTouched?: object, +) => { + if (initialTouched) { + return initialTouched; + } + + if (!initialValues) { + return undefined; + } + + return recursiveMapTouched(initialValues); +}; + +export default mapInitialTouched; From 0eba666f08a4f73201bbece3f657772685e212c5 Mon Sep 17 00:00:00 2001 From: Pedro Bini Date: Wed, 1 Jul 2020 12:56:57 -0300 Subject: [PATCH 2/2] Automatically map initialTouched object --- packages/core/src/hooks/useFormup.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/core/src/hooks/useFormup.tsx b/packages/core/src/hooks/useFormup.tsx index 166bc4c..e1a3cec 100644 --- a/packages/core/src/hooks/useFormup.tsx +++ b/packages/core/src/hooks/useFormup.tsx @@ -13,6 +13,7 @@ import { import FormInputGroupItem from '../components/FormInputGroupItem/FormInputGroupItem'; import mapInitialValuesFromSchema from '../utils/mapInitialValuesFromSchema'; import FormInputGroup from '../components/FormInputGroup/FormInputGroup'; +import mapInitialTouched from '../utils/mapInitialTouched'; import FormInput from '../components/FormInput/FormInput'; import validateForm from '../yup/validateForm'; import useHasMounted from './useHasMounted'; @@ -40,11 +41,18 @@ const useFormup = ( schema, ]); + const initialTouched = React.useMemo(() => ( + mapInitialTouched(options?.initialValues, options?.initialTouched) + ), [ + options, + ]); + const useFormikResult = useFormik({ ...options, validateOnMount: !hasMounted && options.validateOnMount, onSubmit: options?.onSubmit || noop, validationSchema: schema, + initialTouched, initialValues, });