-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioBox.js
57 lines (57 loc) · 1.58 KB
/
RadioBox.js
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
const RadioBox = React.createClass({
getInitialState(){
return {
selected: ''
};
},
render(){
const items = this.props.items;
var keys = items;
if (typeof items === 'object') {
keys = Object.keys(items).map(
function (key) {
return key;
});
}
return (
<table className={'radiobox-group ' + this.props.class}>
<tbody>
{keys.map(this.displayItem)}
</tbody>
</table>
);
},
displayItem(value, index){
var name = this.props.items[value];
var id = this.props.group + '_' + index;
var group = this.props.group;
return (
<tr key={index}>
<td>
<input
type="radio"
id={id}
group={group}
name={group}
value={value}
onChange={this.onItemChange}
checked={this.state.selected === value}
/>
</td>
<td>
<label htmlFor={id}>{name}</label>
</td>
</tr>
);
},
onItemChange(e){
const selected = e.target.value;
this.setState({selected});
this.props.onChange(selected);
},
componentWillMount(){
const selected = this.props.selected;
this.setState({selected});
}
});
export default RadioBox;