import React from 'react';
import PropTypes from 'prop-types';
import Form from 'antd/lib/form';
import Input from 'antd/lib/input';
import InputNumber from 'antd/lib/input-number';
import Checkbox from 'antd/lib/checkbox';
import Button from 'antd/lib/button';
import Upload from 'antd/lib/upload';
import Icon from 'antd/lib/icon';
import { includes } from 'lodash';
import { react2angular } from 'react2angular';
import { toastr } from '@/services/ng';
import { Field, Action, AntdForm } from '../proptypes';
import helper from './dynamicFormHelper';
const fieldRules = ({ type, required, minLength }) => {
const requiredRule = required;
const minLengthRule = minLength && includes(['text', 'email', 'password'], type);
return [
requiredRule && { required, message: 'This field is required.' },
minLengthRule && { min: minLength, message: 'This field is too short.' },
].filter(rule => rule);
};
export const DynamicForm = Form.create()(class DynamicForm extends React.Component {
static propTypes = {
fields: PropTypes.arrayOf(Field),
actions: PropTypes.arrayOf(Action),
feedbackIcons: PropTypes.bool,
hideSubmitButton: PropTypes.bool,
saveText: PropTypes.string,
onSubmit: PropTypes.func,
form: AntdForm.isRequired,
};
static defaultProps = {
fields: [],
actions: [],
feedbackIcons: false,
hideSubmitButton: false,
saveText: 'Save',
onSubmit: () => {},
};
constructor(props) {
super(props);
this.state = {
isSubmitting: false,
inProgressActions: [],
};
this.actionCallbacks = this.props.actions.reduce((acc, cur) => ({
...acc,
[cur.name]: cur.callback,
}), null);
props.actions.forEach((action) => {
this.state.inProgressActions[action.name] = false;
});
}
setActionInProgress = (actionName, inProgress) => {
this.setState(prevState => ({
inProgressActions: {
...prevState.inProgressActions,
[actionName]: inProgress,
},
}));
};
handleSubmit = (e) => {
this.setState({ isSubmitting: true });
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
this.props.onSubmit(
values,
(msg) => {
const { setFieldsValue, getFieldsValue } = this.props.form;
this.setState({ isSubmitting: false });
setFieldsValue(getFieldsValue()); // reset form touched state
toastr.success(msg);
},
(msg) => {
this.setState({ isSubmitting: false });
toastr.error(msg);
},
);
} else this.setState({ isSubmitting: false });
});
}
handleAction = (e) => {
const actionName = e.target.dataset.action;
this.setActionInProgress(actionName, true);
this.actionCallbacks[actionName](() => {
this.setActionInProgress(actionName, false);
});
}
base64File = (fieldName, e) => {
if (e && e.fileList[0]) {
helper.getBase64(e.file).then((value) => {
this.props.form.setFieldsValue({ [fieldName]: value });
});
}
}
renderUpload(field, props) {
const { getFieldDecorator, getFieldValue } = this.props.form;
const { name, initialValue } = field;
const fileOptions = {
rules: fieldRules(field),
initialValue,
getValueFromEvent: this.base64File.bind(this, name),
};
const disabled = getFieldValue(name) !== undefined && getFieldValue(name) !== initialValue;
const upload = (