-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.jsx
44 lines (39 loc) · 1.08 KB
/
index.jsx
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
import React, {PropTypes} from 'react';
function radio(name, selectedValue, onChange) {
return React.createClass({
render: function() {
const optional = {};
if(selectedValue !== undefined) {
optional.checked = (this.props.value === selectedValue);
}
if(typeof onChange === 'function') {
optional.onChange = onChange.bind(null, this.props.value);
}
return (
<input
{...this.props}
type="radio"
name={name}
{...optional} />
);
}
});
}
export default React.createClass({
displayName: 'RadioGroup',
propTypes: {
name: PropTypes.string,
selectedValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.bool,
]),
onChange: PropTypes.func,
children: PropTypes.func.isRequired,
},
render: function() {
const {name, selectedValue, onChange, children} = this.props;
const renderedChildren = children(radio(name, selectedValue, onChange));
return renderedChildren && React.Children.only(renderedChildren);
}
});