Skip to content

Fix Arrayinput multiple reset #10716

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 54 additions & 15 deletions packages/ra-ui-materialui/src/input/ArrayInput/ArrayInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,22 +459,61 @@ describe('<ArrayInput />', () => {
});
});

it('should empty the input on form reset', async () => {
render(<Reset />);
fireEvent.click(await screen.findByRole('button', { name: 'Add' }));
fireEvent.change(screen.getByLabelText('Name'), {
target: { value: 'Leo Tolstoy' },
});
fireEvent.change(screen.getByLabelText('Role'), {
target: { value: 'Writer' },
describe('should empty the input on form reset', () => {
it('should remove a filled line twice', async () => {
render(<Reset />);

expect(screen.queryAllByRole('listitem')).toHaveLength(0);

fireEvent.click(await screen.findByRole('button', { name: 'Add' }));
fireEvent.change(screen.getByLabelText('Name'), {
target: { value: 'Leo Tolstoy' },
});
fireEvent.change(screen.getByLabelText('Role'), {
target: { value: 'Writer' },
});

expect(screen.queryAllByRole('listitem')).toHaveLength(1);
fireEvent.click(screen.getByRole('button', { name: 'Reset' }));
await waitFor(() => {
expect(screen.queryAllByRole('listitem')).toHaveLength(0);
});

fireEvent.click(await screen.findByRole('button', { name: 'Add' }));
fireEvent.change(screen.getByLabelText('Name'), {
target: { value: 'Leo Tolstoy' },
});
fireEvent.change(screen.getByLabelText('Role'), {
target: { value: 'Writer' },
});

expect(screen.queryAllByRole('listitem')).toHaveLength(1);
fireEvent.click(screen.getByRole('button', { name: 'Reset' }));
await waitFor(() => {
expect(screen.queryAllByRole('listitem')).toHaveLength(0); // TODO: fix it
});
});
fireEvent.click(screen.getByRole('button', { name: 'Reset' }));
await waitFor(() => {
expect(screen.queryByDisplayValue('Leo Tolstoy')).toBeNull();
expect(screen.queryByDisplayValue('Writer')).toBeNull();
expect(
screen.queryByRole('button', { name: 'Clear the list' })
).toBeNull();

it('should remove an empty line twice', async () => {
render(<Reset />);

expect(screen.queryAllByRole('listitem')).toHaveLength(0);

fireEvent.click(await screen.findByRole('button', { name: 'Add' }));

expect(screen.queryAllByRole('listitem')).toHaveLength(1);
fireEvent.click(screen.getByRole('button', { name: 'Reset' }));
await waitFor(() => {
expect(screen.queryAllByRole('listitem')).toHaveLength(0);
});

fireEvent.click(await screen.findByRole('button', { name: 'Add' }));

expect(screen.queryAllByRole('listitem')).toHaveLength(1);
fireEvent.click(screen.getByRole('button', { name: 'Reset' }));
await waitFor(() => {
expect(screen.queryAllByRole('listitem')).toHaveLength(0); // TODO: fix it
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Resource,
testI18nProvider,
TestMemoryRouter,
useCreatePath,
useSourceContext,
} from 'ra-core';
import { Button, InputAdornment, Stack } from '@mui/material';
Expand Down Expand Up @@ -970,10 +969,8 @@ const BookCreateReset = () => {
};

export const Reset = () => {
const createPath = useCreatePath();
const initialEntrie = createPath({ resource: 'books', type: 'create' });
return (
<TestMemoryRouter initialEntries={[initialEntrie]}>
<TestMemoryRouter initialEntries={['/books/create']}>
<Admin dataProvider={dataProvider}>
<Resource name="books" create={BookCreateReset} />
</Admin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { SimpleForm } from '../../form';
import { ArrayInput } from './ArrayInput';
import { TextInput } from '../TextInput';
import { SimpleFormIterator } from './SimpleFormIterator';
import { Basic, WithFormDataConsumer } from './SimpleFormIterator.stories';
import {
Basic,
DefaultValue,
WithFormDataConsumer,
} from './SimpleFormIterator.stories';

describe('<SimpleFormIterator />', () => {
// bypass confirm leave form with unsaved changes
Expand Down Expand Up @@ -418,22 +422,7 @@ describe('<SimpleFormIterator />', () => {
});

it('should not reapply default values set at form level after removing and then re-adding one row', async () => {
render(
<Wrapper>
<SimpleForm
defaultValues={{
emails: [{ email: '[email protected]', name: 'test' }],
}}
>
<ArrayInput source="emails">
<SimpleFormIterator>
<TextInput source="email" label="Email" />
<TextInput source="name" label="Name" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Wrapper>
);
render(<DefaultValue />);

const removeFirstButton = getByLabelText(
// @ts-ignore
Expand All @@ -456,16 +445,24 @@ describe('<SimpleFormIterator />', () => {
expect(inputElements.length).toBe(1);
});

expect(
screen
.queryAllByLabelText('Email')
.map(inputElement => (inputElement as HTMLInputElement).value)
).toEqual(['']);
expect(
screen
.queryAllByLabelText('Name')
.map(inputElement => (inputElement as HTMLInputElement).value)
).toEqual(['']);
await waitFor(() => {
expect(
screen
.queryAllByLabelText('Email')
.map(
inputElement => (inputElement as HTMLInputElement).value
)
).toEqual(['']);
});
await waitFor(() => {
expect(
screen
.queryAllByLabelText('Name')
.map(
inputElement => (inputElement as HTMLInputElement).value
)
).toEqual(['']);
});

expect(screen.queryAllByLabelText('ra.action.remove').length).toBe(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@ export const UseSimpleFormIteratorItem = () => (
</Wrapper>
);

export const DefaultValue = () => (
<AdminContext dataProvider={testDataProvider()} defaultTheme="light">
<ResourceContextProvider value="posts">
<SimpleForm
defaultValues={{
emails: [{ email: '[email protected]', name: 'test' }],
}}
>
<ArrayInput source="emails">
<SimpleFormIterator>
<TextInput source="email" label="Email" />
<TextInput source="name" label="Name" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);

export const WithFormDataConsumer = () => (
<AdminContext dataProvider={testDataProvider()}>
<ResourceContextProvider value="posts">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const SimpleFormIterator = (inProps: SimpleFormIteratorProps) => {

const [confirmIsOpen, setConfirmIsOpen] = useState<boolean>(false);
const { append, fields, move, remove, replace } = useArrayInput(props);
const { resetField, trigger, getValues } = useFormContext();
const { trigger, getValues } = useFormContext();
const translate = useTranslate();
const record = useRecordContext(props);
const initialDefaultValue = useRef({});
Expand Down Expand Up @@ -134,10 +134,8 @@ export const SimpleFormIterator = (inProps: SimpleFormIteratorProps) => {
}
}
append(defaultValue);
// Make sure the newly added inputs are not considered dirty by react-hook-form
resetField(`${finalSource}.${fields.length}`, { defaultValue });
},
[append, children, resetField, finalSource, fields.length]
[append, children]
);

const handleReorder = useCallback(
Expand Down
Loading