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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <ArrayInput> should keep error state on children after unmount #9677

Merged
merged 1 commit into from
Mar 1, 2024
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
54 changes: 54 additions & 0 deletions packages/ra-ui-materialui/src/input/ArrayInput/ArrayInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,60 @@ describe('<ArrayInput />', () => {
});
});

it('should not clear errors of children when unmounted', async () => {
let setArrayInputVisible;

const MyArrayInput = () => {
const [visible, setVisible] = React.useState(true);

setArrayInputVisible = setVisible;

return visible ? (
<ArrayInput resource="bar" source="arr">
<SimpleFormIterator>
<TextInput source="id" />
<TextInput source="foo" />
</SimpleFormIterator>
</ArrayInput>
) : null;
};

render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm
onSubmit={jest.fn}
defaultValues={{
arr: [
{ id: 1, foo: 'bar' },
{ id: 2, foo: 'baz' },
],
}}
validate={() => ({ arr: [{ foo: 'Must be "baz"' }, {}] })}
>
<MyArrayInput />
</SimpleForm>
</AdminContext>
);

// change one input to enable the SaveButton (which is disabled when the form is pristine)
fireEvent.change(
screen.getAllByLabelText('resources.bar.fields.arr.id')[0],
{
target: { value: '42' },
}
);
fireEvent.click(await screen.findByLabelText('ra.action.save'));

await screen.findByText('Must be "baz"');

setArrayInputVisible(false);
expect(screen.queryByText('Must be "baz"')).toBeNull();

// ensure errors are still there after re-mount
setArrayInputVisible(true);
await screen.findByText('Must be "baz"');
});

it('should allow to have a helperText', () => {
render(
<AdminContext dataProvider={testDataProvider()}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ export const ArrayInput = (props: ArrayInputProps) => {
formGroups.registerField(source, formGroupName);

return () => {
unregister(source, { keepValue: true });
unregister(source, {
keepValue: true,
keepError: true,
keepDirty: true,
keepTouched: true,
});
formGroups.unregisterField(source, formGroupName);
};
}, [register, unregister, source, formGroups, formGroupName]);
Expand Down