question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose RadioGroupContext via useRadioGroup Hook

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

Expose RadioGroupContext via useRadioGroup Hook (cf. useFormControl).

Examples 🌈

https://codesandbox.io/s/material-demo-9w4yc

Motivation 🔦

I’d like the ability to easily work out whether a wrapped Radio is checked, so I can apply custom styling to a checked label, for example (see demo above). In MUI v3, this was possible because RadioGroup would use cloneElement and pass checked prop to children. With the new context implementation (https://github.com/mui-org/material-ui/pull/15069), I now need to hook into the context but because it’s not part of public API, I’d need to use a Babel transform to import the correct module (CJS vs ESM) in my library output:

  • @material-ui/core/RadioGroup/RadioGroupContext
  • @material-ui/core/esm/RadioGroup/RadioGroupContext

@oliviertassinari has pointed out that we’re currently exposing FormControlContext via a custom useFormControl Hook, so we could probably do the same in this instance? I’m happy to raise a PR for this.

Prior art: https://github.com/mui-org/material-ui/issues/11865

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Dec 27, 2019

While this specific customization is doable without using RadioGroupContext I think we can allow this.

Oh, how would you do it instead?

Manually do the checked test and pass it to MyFormControlLabel: https://codesandbox.io/s/material-demo-6dbb3. It’s not pretty but does the job. Likely based on personal experience whether you prefer maintaining repetition or internals of third party libraries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose RadioGroupContext via useRadioGroup Hook
I have searched the issues of this repository and believe that this is not a duplicate. Summary. Expose RadioGroupContext via useRadioGroup ...
Read more >
Using React's Context API to Simply Components - Muggle-born
Since we're using React Hooks we'll leverage the useContext() hook instead of using Context.Consumer . function useRadioGroup() {
Read more >
https://auctions.truckandtrailer.co.za/res/dist/at...
node_modules/@material-ui/core/esm/RadioGroup/useRadioGroup.js", "../../../. ... utils/useControlled';\nimport RadioGroupContext from '.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found