From 2e041162e5619bdb0ac0c411dcbac812a474ffe8 Mon Sep 17 00:00:00 2001 From: Nikita Zolotykh Date: Fri, 17 Mar 2023 20:46:57 +0300 Subject: [PATCH] fix: fix store hidden errors --- .../core/components/Form/hooks/useField.tsx | 26 +++++++++++++++---- .../core/components/Form/hooks/useStore.tsx | 22 +--------------- src/lib/core/components/Form/types/context.ts | 1 - 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/lib/core/components/Form/hooks/useField.tsx b/src/lib/core/components/Form/hooks/useField.tsx index 54fc426c..fc6a88f4 100644 --- a/src/lib/core/components/Form/hooks/useField.tsx +++ b/src/lib/core/components/Form/hooks/useField.tsx @@ -95,6 +95,25 @@ export const useField = ({ const error = validate?.(_value); const value = transformArrIn(_value); + let newChildErrors: Record = {...state.childErrors}; + + if (childErrors) { + const nearestChildName = _.keys(childErrors).sort( + (a, b) => a.length - b.length, + )[0]; + + if (nearestChildName) { + const existingСhildNames = _.keys(newChildErrors).filter((childName) => + childName.startsWith(nearestChildName), + ); + + newChildErrors = { + ..._.omit(newChildErrors, existingСhildNames), + ...childErrors, + }; + } + } + return { ...state, dirty: !_.isEqual(value, initialValue), @@ -106,10 +125,7 @@ export const useField = ({ valid: !error, value, visited: true, - childErrors: { - ...state.childErrors, - ...(childErrors || {}), - }, + childErrors: newChildErrors, }; }); }; @@ -233,7 +249,7 @@ export const useField = ({ firstRenderRef.current = false; return () => { - tools.onUnmount(name); + (parentOnChange ? parentOnChange : tools.onChange)(name, state.value, {[name]: false}); }; }, []); diff --git a/src/lib/core/components/Form/hooks/useStore.tsx b/src/lib/core/components/Form/hooks/useStore.tsx index b2bb37bc..bea8cfc6 100644 --- a/src/lib/core/components/Form/hooks/useStore.tsx +++ b/src/lib/core/components/Form/hooks/useStore.tsx @@ -3,7 +3,6 @@ import React from 'react'; import _ from 'lodash'; import {Field as FinalFormField, useForm} from 'react-final-form'; -import {REMOVED_ITEM} from '../constants'; import {transformArrIn, transformArrOut} from '../helpers'; import { AsyncValidateError, @@ -75,27 +74,8 @@ export const useStore = (name: string) => { setStore((store) => ({ ...store, values: _.set({...store.values}, name, value), - errors: {...store.errors, ...(errors || {})}, + errors: errors || {}, })), - onUnmount: (name: string) => - setStore((store) => { - const value = _.get(store.values, name); - const values = - value && value !== REMOVED_ITEM - ? {..._.set(store.values, name, undefined)} - : store.values; - - return { - ...store, - values, - errors: { - ..._.omit( - store.errors, - Object.keys(store.errors).filter((key) => key.startsWith(name)), - ), - }, - }; - }), submitFailed, }), [store.initialValue, setStore, submitFailed], diff --git a/src/lib/core/components/Form/types/context.ts b/src/lib/core/components/Form/types/context.ts index 956ddea3..b298cee4 100644 --- a/src/lib/core/components/Form/types/context.ts +++ b/src/lib/core/components/Form/types/context.ts @@ -10,7 +10,6 @@ export interface DynamicFormsContext { tools: { initialValue: FieldObjectValue; onChange: (name: string, value: FieldValue, errors?: Record) => void; - onUnmount: (name: string) => void; submitFailed: boolean; }; }