Skip to content

Commit 3ed0d8e

Browse files
authored
fix: fix store errors (#11)
1 parent 0689895 commit 3ed0d8e

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/lib/core/components/Form/hooks/useField.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ export const useField = <Value extends FieldValue, SpecType extends Spec>({
9595
const error = validate?.(_value);
9696
const value = transformArrIn(_value);
9797

98+
let newChildErrors: Record<string, ValidateError> = {...state.childErrors};
99+
100+
if (childErrors) {
101+
const nearestChildName = _.keys(childErrors).sort(
102+
(a, b) => a.length - b.length,
103+
)[0];
104+
105+
if (nearestChildName) {
106+
const existingСhildNames = _.keys(newChildErrors).filter((childName) =>
107+
childName.startsWith(nearestChildName),
108+
);
109+
110+
newChildErrors = {
111+
..._.omit(newChildErrors, existingСhildNames),
112+
...childErrors,
113+
};
114+
}
115+
}
116+
98117
return {
99118
...state,
100119
dirty: !_.isEqual(value, initialValue),
@@ -106,10 +125,7 @@ export const useField = <Value extends FieldValue, SpecType extends Spec>({
106125
valid: !error,
107126
value,
108127
visited: true,
109-
childErrors: {
110-
...state.childErrors,
111-
...(childErrors || {}),
112-
},
128+
childErrors: newChildErrors,
113129
};
114130
});
115131
};
@@ -233,7 +249,7 @@ export const useField = <Value extends FieldValue, SpecType extends Spec>({
233249
firstRenderRef.current = false;
234250

235251
return () => {
236-
tools.onUnmount(name);
252+
(parentOnChange ? parentOnChange : tools.onChange)(name, state.value, {[name]: false});
237253
};
238254
}, []);
239255

src/lib/core/components/Form/hooks/useStore.tsx

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import React from 'react';
33
import _ from 'lodash';
44
import {Field as FinalFormField, useForm} from 'react-final-form';
55

6-
import {REMOVED_ITEM} from '../constants';
76
import {transformArrIn, transformArrOut} from '../helpers';
87
import {
98
AsyncValidateError,
@@ -75,27 +74,8 @@ export const useStore = (name: string) => {
7574
setStore((store) => ({
7675
...store,
7776
values: _.set({...store.values}, name, value),
78-
errors: {...store.errors, ...(errors || {})},
77+
errors: errors || {},
7978
})),
80-
onUnmount: (name: string) =>
81-
setStore((store) => {
82-
const value = _.get(store.values, name);
83-
const values =
84-
value && value !== REMOVED_ITEM
85-
? {..._.set(store.values, name, undefined)}
86-
: store.values;
87-
88-
return {
89-
...store,
90-
values,
91-
errors: {
92-
..._.omit(
93-
store.errors,
94-
Object.keys(store.errors).filter((key) => key.startsWith(name)),
95-
),
96-
},
97-
};
98-
}),
9979
submitFailed,
10080
}),
10181
[store.initialValue, setStore, submitFailed],

src/lib/core/components/Form/types/context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export interface DynamicFormsContext {
1010
tools: {
1111
initialValue: FieldObjectValue;
1212
onChange: (name: string, value: FieldValue, errors?: Record<string, ValidateError>) => void;
13-
onUnmount: (name: string) => void;
1413
submitFailed: boolean;
1514
};
1615
}

0 commit comments

Comments
 (0)