2018-04-13 15:33:03 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-09-04 15:48:58 +03:00
|
|
|
import { Form } from 'react-final-form';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
2019-06-17 23:40:47 +03:00
|
|
|
import {
|
|
|
|
FormFields,
|
|
|
|
BlockSaveButton,
|
|
|
|
BlockSaveWrapper,
|
|
|
|
formatUrlValues
|
|
|
|
} from './';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
buttonText: PropTypes.string,
|
|
|
|
enableSubmit: PropTypes.bool,
|
2020-09-09 17:12:48 +01:00
|
|
|
formFields: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({ name: PropTypes.string, label: PropTypes.string })
|
|
|
|
.isRequired
|
|
|
|
).isRequired,
|
2018-04-13 15:33:03 +01:00
|
|
|
hideButton: PropTypes.bool,
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
initialValues: PropTypes.object,
|
|
|
|
options: PropTypes.shape({
|
|
|
|
ignored: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
required: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
types: PropTypes.objectOf(PropTypes.string)
|
|
|
|
}),
|
|
|
|
submit: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
2019-09-04 15:48:58 +03:00
|
|
|
function DynamicForm({
|
2018-04-13 15:33:03 +01:00
|
|
|
id,
|
2019-09-04 15:48:58 +03:00
|
|
|
formFields,
|
|
|
|
initialValues,
|
2018-04-13 15:33:03 +01:00
|
|
|
options,
|
2019-09-04 15:48:58 +03:00
|
|
|
submit,
|
|
|
|
buttonText,
|
|
|
|
enableSubmit,
|
|
|
|
hideButton
|
2018-04-13 15:33:03 +01:00
|
|
|
}) {
|
|
|
|
return (
|
2019-09-04 15:48:58 +03:00
|
|
|
<Form
|
|
|
|
initialValues={initialValues}
|
|
|
|
onSubmit={(values, ...args) =>
|
2019-06-17 23:40:47 +03:00
|
|
|
submit(formatUrlValues(values, options), ...args)
|
2019-09-04 15:48:58 +03:00
|
|
|
}
|
2019-02-19 01:59:12 +03:00
|
|
|
>
|
2019-09-04 15:48:58 +03:00
|
|
|
{({ handleSubmit, pristine, error }) => (
|
|
|
|
<form
|
|
|
|
id={`dynamic-${id}`}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
style={{ width: '100%' }}
|
|
|
|
>
|
2020-09-09 17:12:48 +01:00
|
|
|
<FormFields formFields={formFields} options={options} />
|
2019-09-04 15:48:58 +03:00
|
|
|
<BlockSaveWrapper>
|
|
|
|
{hideButton ? null : (
|
|
|
|
<BlockSaveButton disabled={(pristine && !enableSubmit) || error}>
|
|
|
|
{buttonText ? buttonText : null}
|
|
|
|
</BlockSaveButton>
|
|
|
|
)}
|
|
|
|
</BlockSaveWrapper>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</Form>
|
2018-04-13 15:33:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicForm.displayName = 'DynamicForm';
|
|
|
|
DynamicForm.propTypes = propTypes;
|
|
|
|
|
2019-09-04 15:48:58 +03:00
|
|
|
export default DynamicForm;
|