Fix radios component with defaultChecked inputs and conditional content#377
Conversation
5f73033 to
12a9c50
Compare
165b241 to
fe91590
Compare
|
I've tested this locally in my service (https://github.com/NHSDigital/record-a-vaccination-service) and it's working perfectly. |
|
57 changed files and 17 commits in this PR. I assume some of these commits are not indended for this PR? |
12a9c50 to
ab2b166
Compare
fe91590 to
381cb0c
Compare
Well spotted thanks, I've changed the PR base to #375 |
a0eac0d to
2e8a116
Compare
93717ca to
d36cdb0
Compare
2e8a116 to
c703727
Compare
0159b0c to
71d1959
Compare
defaultChecked inputsdefaultChecked inputs and conditional content
c3310bf to
63a2d6f
Compare
Form components with missing `id` (or `idPrefix`) now fall back to the `name` prop before generating IDs. This helps prevent hydration mismatch errors with server-side rendering (SSR) HTML https://react.dev/link/hydration-mismatch
63a2d6f to
2ab21ed
Compare
|
@colinrotherham thank you! This unblocks an issue found by @andyrae6 in https://github.com/NHSDigital/record-a-vaccination-service/pull/55 |
| // Use `name` prop when `id` is missing, otherwise server-side rendered (SSR) | ||
| // components will use different generated IDs after client-side hydration | ||
| // https://react.dev/link/hydration-mismatch | ||
| const elementID = name && !id ? `${name}-${inputType}` : (id ?? generatedID); |
There was a problem hiding this comment.
When tesing with Next.js another re-rendering issue came up. From the changelog:
Note: Form components with missing
id(oridPrefix) now fall back to thenameprop before generating IDs. This helps prevent hydration mismatch errors with server-side rendering (SSR) HTML.
2ab21ed to
b5a4c31
Compare
|
|
Thanks @mikemonteith and @frankieroberto The Sonar duplications are largely from the Storybook examples so we should ignore them in future |


This PR fixes #374
In v6.0.0 we accidentally left internal
useState()in the following components (from v5.x)selectedRadioandsetSelectedRadio()selectedRadioandsetSelected()These should have been removed for the v6 release where:
reffor DOM element accessdefaultValue,valueandonChangehandlersThis makes sure radio buttons work with either
checked(controlled) anddefaultChecked(uncontrolled)What was the fix?
name(not justid) before generating IDs prevents unnnecessary re-rendersWhat was the bug?
Radio inputs previously decided whether to show conditional content using:
Notice that
defaultCheckedis not included? Only controlled inputs workedTo workaround this, the previous version tried to sync up the internal context/state with:
But that caused problems with modern React and NHS.UK frontend conditional content synchronisation
defaultCheckedshowed the conditional contentuseEffect()hide the conditional content (unless already checked)