-
Notifications
You must be signed in to change notification settings - Fork 748
@cds/react: controlled form inputs #5625
Comments
It looks like this is fixed, do you know when your next release will be? |
@ashleyryan likely sometime this week |
Hi @coryrylan I just updated to the newest version of Clarity Core, and the inputs still have different behavior than vanilla inputs. If I set a CdsCheckbox's value programmatically, the onChange event will fire, even though the user didn't interact with it. A regular input only fires Normally this isn't a huge deal, because if you call Updated stackblitz: https://stackblitz.com/edit/react-cds-checkbox-onchange-bgcacb-vowfux?file=index.tsx If you look in the console, you'll see when the events fire |
Ok, we will need to revisit this. The primary issue we are running into is React overrides the setters on the native input when using controlled inputs. This breaks the native setter call which was causing the original issue. When the user interacts with the input React ignores the native DOM event and skips a render cycle since it uses its own setter and internal event. Here is a demo of the issue https://stackblitz.com/edit/react-ts-nwvy1e https://hustle.bizongo.in/simulate-react-on-change-on-controlled-components-baa336920e04 |
Another example, this time for the radio group. While you can avoid unnecessary re-renders in checkboxes by making sure to use the Screen.Recording.2021-03-05.at.10.18.28.AM.mov |
I accidentally ended up in a twitter thread about React Components + Web Components and one of the maintainers from LitElement asked me about the issues we were facing, so I described the issue and he agreed that controlled components are tricky and suggested a "controlled" component: https://twitter.com/justinfagnani/status/1369071959175090176 I was playing around in Core and tried something like this in the update event of
I suspect some of the event listeners created in Of course, then there's the problem that it's way too easy to leave |
@ashleyryan thanks for the update and link to the twitter thread! I'm working on this throughout this week so if I make progress I'll be sure to post back here. |
@ashleyryan I have a potential fix, I am not yet 100% sure this is covering all the use cases. As far as I can test it seems to get the components in sync. #5718 |
It fixes the stackblitz issue anyway, I copied the contents into App.tsx in the clarity react folder and it works as expected. Also tested out making sure indeterminate still sets like it should. |
Merged #5718 but going to keep this open until its published so we can follow up |
Closing for now, will reopen if needed |
Hi there 👋, this is an automated message. To help Clarity keep track of discussions, we automatically lock closed issues after 14 days. Please look for another open issue or open a new issue with updated details and reference this one as necessary. |
Describe the bug
Right now in @cds/react certain form inputs do not work well with controlled input types in React. In this example the second checkbox event is ignored and not updated.
https://stackblitz.com/edit/react-cds-checkbox-onchange-bgcacb?file=index.tsx
This seems specific to how React handles Controlled vs Uncontrolled input types as the plain html/js works without issue
https://stackblitz.com/edit/typescript-2ysgfw?file=index.html
https://reactjs.org/docs/forms.html#controlled-components
How to reproduce
Please provide a link to a reproduction scenario using one of the Clarity Stackblitz templates. Reports without a clear reproduction may not be prioritized until one is provided.
Steps to reproduce the behavior:
Expected behavior
Controlled inputs should work with the custom element.
Versions
Clarity project:
Clarity version:
Framework:
Framework version:
ie: Angular 11
Device:
Additional notes
Add any other notes about the problem here.
The text was updated successfully, but these errors were encountered: