Skip to content

Commit 0812636

Browse files
committed
Traverse key list backwards to fix array removal bug
1 parent 3981e19 commit 0812636

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

Todo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- Split package in `typed-react-form-yup-validator`, `typed-react-form-elements`? Reduces size.
44
- Nested validators in child forms to improve validation performance
55
- Typed FormInput type prop
6+
- Combine array helpers into one object? This is usefull to pass to other components

example/src/App.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ const initialValues: ExampleFormData = {
8282
};
8383

8484
const TodoListSchema = yup.object({
85-
name: yup.string().required("Enter a name").min(5, "Enter a longer name")
85+
name: yup.string().required("Enter a name").min(5, "Enter a longer name"),
86+
todos: yup.array().of(
87+
yup.object({
88+
message: yup.string().required("Enter a todo")
89+
})
90+
)
8691
});
8792

8893
export function Form() {
@@ -169,6 +174,7 @@ export function Form() {
169174
{ form, swap, remove, append, values, setValues } // <- Make sure to use the newly passed form (otherwise type checking will not work!)
170175
) => (
171176
<VisualRender>
177+
<pre>{form.values.length}</pre>
172178
<ul>
173179
{form.values.map((
174180
_,

src/form.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ export class FormState<T, State = DefaultState, Error = DefaultError> {
249249
let newKeys = Object.keys(isDefault ? this.defaultValues : this.values);
250250
let localKeys = Object.keys(values);
251251
let mostKeys = newKeys.length > localKeys.length ? newKeys : localKeys;
252-
for (let i = 0; i < mostKeys.length; i++) {
252+
// Traverse backwards, so when removing array items, the whole array gets shifted in the right direction
253+
for (let i = mostKeys.length - 1; i >= 0; i--) {
253254
let key = mostKeys[i] as keyof T;
254255
this.setValue(
255256
key,

src/hooks.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ export function useChildForm<T, State, Error, Key extends keyof T>(parentForm: F
5555
c.current!.name = name;
5656

5757
// First, set new default values, without validating
58-
c.current!.setValues(parentForm.defaultValues[name] ?? ({} as any), false, true, true, true);
58+
c.current!.setValues(parentForm.defaultValues[name] ?? ({} as any), false, true, true, false);
5959
// Then, set new values and validate if needed
6060
c.current!.setValues(parentForm.values[name] ?? ({} as any), c.current!.validateOnMount, false, true, true);
6161

6262
return () => {
63-
// Only clear value/error if the set form in the childMap is not already overriden
63+
// Only clear if is not already overwritten
6464
if (parentForm.childMap[name] === c.current!) {
65-
parentForm.setError(name, undefined);
66-
parentForm.setValueInternal(name, undefined, false, false);
65+
delete parentForm.childMap[name];
6766
}
6867
};
6968
}, [parentForm, name]);

0 commit comments

Comments
 (0)