-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathForm.js
More file actions
47 lines (41 loc) · 1.2 KB
/
Form.js
File metadata and controls
47 lines (41 loc) · 1.2 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import FormElement from './FormElement';
import { uuid } from './util';
export default class Form extends Component {
constructor() {
super();
this.renderFormElement = this.renderFormElement.bind(this);
}
renderFormElement(element) {
if (element && !element.type.isFormElement) {
const { id = `form-element-${uuid()}` } = element.props;
const formElemProps = { id };
return (
<FormElement { ...formElemProps }>
{ React.cloneElement(element, { id }) }
</FormElement>
);
}
return element;
}
render() {
const { className, type, children, ...props } = this.props;
const formClassNames = classnames(className, `slds-form--${type}`);
return (
<form className={ formClassNames } { ...props }>
{ React.Children.map(children, this.renderFormElement) }
</form>
);
}
}
const FORM_TYPES = ['stacked', 'horizontal', 'inline', 'compound'];
Form.propTypes = {
className: PropTypes.string,
type: PropTypes.oneOf(FORM_TYPES),
children: PropTypes.node,
};
Form.defaultProps = {
type: 'stacked',
};