118 lines
2.9 KiB
JavaScript
Raw Normal View History

import React, { memo, useMemo } from 'react';
2019-07-12 15:39:18 +02:00
import PropTypes from 'prop-types';
import { get, omit } from 'lodash';
2019-07-12 15:39:18 +02:00
import { InputsIndex } from 'strapi-helper-plugin';
2019-07-18 16:53:12 +02:00
import { useEditView } from '../../contexts/EditView';
2019-07-17 17:39:43 +02:00
import InputJSONWithErrors from '../InputJSONWithErrors';
import WysiwygWithErrors from '../WysiwygWithErrors';
2019-07-12 15:39:18 +02:00
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';
}
};
function Inputs({ autoFocus, keys, layout, modifiedData, name, onChange }) {
2019-07-18 16:53:12 +02:00
const { didCheckErrors, errors } = useEditView();
const attribute = useMemo(
() => get(layout, ['schema', 'attributes', name], {}),
[layout, name]
);
const { model, collection } = attribute;
const isMedia = useMemo(() => {
return (
get(attribute, 'plugin', '') === 'upload' &&
(model || collection) === 'file'
);
}, [attribute, collection, model]);
const multiple = collection == 'file';
const metadatas = useMemo(
() => get(layout, ['metadatas', name, 'edit'], {}),
[layout, name]
);
const disabled = useMemo(() => !get(metadatas, 'editable', true), [
metadatas,
]);
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',
]);
2019-07-24 10:16:44 +02:00
const { description, visible } = metadatas;
const value = get(modifiedData, keys);
if (visible === false) {
return null;
}
2019-07-18 16:53:12 +02:00
const inputErrors = get(errors, keys, []);
2019-07-12 15:39:18 +02:00
return (
<InputsIndex
2019-07-24 10:16:44 +02:00
{...metadatas}
2019-07-17 12:06:19 +02:00
autoFocus={autoFocus}
2019-07-18 16:53:12 +02:00
didCheckErrors={didCheckErrors}
disabled={disabled}
2019-07-18 16:53:12 +02:00
errors={inputErrors}
inputDescription={description}
inputStyle={inputStyle}
2019-07-12 15:39:18 +02:00
customInputs={{ json: InputJSONWithErrors, wysiwyg: WysiwygWithErrors }}
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,
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
};
2019-07-17 17:39:43 +02:00
export default memo(Inputs);