2019-07-12 15:39:18 +02:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-07-15 14:41:43 +02:00
|
|
|
import { get, omit } from 'lodash';
|
2019-07-12 15:39:18 +02:00
|
|
|
|
|
|
|
import { InputsIndex } from 'strapi-helper-plugin';
|
|
|
|
|
|
|
|
import InputJSONWithErrors from '../../components/InputJSONWithErrors';
|
|
|
|
import WysiwygWithErrors from '../../components/WysiwygWithErrors';
|
|
|
|
|
|
|
|
const getInputType = (type = '') => {
|
|
|
|
switch (type.toLowerCase()) {
|
|
|
|
case 'boolean':
|
|
|
|
return 'toggle';
|
|
|
|
case 'biginteger':
|
|
|
|
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';
|
|
|
|
case 'json':
|
|
|
|
return 'json';
|
|
|
|
default:
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-17 12:06:19 +02:00
|
|
|
function Inputs({ autoFocus, keys, layout, modifiedData, name, onChange }) {
|
2019-07-15 14:41:43 +02:00
|
|
|
const attribute = get(layout, ['schema', 'attributes', name], {});
|
|
|
|
const { model, collection } = attribute;
|
|
|
|
const isMedia =
|
|
|
|
get(attribute, 'plugin', '') === 'upload' &&
|
|
|
|
(model || collection) === 'file';
|
|
|
|
const multiple = collection == 'file';
|
|
|
|
const metadata = get(layout, ['metadata', name, 'edit'], {});
|
|
|
|
const type = isMedia ? 'file' : get(attribute, 'type', null);
|
|
|
|
const inputStyle = type === 'text' ? { height: '196px' } : {};
|
|
|
|
const validations = omit(attribute, [
|
|
|
|
'type',
|
|
|
|
'model',
|
|
|
|
'via',
|
|
|
|
'collection',
|
|
|
|
'default',
|
|
|
|
'plugin',
|
|
|
|
'enum',
|
|
|
|
]);
|
|
|
|
const { description, visible } = metadata;
|
|
|
|
const value = get(modifiedData, keys);
|
|
|
|
|
|
|
|
if (visible === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:39:18 +02:00
|
|
|
return (
|
|
|
|
<InputsIndex
|
2019-07-15 14:41:43 +02:00
|
|
|
{...metadata}
|
2019-07-17 12:06:19 +02:00
|
|
|
autoFocus={autoFocus}
|
2019-07-15 14:41:43 +02:00
|
|
|
inputDescription={description}
|
|
|
|
inputStyle={inputStyle}
|
2019-07-12 15:39:18 +02:00
|
|
|
customInputs={{ json: InputJSONWithErrors, wysiwyg: WysiwygWithErrors }}
|
2019-07-15 14:41:43 +02:00
|
|
|
multiple={multiple}
|
|
|
|
name={name}
|
|
|
|
onChange={onChange}
|
|
|
|
selectOptions={get(attribute, 'enum', [])}
|
|
|
|
type={getInputType(type)}
|
|
|
|
validations={validations}
|
|
|
|
value={value}
|
2019-07-12 15:39:18 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:06:19 +02:00
|
|
|
Inputs.defaultProps = {
|
|
|
|
autoFocus: false,
|
|
|
|
};
|
|
|
|
|
2019-07-12 15:39:18 +02:00
|
|
|
Inputs.propTypes = {
|
2019-07-17 12:06:19 +02:00
|
|
|
autoFocus: PropTypes.bool,
|
2019-07-15 14:41:43 +02:00
|
|
|
keys: PropTypes.string.isRequired,
|
|
|
|
layout: PropTypes.object.isRequired,
|
|
|
|
modifiedData: PropTypes.object.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2019-07-12 15:39:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Inputs;
|