Skip to content

Commit 5dcec9b

Browse files
oliviertassinariJoe Shelby
authored and
Joe Shelby
committed
[FormControlLabel] Add a failing test case and fix it (mui#12141)
1 parent 6812f0e commit 5dcec9b

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

packages/material-ui/src/FormControlLabel/FormControlLabel.js

+24-27
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,37 @@ function FormControlLabel(props, context) {
4747
value,
4848
...other
4949
} = props;
50-
5150
const { muiFormControl } = context;
52-
let disabled = disabledProp;
5351

54-
if (typeof control.props.disabled !== 'undefined') {
55-
if (typeof disabled === 'undefined') {
56-
disabled = control.props.disabled;
57-
}
52+
let disabled = disabledProp;
53+
if (typeof disabled === 'undefined' && typeof control.props.disabled !== 'undefined') {
54+
disabled = control.props.disabled;
5855
}
59-
60-
if (muiFormControl) {
61-
if (typeof disabled === 'undefined') {
62-
disabled = muiFormControl.disabled;
63-
}
56+
if (typeof disabled === 'undefined' && muiFormControl) {
57+
disabled = muiFormControl.disabled;
6458
}
6559

66-
const className = classNames(
67-
classes.root,
68-
{
69-
[classes.disabled]: disabled,
70-
},
71-
classNameProp,
72-
);
60+
const controlProps = {
61+
disabled,
62+
};
63+
['checked', 'name', 'onChange', 'value', 'inputRef'].forEach(key => {
64+
if (typeof control.props[key] === 'undefined' && typeof props[key] !== 'undefined') {
65+
controlProps[key] = props[key];
66+
}
67+
});
7368

7469
return (
75-
<label className={className} {...other}>
76-
{React.cloneElement(control, {
77-
disabled,
78-
checked: typeof control.props.checked === 'undefined' ? checked : control.props.checked,
79-
name: control.props.name || name,
80-
onChange: control.props.onChange || onChange,
81-
value: control.props.value || value,
82-
inputRef: control.props.inputRef || inputRef,
83-
})}
70+
<label
71+
className={classNames(
72+
classes.root,
73+
{
74+
[classes.disabled]: disabled,
75+
},
76+
classNameProp,
77+
)}
78+
{...other}
79+
>
80+
{React.cloneElement(control, controlProps)}
8481
<Typography
8582
component="span"
8683
className={classNames(classes.label, { [classes.disabled]: disabled })}

packages/material-ui/src/FormControlLabel/FormControlLabel.test.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createShallow, createMount, getClasses } from '../test-utils';
44
import Checkbox from '../Checkbox';
55
import FormControlLabel from './FormControlLabel';
66

7-
describe('FormControlLabel', () => {
7+
describe('<FormControlLabel />', () => {
88
let shallow;
99
let mount;
1010
let classes;
@@ -28,7 +28,7 @@ describe('FormControlLabel', () => {
2828
});
2929

3030
describe('prop: disabled', () => {
31-
it('should disable everything', () => {
31+
it('should disable everything 1', () => {
3232
const wrapper = shallow(<FormControlLabel label="Pizza" disabled control={<div />} />);
3333
const label = wrapper.childAt(1);
3434
assert.strictEqual(
@@ -41,7 +41,7 @@ describe('FormControlLabel', () => {
4141
assert.strictEqual(label.hasClass(classes.disabled), true);
4242
});
4343

44-
it('should disable everything', () => {
44+
it('should disable everything 2', () => {
4545
const wrapper = shallow(<FormControlLabel label="Pizza" control={<div disabled />} />);
4646
const label = wrapper.childAt(1);
4747
assert.strictEqual(
@@ -105,4 +105,16 @@ describe('FormControlLabel', () => {
105105
});
106106
});
107107
});
108+
109+
it('should not inject extra properties', () => {
110+
// eslint-disable-next-line react/prop-types
111+
const Control = ({ inputRef, ...props }) => <div name="name" {...props} />;
112+
const wrapper = mount(<FormControlLabel label="Pizza" control={<Control />} />);
113+
assert.strictEqual(wrapper.find('div').props().name, 'name');
114+
});
115+
116+
it('should forward some properties', () => {
117+
const wrapper = mount(<FormControlLabel value="value" label="Pizza" control={<div />} />);
118+
assert.strictEqual(wrapper.find('div').props().value, 'value');
119+
});
108120
});

0 commit comments

Comments
 (0)