-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathFieldSet.js
More file actions
69 lines (58 loc) · 1.69 KB
/
FieldSet.js
File metadata and controls
69 lines (58 loc) · 1.69 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import FormElement from './FormElement';
import { uuid } from './util';
const FieldSet = ({ className, label, children, ...props }) => {
const fsClassNames = classnames(className, 'slds-form--compound');
return (
<fieldset className={ fsClassNames } { ...props }>
{
label ?
<legend className='slds-form-element__label'>{ label }</legend> :
null
}
<div className='form-element__group'>
{ children }
</div>
</fieldset>
);
};
FieldSet.propTypes = {
className: PropTypes.string,
label: PropTypes.string,
children: PropTypes.node,
};
FieldSet.isFormElement = true;
class Row extends Component {
renderChild(totalCols, child) {
if (child && !child.type.isFormElement) {
const { id = `form-element-${uuid()}` } = child.props;
const formElemProps = { id, totalCols, cols: 1 };
return (
<FormElement { ...formElemProps }>
{ React.cloneElement(child, { id }) }
</FormElement>
);
}
return React.cloneElement(child, { totalCols });
}
render() {
const { className, cols, children } = this.props;
const totalCols = cols || React.Children.count(children);
const rowClassNames = classnames(className, 'slds-form-element__row');
return (
<div className={ rowClassNames }>
{ React.Children.map(children, this.renderChild.bind(this, totalCols)) }
</div>
);
}
}
Row.propTypes = {
className: PropTypes.string,
cols: PropTypes.number,
children: PropTypes.node,
};
Row.isFormElement = true;
FieldSet.Row = Row;
export default FieldSet;