Skip to content

Fix <ArrayInput> adds previously removed item #8029

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

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,65 @@ describe('<SimpleFormIterator />', () => {
expect(screen.queryAllByText('ra.action.remove').length).toBe(1);
});

it('should add correct children with default value after removing one', async () => {
render(
<Wrapper>
<SimpleForm>
<ArrayInput
source="emails"
defaultValue={[
{ email: '[email protected]', name: 'test' },
]}
>
<SimpleFormIterator>
<TextInput
source="email"
label="Email"
defaultValue="[email protected]"
/>
<TextInput source="name" label="Name" />
</SimpleFormIterator>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you also include an input with no default value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

</ArrayInput>
</SimpleForm>
</Wrapper>
);

const removeFirstButton = getByText(
(screen.queryAllByLabelText('Email')[0] as HTMLElement).closest(
'li'
),
'ra.action.remove'
).closest('button');

fireEvent.click(removeFirstButton);
await waitFor(() => {
expect(screen.queryAllByLabelText('Email').length).toEqual(0);
});

const addItemElement = screen
.getByText('ra.action.add')
.closest('button');

fireEvent.click(addItemElement);
await waitFor(() => {
const inputElements = screen.queryAllByLabelText('Email');
expect(inputElements.length).toBe(1);
});

expect(
screen
.queryAllByLabelText('Email')
.map(inputElement => inputElement.value)
).toEqual(['[email protected]']);
expect(
screen
.queryAllByLabelText('Name')
.map(inputElement => inputElement.value)
).toEqual(['']);

expect(screen.queryAllByText('ra.action.remove').length).toBe(1);
});

it('should remove children row on remove button click', async () => {
const emails = [{ email: '[email protected]' }, { email: '[email protected]' }];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import {
Children,
cloneElement,
MouseEvent,
MouseEventHandler,
ReactElement,
ReactNode,
useCallback,
Expand Down Expand Up @@ -56,21 +55,21 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {

const addField = useCallback(
(item: any = undefined) => {
append(item);
let defaultValue = item;
if (item == null) {
defaultValue = {} as Record<string, unknown>;
Children.forEach(children, input => {
if (React.isValidElement(input)) {
defaultValue[input.props.source] =
input.props.defaultValue ?? '';
}
});
}
append(defaultValue);
},
[append]
[append, children]
);

// add field and call the onClick event of the button passed as addButton prop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this part?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was unnecessary and actually induced an issue by adding an item twice

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll break in the case where the developer passed a custom add button. If there is a double add, you should probably remove the one in AddItemButton instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restored

const handleAddButtonClick = (
originalOnClickHandler: MouseEventHandler
) => (event: MouseEvent) => {
addField();
if (originalOnClickHandler) {
originalOnClickHandler(event);
}
};

const handleReorder = useCallback(
(origin: number, destination: number) => {
move(origin, destination);
Expand Down Expand Up @@ -118,9 +117,6 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
<li className={SimpleFormIteratorClasses.line}>
<span className={SimpleFormIteratorClasses.action}>
{cloneElement(addButton, {
onClick: handleAddButtonClick(
addButton.props.onClick
),
className: clsx(
'button-add',
`button-add-${source}`
Expand Down