-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
</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]' }]; | ||
|
||
|
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, | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove this part? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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}` | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done