UNPKG

shradmin

Version:

A frontend Framework for building admin application, using antd and mirrorjs.

236 lines (176 loc) 7.44 kB
'use strict'; exports.__esModule = true; var _style3 = require('antd/lib/button/style'); var _button = require('antd/lib/button'); var _button2 = _interopRequireDefault(_button); var _style4 = require('antd/lib/form/style'); var _form = require('antd/lib/form'); var _form2 = _interopRequireDefault(_form); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _mapValues = require('lodash/mapValues'); var _mapValues2 = _interopRequireDefault(_mapValues); var _get = require('lodash/get'); var _get2 = _interopRequireDefault(_get); var _pickBy = require('lodash/pickBy'); var _pickBy2 = _interopRequireDefault(_pickBy); var _isEqual = require('lodash/isEqual'); var _isEqual2 = _interopRequireDefault(_isEqual); var _isFunction = require('lodash/isFunction'); var _isFunction2 = _interopRequireDefault(_isFunction); var _has = require('lodash/has'); var _has2 = _interopRequireDefault(_has); var _inflection = require('inflection'); var _inflection2 = _interopRequireDefault(_inflection); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var FormItem = _form2.default.Item; var buttonLayout = { wrapperCol: { lg: { span: 6, offset: 3 }, xs: { span: 24, offset: 0 }, sm: { span: 12, offset: 4 } } }; var formItemLayout = { labelCol: { lg: { span: 3 }, xs: { span: 24 }, sm: { span: 4 } }, wrapperCol: { lg: { span: 6 }, xs: { span: 24 }, sm: { span: 12 } } }; var defaultFormat = function defaultFormat(value) { return value; }; var defaultParse = function defaultParse(value) { return value; }; var data = {}; var SimpleForm = function (_Component) { _inherits(SimpleForm, _Component); function SimpleForm() { var _temp, _this, _ret; _classCallCheck(this, SimpleForm); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.updateData = function (record) { var _this$props$form = _this.props.form, setFieldsValue = _this$props$form.setFieldsValue, getFieldsValue = _this$props$form.getFieldsValue; var initValues = (0, _mapValues2.default)(record || {}, function (value, source) { var format = (0, _get2.default)(_this.formats, source); return format ? format(value) : value; }); var fields = getFieldsValue(); data = (0, _pickBy2.default)(initValues, function (value, key) { return (0, _has2.default)(fields, key); }); setFieldsValue(data); }, _this.handleSubmit = function (e) { e.preventDefault(); var save = _this.props.save; _this.props.form.validateFieldsAndScroll(function (error, values) { if (!error) { values = (0, _mapValues2.default)(values, function (value, source) { var parse = (0, _get2.default)(_this.parses, source); return parse ? parse(value) : value; }); save(values); } }); }, _temp), _possibleConstructorReturn(_this, _ret); } SimpleForm.prototype.componentDidMount = function componentDidMount() { var _this2 = this; var record = this.props.record; this.parses = {}; this.formats = {}; _react2.default.Children.forEach(this.props.children, function (child) { if (child) { var _child$props = child.props, source = _child$props.source, parse = _child$props.parse, format = _child$props.format; _this2.parses[source] = parse || defaultParse; _this2.formats[source] = format || defaultFormat; } }); this.updateData(record); }; SimpleForm.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { var record = this.props.record; if (nextProps.record && !(0, _isEqual2.default)(record, nextProps.record)) { this.updateData(nextProps.record); } }; SimpleForm.prototype.render = function render() { var _props = this.props, children = _props.children, model = _props.model, record = _props.record, translate = _props.translate, getFieldDecorator = _props.form.getFieldDecorator; return _react2.default.createElement( _form2.default, { onSubmit: this.handleSubmit }, _react2.default.Children.map(children, function (child, index) { if (child) { var _child$props2 = child.props, source = _child$props2.source, input = _child$props2.input, options = _child$props2.options, layoutSpan = _child$props2.layoutSpan; return _react2.default.createElement( FormItem, _extends({ key: index }, formItemLayout, layoutSpan, { label: translate('models.' + model + '.fields.' + source, { _: _inflection2.default.humanize(source) }), colon: false }), input ? getFieldDecorator(source, options)(input) : _react2.default.cloneElement(child, { record: record }) ); } return null; }), _react2.default.createElement( FormItem, buttonLayout, _react2.default.createElement( _button2.default, { type: 'primary', htmlType: 'submit' }, translate('actions.save') ) ) ); }; return SimpleForm; }(_react.Component); SimpleForm.propTypes = process.env.NODE_ENV !== "production" ? { model: _propTypes2.default.string.isRequired, save: _propTypes2.default.func, record: _propTypes2.default.object } : {}; exports.default = _form2.default.create({ onValuesChange: function onValuesChange(props, values) { data = _extends({}, data, values); if ((0, _isFunction2.default)(props.onChange)) { props.onChange(data); } if ((0, _isFunction2.default)(props.onFieldChange)) { props.onFieldChange(values); } } })(SimpleForm); module.exports = exports['default'];