forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioButtonGroup.js
More file actions
157 lines (137 loc) · 4.01 KB
/
RadioButtonGroup.js
File metadata and controls
157 lines (137 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import React, {Component, PropTypes} from 'react';
import RadioButton from '../RadioButton';
import warning from 'warning';
class RadioButtonGroup extends Component {
static propTypes = {
/**
* Should be used to pass `RadioButton` components.
*/
children: PropTypes.node,
/**
* The CSS class name of the root element.
*/
className: PropTypes.string,
/**
* The `value` property of the radio button that will be
* selected by default. This takes precedence over the `checked` property
* of the `RadioButton` elements.
*/
defaultSelected: PropTypes.any,
/**
* Where the label will be placed for all child radio buttons.
* This takes precedence over the `labelPosition` property of the
* `RadioButton` elements.
*/
labelPosition: PropTypes.oneOf(['left', 'right']),
/**
* The name that will be applied to all child radio buttons.
*/
name: PropTypes.string.isRequired,
/**
* Callback function that is fired when a radio button has
* been checked.
*
* @param {object} event `change` event targeting the selected
* radio button.
* @param {*} value The `value` of the selected radio button.
*/
onChange: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* The `value` of the currently selected radio button.
*/
valueSelected: PropTypes.any,
};
static defaultProps = {
style: {},
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
numberCheckedRadioButtons: 0,
selected: '',
};
componentWillMount() {
let cnt = 0;
React.Children.forEach(this.props.children, (option) => {
if (this.hasCheckAttribute(option)) cnt++;
}, this);
this.setState({
numberCheckedRadioButtons: cnt,
selected: this.props.valueSelected || this.props.defaultSelected || '',
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.hasOwnProperty('valueSelected')) {
this.setState({
selected: nextProps.valueSelected,
});
}
}
hasCheckAttribute(radioButton) {
return radioButton.props.hasOwnProperty('checked') &&
radioButton.props.checked;
}
updateRadioButtons(newSelection) {
if (this.state.numberCheckedRadioButtons === 0) {
this.setState({selected: newSelection});
} else {
warning(false, `Cannot select a different radio button while another radio button
has the 'checked' property set to true.`);
}
}
handleChange = (event, newSelection) => {
this.updateRadioButtons(newSelection);
// Successful update
if (this.state.numberCheckedRadioButtons === 0) {
if (this.props.onChange) this.props.onChange(event, newSelection);
}
};
getSelectedValue() {
return this.state.selected;
}
setSelectedValue(newSelectionValue) {
this.updateRadioButtons(newSelectionValue);
}
clearValue() {
this.setSelectedValue('');
}
render() {
const {prepareStyles} = this.context.muiTheme;
const options = React.Children.map(this.props.children, (option) => {
const {
name, // eslint-disable-line no-unused-vars
value, // eslint-disable-line no-unused-vars
label, // eslint-disable-line no-unused-vars
onCheck, // eslint-disable-line no-unused-vars
...other,
} = option.props;
return (
<RadioButton
{...other}
ref={option.props.value}
name={this.props.name}
key={option.props.value}
value={option.props.value}
label={option.props.label}
labelPosition={this.props.labelPosition}
onCheck={this.handleChange}
checked={option.props.value === this.state.selected}
/>
);
}, this);
return (
<div
style={prepareStyles(Object.assign({}, this.props.style))}
className={this.props.className}
>
{options}
</div>
);
}
}
export default RadioButtonGroup;