Skip to content

Commit a7b6cf9

Browse files
committed
Merge pull request mui#278 from mmrtnz/switches-valueLink
Switches checkedLink and default Id
2 parents 6ec4673 + 65b0b92 commit a7b6cf9

File tree

6 files changed

+144
-88
lines changed

6 files changed

+144
-88
lines changed

docs/src/app/components/pages/components/switches.jsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Checkbox = mui.Checkbox;
44
var RadioButton = mui.RadioButton;
55
var RadioButtonGroup = mui.RadioButtonGroup;
66
var Toggle = mui.Toggle;
7+
var CodeExample = require('../../code-example/code-example.jsx');
78
var ComponentDoc = require('../../component-doc.jsx');
89
var RaisedButton = mui.RaisedButton;
910

@@ -59,11 +60,9 @@ var SwitchesPage = React.createClass({
5960
' label="initiate self-destruct sequence"\n' +
6061
' disabled={true} />\n\n';
6162

62-
var desc = 'This component generates a switches element and all props except for the custom ' +
63-
'props below will be passed down to the switch element. All switch can now accept input ' +
64-
'attributes of type "checkbox" and "radio" as properties. This can be seen in checkbox ' +
65-
'3. In it the input attribute "disabled" is included as a property and is handled ' +
66-
'accordingly';
63+
var desc = 'These components extend their current input elements (checkbox and radio) and ' +
64+
'will support all of its props and events. Checkboxes and Toggles support ' +
65+
'checkedLink';
6766

6867
var componentInfo = [
6968
{
@@ -161,6 +160,12 @@ var SwitchesPage = React.createClass({
161160
desc: 'Sets the default radio button to be the one whose value matches ' +
162161
'defaultSelected (case-sensitive). This will override any individual radio ' +
163162
'button with the defaultChecked or checked property stated.'
163+
},
164+
{
165+
name: 'valueSelected',
166+
type: 'string',
167+
header: 'optional',
168+
desc: 'The value of the currently selected radio button.'
164169
}
165170
]
166171
},
@@ -178,6 +183,11 @@ var SwitchesPage = React.createClass({
178183
header: 'RadioButtonGroup.setSelectedValue(newSelection)',
179184
desc: 'Sets the selected radio button to the radio button whose value matches ' +
180185
'newSelection'
186+
},
187+
{
188+
name: 'clearValue',
189+
header: 'RadioButtonGroup.clearValue()',
190+
desc: 'Clears the selected value for the radio button group.'
181191
}
182192
]
183193
},
@@ -298,8 +308,7 @@ var SwitchesPage = React.createClass({
298308
id="checkboxId1"
299309
name="checkboxName1"
300310
value="checkboxValue1"
301-
label="went for a run today"
302-
onCheck={this._onCheck}/>
311+
label="went for a run today"/>
303312
</div>
304313
<div className="switches-example-container">
305314
<Checkbox

src/js/checkbox.jsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var React = require('react');
22
var Classable = require('./mixins/classable.js');
3+
var DomIdable = require('./mixins/dom-idable.js');
34
var EnhancedSwitch = require('./enhanced-switch.jsx');
45
var CheckboxOutline = require('./svg-icons/toggle-check-box-outline-blank.jsx');
56
var CheckboxChecked = require('./svg-icons/toggle-check-box-checked.jsx');
67

78
var Checkbox = React.createClass({
89

9-
mixins: [Classable],
10+
mixins: [Classable, DomIdable],
1011

1112
propTypes: {
1213
id: React.PropTypes.string,
@@ -17,43 +18,52 @@ var Checkbox = React.createClass({
1718
},
1819

1920
componentDidMount: function() {
20-
this.setState({switched: this.refs.enhancedSwitch.isSwitched()});
21+
this.setState({checked: this.refs.enhancedSwitch.isSwitched()});
2122
},
2223

2324
getInitialState: function() {
2425
return {
25-
switched: this.props.defaultChecked || this.props.checked
26+
checked: this.props.defaultChecked || this.props.checked
2627
}
2728
},
2829

2930
componentWillReceiveProps: function(nextProps) {
30-
var hasCheckedProperty = nextProps.hasOwnProperty('checked');
31-
var hasDifferentDefaultProperty =
31+
var hasCheckedLinkProp = nextProps.hasOwnProperty('checkedLink');
32+
var hasCheckedProp = nextProps.hasOwnProperty('checked');
33+
var hasNewDefaultProp =
3234
(nextProps.hasOwnProperty('defaultChecked') &&
3335
(nextProps.defaultChecked != this.props.defaultChecked));
34-
35-
if (hasCheckedProperty) {
36-
this.setState({switched: nextProps.checked});
37-
} else if (hasDifferentDefaultProperty) {
38-
this.setState({switched: nextProps.defaultChecked});
36+
var newState = {};
37+
38+
if (hasCheckedProp) {
39+
newState.checked = nextProps.checked;
40+
} else if (hasCheckedLinkProp) {
41+
newState.checked = nextProps.checkedLink.value;
42+
} else if (hasNewDefaultProp) {
43+
newState.checked = nextProps.defaultChecked;
3944
}
45+
46+
if (newState) this.setState(newState);
4047
},
4148

4249
render: function() {
4350
var {
51+
id,
4452
onCheck,
4553
...other
4654
} = this.props;
4755

4856
var classes = this.getClasses("mui-checkbox", {
4957
'mui-switch-wrap': true,
50-
'mui-is-switched': this.state.switched,
58+
'mui-is-switched': this.state.checked,
5159
'mui-is-disabled': this.props.disabled,
5260
'mui-is-required': this.props.required
5361
});
5462

63+
var inputId = this.props.id || this.getDomId();
64+
5565
var labelElement = this.props.label ? (
56-
<label className="mui-switch-label" htmlFor={this.props.id}>
66+
<label className="mui-switch-label" htmlFor={inputId}>
5767
{this.props.label}
5868
</label>
5969
) : null;
@@ -63,12 +73,13 @@ var Checkbox = React.createClass({
6373

6474
<EnhancedSwitch
6575
{...other}
76+
id={inputId}
6677
ref="enhancedSwitch"
6778
inputType="checkbox"
6879
onSwitch={this._onCheck}
6980
defaultSwitched={this.props.defaultChecked} />
7081

71-
<div className="mui-checkbox-icon mui-switch">
82+
<div className="mui-checkbox-icon mui-switch">
7283
<CheckboxOutline className="mui-checkbox-box" />
7384
<CheckboxChecked className="mui-checkbox-check" />
7485
</div>
@@ -80,7 +91,7 @@ var Checkbox = React.createClass({
8091
},
8192

8293
_onCheck: function(e, isInputChecked) {
83-
if (!this.props.hasOwnProperty('checked')) this.setState({switched: !this.refs.enhancedSwitch.state.switched});
94+
if (!this.props.hasOwnProperty('checked')) this.setState({checked: !this.refs.enhancedSwitch.state.checked});
8495
if (this.props.onCheck) this.props.onCheck(e, isInputChecked);
8596
},
8697

src/js/enhanced-switch.jsx

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ var EnhancedSwitch = React.createClass({
1414
defaultSwitched: React.PropTypes.bool
1515
},
1616

17-
mixins: [Classable],
18-
1917
getInitialState: function() {
2018
return {
21-
switched: this.props.defaultSwitched || false
19+
switched: this.props.defaultSwitched ||
20+
(this.props.valueLink && this.props.valueLink.value)
2221
}
2322
},
2423

@@ -28,28 +27,26 @@ var EnhancedSwitch = React.createClass({
2827
},
2928

3029
componentWillReceiveProps: function(nextProps) {
31-
var hasCheckedProperty = nextProps.hasOwnProperty('checked');
32-
var hasDifferentDefaultProperty =
30+
var hasCheckedLinkProp = nextProps.hasOwnProperty('checkedLink');
31+
var hasCheckedProp = nextProps.hasOwnProperty('checked');
32+
var hasNewDefaultProp =
3333
(nextProps.hasOwnProperty('defaultSwitched') &&
3434
(nextProps.defaultSwitched != this.props.defaultSwitched));
35+
var newState = {};
3536

36-
if (hasCheckedProperty) {
37-
this.setState({switched: nextProps.checked});
38-
} else if (hasDifferentDefaultProperty) {
39-
this.setState({switched: nextProps.defaultSwitched});
40-
}
41-
},
4237

43-
handleChange: function(e) {
44-
var isInputChecked = this.refs.checkbox.getDOMNode().checked;
38+
if (hasCheckedProp) {
39+
newState.switched = nextProps.checked;
40+
} else if (hasCheckedLinkProp) {
41+
newState.switched = nextProps.checkedLink.value;
42+
} else if (hasNewDefaultProp) {
43+
newState.switched = nextProps.defaultSwitched;
44+
}
4545

46-
if (!this.props.hasOwnProperty('checked')) this.setState({switched: isInputChecked});
47-
if (this.props.onSwitch) this.props.onSwitch(e, isInputChecked);
46+
if (newState) this.setState(newState);
4847
},
4948

5049
render: function() {
51-
var classes = this.getClasses("mui-enhanced-switch");
52-
5350
var {
5451
type,
5552
name,
@@ -59,15 +56,24 @@ var EnhancedSwitch = React.createClass({
5956
...other
6057
} = this.props;
6158

59+
var inputProps;
60+
61+
inputProps = {
62+
ref: "checkbox",
63+
type: this.props.inputType,
64+
name: this.props.name,
65+
value: this.props.value,
66+
};
67+
68+
if (!this.props.hasOwnProperty('checkedLink')) {
69+
inputProps.onChange = this._handleChange;
70+
}
71+
6272
return (
6373
<input
64-
{...other}
65-
ref="checkbox"
66-
className={classes}
67-
type={this.props.inputType}
68-
name={this.props.name}
69-
value={this.props.value}
70-
onChange={this.handleChange} />
74+
{...other}
75+
{...inputProps}
76+
className="mui-enhanced-switch"/>
7177
);
7278
},
7379

@@ -85,7 +91,19 @@ var EnhancedSwitch = React.createClass({
8591
var message = 'Cannot call set method while checked is defined as a property.';
8692
console.error(message);
8793
}
94+
},
95+
96+
_handleChange: function(e) {
97+
var isInputChecked = this.refs.checkbox.getDOMNode().checked;
98+
99+
if (!this.props.hasOwnProperty('checked')) this.setState({switched: isInputChecked});
100+
if (this.props.onSwitch) this.props.onSwitch(e, isInputChecked);
101+
},
102+
103+
getValue: function() {
104+
return this.refs.checkbox.getDOMNode().value;
88105
}
106+
89107
});
90108

91109
module.exports = EnhancedSwitch;

src/js/radio-button-group.jsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var RadioButtonGroup = React.createClass({
1010

1111
propTypes: {
1212
name: React.PropTypes.string.isRequired,
13+
valueSelected: React.PropTypes.string,
1314
defaultSelected: React.PropTypes.string,
1415
onChange: React.PropTypes.func
1516
},
@@ -20,19 +21,9 @@ var RadioButtonGroup = React.createClass({
2021
},
2122

2223
getInitialState: function() {
23-
var initialSelection = '';
24-
25-
if (this.props.hasOwnProperty('defaultSelected')) {
26-
initialSelection = this.props.defaultSelected;
27-
} else {
28-
this.props.children.forEach(function(option) {
29-
if (this._hasCheckAttribute(option) || option.props.defaultChecked) initialSelection = option.props.value;
30-
}, this);
31-
}
32-
3324
return {
3425
numberCheckedRadioButtons: 0,
35-
selected: initialSelection
26+
selected: this.props.valueSelected || this.props.defaultSelected || ''
3627
};
3728
},
3829

@@ -47,21 +38,17 @@ var RadioButtonGroup = React.createClass({
4738
},
4839

4940
componentWillReceiveProps: function(nextProps) {
50-
var newSelection = '';
51-
52-
nextProps.children.forEach(function(option) {
53-
if (this._hasCheckAttribute(option) || option.props.defaultChecked) newSelection = option.props.value;
54-
}, this);
55-
56-
this.setState({selected: newSelection});
41+
if (nextProps.hasOwnProperty('valueSelected')) {
42+
this.setState({selected: nextProps.valueSelected});
43+
}
5744
},
5845

5946
render: function() {
6047
var options = this.props.children.map(function(option) {
6148

6249
var {
6350
name,
64-
value,
51+
value,
6552
label,
6653
onCheck,
6754
...other
@@ -88,9 +75,7 @@ var RadioButtonGroup = React.createClass({
8875

8976
_updateRadioButtons: function(newSelection) {
9077
if (this.state.numberCheckedRadioButtons == 0) {
91-
9278
this.setState({selected: newSelection});
93-
9479
} else {
9580
var message = "Cannot select a different radio button while another radio button " +
9681
"has the 'checked' property set to true.";
@@ -113,7 +98,12 @@ var RadioButtonGroup = React.createClass({
11398

11499
setSelectedValue: function(newSelection) {
115100
this._updateRadioButtons(newSelection);
101+
},
102+
103+
clearValue: function() {
104+
this.setSelectedValue('');
116105
}
106+
117107
});
118108

119109
module.exports = RadioButtonGroup;

0 commit comments

Comments
 (0)