Skip to content

Instantly share code, notes, and snippets.

@toadkicker
Created February 7, 2020 18:16
Show Gist options
  • Save toadkicker/2f13ef289b8b0931e002a9141de14c71 to your computer and use it in GitHub Desktop.
Save toadkicker/2f13ef289b8b0931e002a9141de14c71 to your computer and use it in GitHub Desktop.
contact form
import { Button } from '@material-ui/core';
import CloseIcon from '@material-ui/icons/Close';
import SaveIcon from '@material-ui/icons/Save';
import UndoIcon from '@material-ui/icons/Undo';
import { Formik } from 'formik';
import { i18n } from 'i18n';
import model from 'modules/contact/contactModel';
import React, { Component } from 'react';
import FormSchema from 'view/shared/form/formSchema';
import Spinner from 'view/shared/Spinner';
import FormWrapper, {
FormButtons,
} from 'view/shared/styles/FormWrapper';
import ViewFormItem from 'view/shared/form/items/ViewFormItem';
import UserAutocompleteFormItem from 'view/iam/autocomplete/UserAutocompleteFormItem';
import ContactTypesAutocompleteFormItem from 'view/contactTypes/autocomplete/ContactTypesAutocompleteFormItem';
const { fields } = model;
class ContactForm extends Component {
schema = new FormSchema(fields.id, [
fields.userId,
fields.contactType,
]);
handleSubmit = (values) => {
const { id, ...data } = this.schema.cast(values);
this.props.onSubmit(id, data);
};
initialValues = () => {
const record = this.props.record;
return this.schema.initialValues(record || {});
};
renderForm() {
const { saveLoading, isEditing, modal } = this.props;
return (
<FormWrapper>
<Formik
initialValues={this.initialValues()}
validationSchema={this.schema.schema}
onSubmit={this.handleSubmit}
render={(form) => {
return (
<form onSubmit={form.handleSubmit}>
{isEditing && (
<ViewFormItem
name={fields.id.name}
label={fields.id.label}
/>
)}
<UserAutocompleteFormItem
name={fields.userId.name}
label={fields.userId.label}
required={fields.userId.required}
showCreate={!this.props.modal}
form={form}
/>
<ContactTypesAutocompleteFormItem
name={fields.contactType.name}
label={fields.contactType.label}
required={fields.contactType.required}
showCreate={!this.props.modal}
form={form}
mode="multiple"
/>
<FormButtons
style={{
flexDirection: modal
? 'row-reverse'
: undefined,
}}
>
<Button
variant="contained"
color="primary"
disabled={saveLoading}
type="button"
onClick={form.handleSubmit}
startIcon={<SaveIcon />}
>
{i18n('common.save')}
</Button>
<Button
disabled={saveLoading}
onClick={form.handleReset}
type="button"
startIcon={<UndoIcon />}
>
{i18n('common.reset')}
</Button>
{this.props.onCancel ? (
<Button
disabled={saveLoading}
onClick={() => this.props.onCancel()}
type="button"
startIcon={<CloseIcon />}
>
{i18n('common.cancel')}
</Button>
) : null}
</FormButtons>
</form>
);
}}
/>
</FormWrapper>
);
}
render() {
const { isEditing, findLoading, record } = this.props;
if (findLoading) {
return <Spinner />;
}
if (isEditing && !record) {
return <Spinner />;
}
return this.renderForm();
}
}
export default ContactForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment