chowa
Version:
UI component library based on React
64 lines (63 loc) • 2.19 kB
JavaScript
/**
* @license chowa v1.1.3
*
* Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn).
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
;
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const PropTypes = require("prop-types");
const classnames_1 = require("classnames");
const utils_1 = require("../utils");
const form_item_1 = require("./form-item");
const create_form_1 = require("./create-form");
class Form extends React.PureComponent {
constructor(props) {
super(props);
[
'onSubmitHandler'
].forEach((fn) => {
this[fn] = this[fn].bind(this);
});
}
onSubmitHandler(e) {
if (this.props.onSubmit) {
this.props.onSubmit();
}
e.preventDefault();
}
render() {
const { children, className, style, inline, labelPosition } = this.props;
const componentClass = classnames_1.default({
[utils_1.preClass('form')]: true,
[utils_1.preClass('form-inline')]: inline,
[className]: utils_1.isExist(className)
});
return (React.createElement("form", Object.assign({}, utils_1.otherProps(Form.propTypes, this.props), { onSubmit: this.onSubmitHandler, style: style, className: componentClass }), React.Children.map(children, (child) => {
if (utils_1.isReactElement(child) && child.type === form_item_1.default) {
return React.cloneElement(child, {
labelPosition: child.props.labelPosition === 'right'
? labelPosition : child.props.labelPosition
});
}
return child;
})));
}
}
Form.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
labelPosition: PropTypes.oneOf(['top', 'left', 'right']),
inline: PropTypes.bool,
onSubmit: PropTypes.func
};
Form.defaultProps = {
inline: false,
labelPosition: 'right'
};
Form.Item = form_item_1.default;
Form.createForm = create_form_1.default;
exports.default = Form;