-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Expose RadioGroupContext via useRadioGroup Hook #18918
Comments
That looks fine to me. I can't remember a situation where exposing the context as read-only has locked us into a "bad" API. While this specific customization is doable without using |
Oh, how would you do it instead? |
Do you mean excluding values such as update functions from what's returned by |
Manually do the checked test and pass it to |
Is there a way to hook into the By importing the |
I'm reopening as I believe we miss:
|
FYI it already is exported via https://github.com/mui-org/material-ui/blob/5aad5f2f729066534bd9d408cc8518b51a9f0f14/packages/material-ui/src/RadioGroup/index.js#L2 and https://github.com/mui-org/material-ui/blob/5aad5f2f729066534bd9d408cc8518b51a9f0f14/packages/material-ui/src/index.js#L239 so we just need a documentation change. |
I am seeing a weird situation. When I am importing the RadioGroup and useRadioGroup like this
I notice that the context is being created twice. This is because However, if I do
Then both modules refer to the same esm module Basically, because they refer to modules in different places, the context created on import is also different. I cannot repro this on codesandbox, but I can repro this locally. I assume codesandbox uses UMD so this problem would not be prevalent there. Got a small hole in my knowledge as I don't know exactly why it choses to go for the cjs in the first example. |
@joonarahko that works as expected, you're not supposed to import beyond the 2nd level. The second way you showed is the correct way: https://material-ui.com/guides/minimizing-bundle-size/#option-1 You may find this ESLint plugin useful (currently not published, so just paste it into your project): https://github.com/mui-org/material-ui/blob/27471b4/packages/eslint-plugin-material-ui/src/rules/restricted-path-imports.js |
Ok :) Makes sense. Thanks @NMinhNguyen |
Summary 💡
Expose
RadioGroupContext
viauseRadioGroup
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
ischecked
, so I can apply custom styling to achecked
label, for example (see demo above). In MUI v3, this was possible becauseRadioGroup
would usecloneElement
and passchecked
prop tochildren
. With the new context implementation (#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 customuseFormControl
Hook, so we could probably do the same in this instance? I'm happy to raise a PR for this.Prior art: #11865
The text was updated successfully, but these errors were encountered: