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

Conditional Fields cause form to completely fall over (have reproducible solution for you) #868

Closed
NickCullen opened this issue Jan 20, 2020 · 33 comments
Labels
bug Something isn't working

Comments

@NickCullen
Copy link

Describe the bug
When using conditional fields with controlled custom components, it causes all other fields to unregister and re-register themselves thus resetting the form.

To Reproduce
Steps to reproduce the behavior:

  1. Set checkbox to 'no'
  2. Select item 1 from select box
  3. Select item 2 from select box
  4. Conditional field will now appear as item 2 is selected
  5. Type anything into the text box
  6. Remove item 2 from select box
  7. Add item 2 back into the select box
  8. Type into the text box and watch the form reset

Codesandbox link
Not got a codesandbox account but you can find the code here:
https://github.com/NickCullen/react_form_conditional_field_error

Expected behavior
The form not to reset - but only the value for the conditional field when removing item 2 from the list to reset.

Desktop (please complete the following information):

  • Windows
  • Chrome
  • Version 79

Additional context
npm install
npm start
http://localhost:3000/

@bluebill1049
Copy link
Member

can you please create a codesandbox?

@bluebill1049 bluebill1049 added the status: need more detail Please follow our issue template. label Jan 20, 2020
@NickCullen
Copy link
Author

@bluebill1049
Copy link
Member

bluebill1049 commented Jan 20, 2020

can you try out Controller see if this solves the issue? for your InputTextBox component.

https://react-hook-form.com/api#Controller

@NickCullen
Copy link
Author

NickCullen commented Jan 20, 2020 via email

@bluebill1049
Copy link
Member

what's the trouble @NickCullen ?

@bluebill1049 bluebill1049 added bug Something isn't working and removed status: need more detail Please follow our issue template. labels Jan 20, 2020
@bluebill1049
Copy link
Member

find the root cause: https://codesandbox.io/s/competent-yonath-fkxuc
patching it.

bluebill1049 added a commit that referenced this issue Jan 20, 2020
@bluebill1049 bluebill1049 self-assigned this Jan 20, 2020
@NickCullen
Copy link
Author

NickCullen commented Jan 20, 2020 via email

@bluebill1049
Copy link
Member

thanks @NickCullen for the detailed reply and codesandbox. It's good you prefer the custom register, and i agree it will give you better control. i have found the bug and patching it. the above codesandbox should be working now. let me know if you still encounter issue.

@NickCullen
Copy link
Author

NickCullen commented Jan 20, 2020 via email

@bluebill1049
Copy link
Member

Amazing!!! Thank you for being very prompt also. Out of interest, what was the issue? Kind Regards, Nicholas Cullen

On 20 Jan 2020, at 08:27, Bill @.***> wrote: thanks @NickCullen for the detailed reply and codesandbox. It's good you prefer the custom register, and i agree it will give you better control. i have found the bug and patching it. the above codesandbox should be working now. let me know if you still encounter issue. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

it was due to unnecessary useCallback dependency, small thing trigger big effect 😢

@NickCullen
Copy link
Author

@bluebill1049 Apologies, I don't think this is actually completely fixed :(

If you follow the steps then type into the box a second time you can notice the radio option reset back to "yes".

Also, at this point - if you change the value of the radio option back to no, the text in the conditional field will reset and you can no longer alter the value of the radio option.

This happens in the sandbox you sent, also: https://codesandbox.io/s/competent-yonath-fkxuc

@bluebill1049 bluebill1049 reopened this Jan 20, 2020
@bluebill1049
Copy link
Member

hmmm it's working for me. can you send a video of the bug?

@NickCullen
Copy link
Author

@bluebill1049 Also I noticed you removed the cleanup to unregister the multi selector from the form on the useEffect but left the text box and radio option cleanup there - was there any reason for this?

@bluebill1049
Copy link
Member

oh that's probably why it's working lol

@bluebill1049
Copy link
Member

i didn't remove it. can you refresh and double check?

@bluebill1049
Copy link
Member

useEffect(() => {
    form.register(props.name);
    console.log("yes/no box register");
    return () => {
        console.log('remove here--')
        form.unregister(props.name)
    };
  }, [form.register]);

still there.

@NickCullen
Copy link
Author

https://codesandbox.io/s/dazzling-violet-12tlg

Updated reproducable steps

@NickCullen
Copy link
Author

For some reason its gone back to how it was originally with https://codesandbox.io/s/dazzling-violet-12tlg :/

@bluebill1049
Copy link
Member

try this codesandbox: https://codesandbox.io/s/competent-yonath-fkxuc ?

@NickCullen
Copy link
Author

Just did, and its gone back to how it was originally - any more PR's gone through?

@bluebill1049
Copy link
Member

strange it works for me...

let me write it here and test again:

Set checkbox to 'no'
Select item 1 from select box
Select item 2 from select box
Conditional field will now appear as item 2 is selected
Type anything into the text box
Remove item 2 from select box
Add item 2 back into the select box
Type into the text box and watch the checkbox reset back to 'yes'
Change checkbox to 'no'
text in conditional field will dissapear and radio option becomes un-usable

@bluebill1049
Copy link
Member

Type into the text box and watch the checkbox reset back to 'yes' didn't happen for me

@NickCullen
Copy link
Author

They are the correct steps yeah - although I am seeing the original issue now (and it is using the correct version of form hook)

image

@bluebill1049
Copy link
Member

bluebill1049 commented Jan 20, 2020

oh don't use lib, use the ./src folder! hahah it's not published yet.

@NickCullen
Copy link
Author

Trying to get a video of it, I am using the one you sent me and I'm seeing the behavior from the start of the issue.

@bluebill1049
Copy link
Member

kk thanks. very strange...

@bluebill1049
Copy link
Member

either i miss something or wrong code,

@NickCullen
Copy link
Author

Here we go: https://codebase8-my.sharepoint.com/:v:/p/nick_cullen/Ed03P99MhP1PvYn_wftY6CUBSGfgpxv4Ic8DFUlu37DmDw?e=Cq5jak

@bluebill1049
Copy link
Member

try again with this demo: https://codesandbox.io/s/competent-yonath-fkxuc

maybe i didn't save the file.

@NickCullen
Copy link
Author

Thanks @bluebill1049 that's working as expecting, and not loosing state with radio options etc.

In my clients project I have referenced "react-hook-form": "^4.5.7-beta.3" and it is all working as expected. Although you said earlier I wouldn't be able to access these changes via that?

I'm quite confused how it's working in my clients project now :)

@bluebill1049
Copy link
Member

lol, clash of betas. i did release a beta which probably include the fix (patch was for next.js)

@NickCullen
Copy link
Author

So to confirm this is in that beta branch? :)

Thanks very much for the help!

@bluebill1049
Copy link
Member

yes. i will release the patch soon with other fixes.

@bluebill1049 bluebill1049 removed their assignment Aug 12, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants