-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathCheckboxGroup.js
More file actions
108 lines (99 loc) · 2.88 KB
/
CheckboxGroup.js
File metadata and controls
108 lines (99 loc) · 2.88 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
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import FormElement from './FormElement';
export default class CheckboxGroup extends React.Component {
constructor() {
super();
this.onChange = this.onChange.bind(this);
this.renderControl = this.renderControl.bind(this);
}
onChange(e) {
if (this.props.onChange) {
const values = [];
React.Children.forEach(this.props.children, (check, i) => {
const el = check.props.ref || this[`check${(i + 1)}`];
const checkEl = el.querySelector('input[type=checkbox]');
if (checkEl && checkEl.checked) {
values.push(check.props.value);
}
});
this.props.onChange(e, values);
}
}
renderControl(checkbox, i) {
const props = { grouped: true };
if (checkbox.props.ref) {
props.ref = checkbox.props.ref;
} else {
props.checkboxRef = node => (this[`check${(i + 1)}`] = node);
}
if (this.props.name) {
props.name = this.props.name;
}
return React.cloneElement(checkbox, props);
}
render() {
const {
className, label, totalCols, cols, style, required, error, children, ...props
} = this.props;
const grpClassNames = classnames(
className,
'slds-form-element',
{
'slds-has-error': error,
'slds-is-required': required,
},
typeof totalCols === 'number' ? `slds-size--${cols || 1}-of-${totalCols}` : null
);
const grpStyles = typeof totalCols === 'number' ? { display: 'inline-block', ...style } : style;
const errorMessage =
error ?
(typeof error === 'string' ? error :
typeof error === 'object' ? error.message :
undefined) :
undefined;
delete props.onChange;
return (
<fieldset
className={ grpClassNames }
style={ grpStyles }
onChange={ this.onChange }
{ ...props }
>
<legend className='slds-form-element__label slds-form-element__label--top'>
{ label }
{
required ?
<abbr className='slds-required'>*</abbr> :
undefined
}
</legend>
<div
className='slds-form-element__control'
>
{ React.Children.map(children, this.renderControl) }
{
errorMessage ?
<div className='slds-form-element__help'>{ errorMessage }</div> :
undefined
}
</div>
</fieldset>
);
}
}
CheckboxGroup.propTypes = {
className: PropTypes.string,
label: PropTypes.string,
required: PropTypes.bool,
error: FormElement.propTypes.error,
name: PropTypes.string,
totalCols: PropTypes.number,
cols: PropTypes.number,
onChange: PropTypes.func,
children: PropTypes.node,
/* eslint-disable react/forbid-prop-types */
style: PropTypes.object,
};
CheckboxGroup.isFormElement = true;