Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悶 fix #3286 remove with deep nested field array #3287

Merged
merged 2 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/useFieldArray.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
FieldError,
DeepMap,
SubmitHandler,
UseFormMethods,
FieldValues,
} from './types';
import { VALIDATION_MODE } from './constants';
import { FormProvider } from './useFormContext';
Expand Down Expand Up @@ -3827,6 +3829,127 @@ describe('useFieldArray', () => {
});

describe('array of array fields', () => {
it('should remove correctly with nested field array and set shouldUnregister to false', () => {
const Component = () => {
const { register, control } = useForm({
shouldUnregister: false,
});
const { fields, append } = useFieldArray({
name: 'fieldArray',
control,
});

return (
<form>
{fields.map((field, index) => (
<ArrayField
key={field.id}
arrayIndex={index}
arrayField={field}
register={register}
control={control}
/>
))}

<button
type="button"
onClick={() => {
append({
value: `fieldArray[${fields.length}].value`,
});
}}
>
Add array
</button>
</form>
);
};

const ArrayField = ({
arrayIndex,
arrayField,
register,
control,
}: {
arrayIndex: number;
register: UseFormMethods['register'];
arrayField: Partial<FieldValues>;
control: Control;
}) => {
const { fields, append, remove } = useFieldArray({
name: `fieldArray[${arrayIndex}].nestedFieldArray`,
control,
});

return (
<div>
<input
ref={register}
name={`fieldArray[${arrayIndex}].value`}
defaultValue={arrayField.value}
/>
<br />
{fields.map((nestedField, index) => (
<div key={nestedField.id}>
<input
ref={register()}
name={`fieldArray[${arrayIndex}].nestedFieldArray[${index}].value`}
defaultValue={nestedField.value}
/>
<button type="button" onClick={() => remove(index)}>
remove
</button>
</div>
))}
<button
type="button"
onClick={() => {
append({
value: `fieldArray[${arrayIndex}].nestedFieldArray[${fields.length}].value`,
});
}}
>
Add nested array
</button>
</div>
);
};

render(<Component />);

fireEvent.click(
screen.getByRole('button', {
name: 'Add array',
}),
);

fireEvent.click(
screen.getByRole('button', {
name: 'Add nested array',
}),
);

fireEvent.click(
screen.getByRole('button', {
name: 'Add nested array',
}),
);

fireEvent.click(
screen.getAllByRole('button', {
name: 'remove',
})[0],
);

fireEvent.click(
screen.getAllByRole('button', {
name: 'remove',
})[0],
);

expect(screen.getAllByRole('textbox').length).toEqual(1);
});

it('should prepend correctly with default values on nested array fields', () => {
const ChildComponent = ({
index,
Expand Down
8 changes: 4 additions & 4 deletions src/useFieldArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ export const useFieldArray = <
>(fields);

const getCurrentFieldsValues = () =>
get(getValues(), name, allFields.current).map(
(item: Partial<TFieldArrayValues>, index: number) => ({
get(getValues(), name, allFields.current)
.slice(0, allFields.current.length)
.map((item: Partial<TFieldArrayValues>, index: number) => ({
...allFields.current[index],
...item,
}),
);
}));

allFields.current = fields;
fieldArrayNamesRef.current.add(name);
Expand Down