-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
RadioControl: add ref forwarding #41641
Conversation
* }; | ||
* ``` | ||
*/ | ||
export const RadioControl = forwardRef( UnforwardedRadioControl ); |
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.
@ciampo Does that function name seem confusing? UnforwardedRadioControl? Not sure if other components use that but it just seems weird especially reading from start to finish as the function now forwards the ref.
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.
This is a pattern that @mirka and I have adopted in the context of the TypeScript migration that we're undertaking across the components in the package (see this guide for more details)
In order to have Storybook extract and show prop types correctly, we need to have a named export for the component ("RadioControl" in this case). Therefore, we had to find a suitable name for the intermediate representation of the component, and we opted for:
- adding the "unforwarded" prefix when the component is later wrapped in a
forwardRef
call - adding the "unconnected" prefix when the component is later wrapped in a
contextConnect
call
Hope that's a bit more clear!
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.
One question. Other than that everything looks good to me
@@ -93,6 +65,7 @@ export function RadioControl( | |||
aria-describedby={ | |||
!! help ? `${ id }__help` : undefined | |||
} | |||
ref={ forwardedRef } |
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.
Havn't used forwardRef inside an Array.map
before. Anything we need to think about or is this pattern ok?
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.
Good point — I had actually missed that map
statement while making these changes quickly.
It turns out that this task is more complicated than expected — we could forward the ref
to BaseControl
, but BaseControl
currently doesn't accept refs.
I tried to quickly add ref forwarding to BaseControl
, but I ran into type issues — mainly to do with the fact that VisualLabel
is currently defined as a static property on BaseControl
(e.g. BaseControl.VisualLabel
), and adding ref forwarding causes an error with that
Since this is not currently a priority, I've decided to stop investigating this further for now. What do folks think is the best way forward? I'm also happy to drop this entirely for the time being.
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.
I agree we can drop it for now, unless we have a concrete use case. I came across this note in the React docs that forwarding refs is a breaking change, and while I'm not 100% sure about what that entails, it did give me pause.
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.
Makes sense. Will close this PR for now
What?
Adds ref forwarding to the
RadioControl
componentWhy?
Consistency with other components in the package (both in terms of ref forwarding, and in terms of code patterns specifically around how we export a component).
How?
Wrapped the component into
forwardRef
, renamed original component toUnforwardedRadioControl
, moved JSDocsTesting Instructions