forked from pterolex/react-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioButtonGroup.jsx
89 lines (73 loc) · 2.21 KB
/
RadioButtonGroup.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
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
/**
* @jsx React.DOM
*/
'use strict';
var React = require('react/addons');
var cx = React.addons.classSet;
var FaIcon = require('./FaIcon.jsx');
require('./RadioButtonGroup.less');
var RadioButtonGroup = React.createClass({
propTypes: {
activeId: React.PropTypes.string,
buttons: React.PropTypes.array.isRequired,
disabled: React.PropTypes.bool,
onChange: React.PropTypes.func,
display: React.PropTypes.string
},
getInitialState() {
return {
activeId: this.props.activeId
};
},
componentWillReceiveProps(nextProps) {
this.setState({
activeId: nextProps.activeId
});
},
handleClick(id) {
if (this.props.disabled) return;
this.setState({
activeId: id
});
this.props.onChange(id);
},
renderSingleBtn(btn) {
var isActive = this.state.activeId === btn.id;
var rectangleClass = cx({
'WLC-active': isActive,
'WLC-RadioBtnWithLabel': true,
});
var labelNode = btn.label
? <span onTouchTap={this.handleClick.bind(this, btn.id)}>{btn.label}</span>
: '';
if (this.props.display === 'rectangle') return (
<div className={rectangleClass} key={btn.id} onTouchTap={this.handleClick.bind(this, btn.id)}>
{labelNode}
</div>
);
return (
<div className='WLC-RadioBtnWithLabel' key={btn.id}>
<FaIcon type={(btn.icon).toLowerCase()}
value={btn.iconText}
onClick={this.handleClick.bind(this, btn.id)} />
{labelNode}
</div>
);
},
render() {
var radioButtonsNode = this.props.buttons.map( (btn,i) => {
return this.renderSingleBtn(btn,i);
});
var rootClass = cx({
'WLC-RadioButtonGroup': true,
'WLC-disabled': this.props.disabled,
'WLC-rectangle': this.props.display === 'rectangle',
});
return (
<div className={rootClass}>
{radioButtonsNode}
</div>
);
}
});
module.exports = RadioButtonGroup;