185 lines
5.0 KiB
JavaScript
Raw Normal View History

/**
*
* Edit
*
*/
import React from 'react';
2018-02-21 17:14:35 +01:00
import PropTypes from 'prop-types';
import {
findIndex,
2018-02-21 17:14:35 +01:00
get,
2018-02-26 16:04:59 +01:00
has,
2018-02-21 17:14:35 +01:00
isEmpty,
isFunction,
upperFirst,
2018-02-21 17:14:35 +01:00
} from 'lodash';
// You can find these components in either
// ./node_modules/strapi-helper-plugin/lib/src
// or strapi/packages/strapi-helper-plugin/lib/src
import Input from 'components/InputsIndex';
import InputJSONWithErrors from 'components/InputJSONWithErrors';
import WysiwygWithErrors from 'components/WysiwygWithErrors';
import styles from './styles.scss';
2018-02-21 17:14:35 +01:00
const getInputType = (type = '') => {
switch (type.toLowerCase()) {
case 'boolean':
return 'checkbox';
case 'bigint':
case 'decimal':
case 'float':
case 'integer':
return 'number';
case 'date':
case 'datetime':
return 'date';
case 'email':
return 'email';
case 'enumeration':
return 'select';
case 'password':
return 'password';
case 'string':
return 'text';
case 'text':
return 'textarea';
case 'file':
case 'files':
return 'file';
2018-06-15 16:48:29 +02:00
case 'json':
return 'json';
2018-02-21 17:14:35 +01:00
default:
return 'text';
}
};
class Edit extends React.PureComponent {
getInputErrors = (attr) => {
const index = findIndex(this.props.formErrors, ['name', attr]);
return index !== -1 ? this.props.formErrors[index].errors : [];
}
2018-02-21 17:14:35 +01:00
/**
* Retrieve the Input layout
* @param {String} attr [description]
* @return {Object} Object containing the Input's label customBootstrapClass, ...
*/
2018-07-23 17:46:05 +02:00
getInputLayout = (attr) => {
const { layout } = this.props;
return Object.keys(get(layout, ['attributes', attr], {})).reduce((acc, current) => {
acc[current] = isFunction(layout.attributes[attr][current]) ?
layout.attributes[attr][current](this) :
layout.attributes[attr][current];
2018-02-21 17:14:35 +01:00
return acc;
2018-07-23 17:46:05 +02:00
}, {});
};
2018-02-21 17:14:35 +01:00
/**
* Retrieve the input's validations
* @param {String} attr
* @return {Object}
*/
getInputValidations = (attr) => {
const { formValidations } = this.props;
const index = findIndex(formValidations, ['name', attr]);
return get(formValidations, [index, 'validations'], {});
}
/**
* Retrieve all relations made with the upload plugin
* @param {Object} props
* @return {Object}
*/
getUploadRelations = (props) => (
Object.keys(get(props.schema, 'relations', {})).reduce((acc, current) => {
if (get(props.schema, ['relations', current, 'plugin']) === 'upload') {
acc[current] = {
description: '',
label: upperFirst(current),
type: 'file',
};
}
return acc;
}, {})
)
2018-02-26 16:04:59 +01:00
fileRelationAllowMultipleUpload = (relationName) => has(this.props.schema, ['relations', relationName, 'collection']);
2018-07-23 17:46:05 +02:00
orderAttributes = () => get(this.props.schema, ['editDisplay', 'fields'], []);
2018-02-21 17:14:35 +01:00
render(){
return (
<div className={styles.form}>
<div className="row">
2018-07-23 17:46:05 +02:00
{this.orderAttributes().map((attr, key) => {
const details = get(this.props.schema, ['editDisplay', 'availableFields', attr]);
2018-02-21 17:14:35 +01:00
// Retrieve the input's bootstrapClass from the layout
const layout = this.getInputLayout(attr);
2018-03-19 12:30:04 +01:00
const appearance = get(layout, 'appearance');
const type = !isEmpty(appearance) ? appearance.toLowerCase() : get(layout, 'type', getInputType(details.type));
const inputDescription = get(details, 'description', null);
2018-02-21 17:14:35 +01:00
2018-06-15 16:48:29 +02:00
return (
2018-02-21 17:14:35 +01:00
<Input
autoFocus={key === 0}
customBootstrapClass={get(layout, 'className')}
customInputs={{ json: InputJSONWithErrors, wysiwyg: WysiwygWithErrors }}
2018-02-21 17:14:35 +01:00
didCheckErrors={this.props.didCheckErrors}
errors={this.getInputErrors(attr)}
inputDescription={inputDescription}
2018-02-21 17:14:35 +01:00
key={attr}
label={get(layout, 'label') || details.label || ''}
2018-02-26 16:04:59 +01:00
multiple={this.fileRelationAllowMultipleUpload(attr)}
2018-02-21 17:14:35 +01:00
name={attr}
2018-03-26 15:25:32 +02:00
onBlur={this.props.onBlur}
2018-02-21 17:14:35 +01:00
onChange={this.props.onChange}
2018-07-23 17:46:05 +02:00
placeholder={get(layout, 'placeholder') || details.placeholder || ''}
2018-04-09 15:52:58 +02:00
resetProps={this.props.resetProps}
selectOptions={get(this.props.attributes, [attr, 'enum'])}
2018-03-19 12:30:04 +01:00
type={type}
validations={this.getInputValidations(attr)}
2018-02-21 17:14:35 +01:00
value={this.props.record[attr]}
/>
);
})}
</div>
</div>
2018-02-21 17:14:35 +01:00
);
}
}
2018-02-21 17:14:35 +01:00
Edit.defaultProps = {
attributes: {},
formErrors: [],
formValidations: [],
2018-02-21 17:14:35 +01:00
layout: {},
2018-03-26 15:25:32 +02:00
onBlur: () => {},
2018-02-21 17:14:35 +01:00
onChange: () => {},
record: {},
2018-04-09 15:52:58 +02:00
resetProps: false,
2018-02-26 16:04:59 +01:00
schema: {},
2018-02-21 17:14:35 +01:00
};
Edit.propTypes = {
attributes: PropTypes.object,
didCheckErrors: PropTypes.bool.isRequired,
formErrors: PropTypes.array,
formValidations: PropTypes.array,
2018-02-21 17:14:35 +01:00
layout: PropTypes.object,
2018-03-26 15:25:32 +02:00
onBlur: PropTypes.func,
2018-02-21 17:14:35 +01:00
onChange: PropTypes.func,
record: PropTypes.object,
2018-04-09 15:52:58 +02:00
resetProps: PropTypes.bool,
2018-02-26 16:04:59 +01:00
schema: PropTypes.object,
2018-02-21 17:14:35 +01:00
};
export default Edit;