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
Adding an item to ArrayInput on Edit after removing one brings back the same item that was removed #7385
Comments
Reproduced. Thanks for the report |
The codesandbox is actually using the beta and the latest react and MUI versions. I can't reproduce the issue on the latest v3, only on the v4 beta. |
Hey @djhi, I have forked the sandbox: https://codesandbox.io/s/happy-germain-877ypu |
Nope, you can see the version actually used by hovering each line |
That's an awful UX then, but here, try again, I have made sure v3 is used and I can reproduce this: https://codesandbox.io/s/happy-germain-877ypu Make sure you save the post first and go to edit when testing. Because I needed a workaround for this which doesn't involve spending too much time on it, I was able to come up with this: const AddItemButton = ({ onClick, ...rest }: ButtonProps) => {
const { change, getFieldState } = useForm();
return (
<Button
label="ra.action.add"
{...rest}
onClick={(e) => {
const fieldState = getFieldState("formArrayFieldSource");
const nextIndex = fieldState?.value && Array.isArray(fieldState.value) ? fieldState.value.length : 0;
onClick && onClick(e);
change(`formArrayFieldSource[${nextIndex}]`, {
...getEmptyArrayItem()
});
}}>
<AddCircleOutlineRoundedIcon />
</Button>
);
}; where |
Agreed, this part of their UI is weird. I can't reproduce the issue on your codesandbox though |
Not sure why you can't reproduce it, I tried on two different computers and I was able to. I have updated the issue with the video and workaround which did it for me. |
Hi! I have this same problem in v3.19.10 version like @dejancg. @djhi tested this issue but maybe need the condition of some material-ui version. Could @djhi tell me which versión of material did you use? The issue happens in my case with this context: "dependencies": { I traied to change version in the codesanbox but the project crash, i haven´t so much experience with this tool. Also i would to know if in the last versión 4.0.1 is solved. Thanks you so much! |
I tried running the project locally to be sure it's not a problem caused by codesandbox. |
So this happens because we don't provide the values for the new item as explained in react-hook-form documentation. I can only think of two solutions for this, probably complementary:
Open to suggestions. |
What you were expecting:
Removing an item from ArrayInput should really remove it.
What happened instead:
After removing an item and adding a new one, the previosuly removed item is back.
Steps to reproduce:
Start here: codesandbox.io/s/happy-germain-877ypu
I have added a video for clarification:
2022-03-14_12-42-48.mp4
Other information:
There is a workaround which fixed the problem for me:
where
getEmptyArrayItem
is a function which returns an empty array item, such that it doesn't have any undefined members.The idea behind it is to manually make sure the newly added array item is really new.
Environment
The text was updated successfully, but these errors were encountered: