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

RadioControl: add ref forwarding #41641

Closed
wants to merge 2 commits into from
Closed

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented Jun 9, 2022

What?

Adds ref forwarding to the RadioControl component

Why?

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 to UnforwardedRadioControl, moved JSDocs

Testing Instructions

  • Component should behave like before in Storybook and the editor
  • Tests should pass

@ciampo ciampo added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components [Feature] Component System WordPress component system labels Jun 9, 2022
@ciampo ciampo requested a review from ajitbohra as a code owner June 9, 2022 17:52
@ciampo ciampo self-assigned this Jun 9, 2022
* };
* ```
*/
export const RadioControl = forwardRef( UnforwardedRadioControl );
Copy link
Contributor

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.

Copy link
Contributor Author

@ciampo ciampo Jun 10, 2022

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!

Copy link
Member

@walbo walbo left a 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 }
Copy link
Member

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?

Copy link
Contributor Author

@ciampo ciampo Jun 10, 2022

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

Screenshot 2022-06-10 at 09 19 08

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.

Copy link
Member

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.

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Component System WordPress component system [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants