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

[Radio] Fix disabled state styling regression #43592

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/mui-material/src/Radio/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwitchBase from '../internal/SwitchBase';
import RadioButtonIcon from './RadioButtonIcon';
import capitalize from '../utils/capitalize';
import createChainedFunction from '../utils/createChainedFunction';
import useFormControl from '../FormControl/useFormControl';
import useRadioGroup from '../RadioGroup/useRadioGroup';
import radioClasses, { getRadioUtilityClass } from './radioClasses';
import rootShouldForwardProp from '../styles/rootShouldForwardProp';
Expand Down Expand Up @@ -51,7 +52,7 @@ const RadioRoot = styled(SwitchBase, {
},
variants: [
{
props: { color: 'default', disableRipple: false },
props: { color: 'default', disabled: false, disableRipple: false },
style: {
'&:hover': {
backgroundColor: theme.vars
Expand All @@ -63,7 +64,7 @@ const RadioRoot = styled(SwitchBase, {
...Object.entries(theme.palette)
.filter(([, palette]) => palette && palette.main)
.map(([color]) => ({
props: { color, disableRipple: false },
props: { color, disabled: false, disableRipple: false },
style: {
'&:hover': {
backgroundColor: theme.vars
Expand All @@ -75,7 +76,7 @@ const RadioRoot = styled(SwitchBase, {
...Object.entries(theme.palette)
.filter(([, palette]) => palette && palette.main)
.map(([color]) => ({
props: { color },
props: { color, disabled: false },
style: {
[`&.${radioClasses.checked}`]: {
color: (theme.vars || theme).palette[color].main,
Expand Down Expand Up @@ -121,11 +122,26 @@ const Radio = React.forwardRef(function Radio(inProps, ref) {
onChange: onChangeProp,
size = 'medium',
className,
disabled: disabledProp,
disableRipple = false,
...other
} = props;

const muiFormControl = useFormControl();

let disabled = disabledProp;

if (muiFormControl) {
if (typeof disabled === 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only read the value if undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the prop should take precedence over what comes form the form field context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, my bad 😅 I got confused and thought we were checking typeof muiFormControl.disabled === 'undefined'

disabled = muiFormControl.disabled;
}
}

disabled ??= false;

const ownerState = {
...props,
disabled,
disableRipple,
color,
size,
Expand Down Expand Up @@ -154,6 +170,7 @@ const Radio = React.forwardRef(function Radio(inProps, ref) {
checkedIcon={React.cloneElement(checkedIcon, {
fontSize: defaultCheckedIcon.props.fontSize ?? size,
})}
disabled={disabled}
ownerState={ownerState}
classes={classes}
name={name}
Expand Down
31 changes: 31 additions & 0 deletions test/regressions/fixtures/Radio/RadioDisabledState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react';
import Radio from '@mui/material/Radio';

export default function RadioButtonsDisabled() {
const [selectedValue, setSelectedValue] = React.useState('a');

const handleChange = (event) => {
setSelectedValue(event.target.value);
};

return (
<div>
<Radio
disabled
checked={selectedValue === 'a'}
onChange={handleChange}
value="a"
name="radio-buttons"
inputProps={{ 'aria-label': 'A' }}
/>
<Radio
disabled
checked={selectedValue === 'b'}
onChange={handleChange}
value="b"
name="radio-buttons"
inputProps={{ 'aria-label': 'B' }}
/>
</div>
);
}