This repository has been archived by the owner on Jul 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
CheckboxGroup.babel.js
83 lines (70 loc) · 2.39 KB
/
CheckboxGroup.babel.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
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
import $ from 'jquery';
import React from 'react'; // eslint-disable-line no-unused-vars
import ReactDOM from 'reactDom';
import Helper from 'babel/utils/helper/Helper';
import IconTooltip from 'babel/components/helper/tooltip/IconTooltip';
import FormGroup from 'babel/components/forms/base/FormGroup';
import 'babel/utils/helper/strings/StringUtils';
export default class CheckboxGroup extends FormGroup {
constructor(props) {
super(props);
// Autobind methods
this.getValue = this.getValue.bind(this);
}
componentDidMount() {
this.node = ReactDOM.findDOMNode(this);
this.input = {
value: this.props.defaultValue
};
super.componentDidMount();
}
onChange() {
this.input.value = this.getValue();
super.onChange();
}
getValue() {
const selectedInputs = $(this.node).find('input:checked');
const values = $.map(selectedInputs,(input) => {
return $(input).val();
});
return values.toString();
}
render() {
const inputClasses = Helper.classnames([this.props.className,'checkbox-group','form-group',{
'required': this.props.required,
'has-error': !this.state.isValid
}]);
const defaultValues = this.props.defaultValue.toString().split(',');
return (
<div className={inputClasses}>
<label htmlFor={this.props.id} className="control-label">{this.props.label}</label>
{this.props.tooltip ? <IconTooltip className="form-tooltip" {...this.props.tooltip} /> : null}
{this.props.options.map((current) => {
const id = (this.props.id + current.value + '').toCamelCase();
return (
<div className="checkbox" key={id}>
<label htmlFor={id}>
<input
type="checkbox"
name={this.props.id}
id={id}
value={current.value}
checked={defaultValues.indexOf(current.value.toString()) >= 0 ? 'checked' : null}
onChange={this.onChange}
onBlur={this.onBlur} />
{current.label || current.value}
</label>
</div>
);
})}
{this.getErrorMessage ? this.getErrorMessage() : null}
</div>
);
}
}
$.extend(true,CheckboxGroup.propTypes,{
options: React.PropTypes.array
});
$.extend(true,CheckboxGroup.defaultProps,{
options: []
});