134 lines
3.3 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';
2019-10-29 19:26:42 +01:00
import {
get,
// omit,
} from 'lodash';
// import { InputsIndex } from 'strapi-helper-plugin';
import { InputFileWithErrors } from 'strapi-helper-plugin';
import { Inputs as InputsIndex } from '@buffetjs/custom';
2019-07-12 15:39:18 +02:00
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':
2019-10-29 19:26:42 +01:00
return 'bool';
2019-07-12 15:39:18 +02:00
case 'biginteger':
case 'decimal':
case 'float':
case 'integer':
return 'number';
case 'date':
case 'datetime':
2019-10-29 19:26:42 +01:00
case 'time':
return type;
2019-07-12 15:39:18 +02:00
case 'email':
return 'email';
case 'enumeration':
return 'select';
case 'password':
return 'password';
case 'string':
return 'text';
case 'text':
return 'textarea';
2019-07-25 13:44:35 +02:00
case 'media':
2019-07-12 15:39:18 +02:00
case 'file':
case 'files':
2019-10-29 19:26:42 +01:00
return 'media';
2019-07-12 15:39:18 +02:00
case 'json':
return 'json';
2019-07-25 13:44:35 +02:00
case 'wysiwyg':
case 'WYSIWYG':
2019-07-31 11:45:08 +02:00
case 'richtext':
2019-07-25 13:44:35 +02:00
return 'wysiwyg';
2019-07-12 15:39:18 +02:00
default:
return 'text';
}
};
2019-10-29 19:26:42 +01:00
function Inputs({ autoFocus, keys, name, onBlur, onChange }) {
const { didCheckErrors, errors, layout, modifiedData } = useEditView();
console.log({ errors });
const attribute = useMemo(
() => get(layout, ['schema', 'attributes', name], {}),
[layout, name]
);
const metadatas = useMemo(
() => get(layout, ['metadatas', name, 'edit'], {}),
[layout, name]
);
const disabled = useMemo(() => !get(metadatas, 'editable', true), [
metadatas,
]);
2019-07-25 13:44:35 +02:00
const type = useMemo(() => get(attribute, 'type', null), [attribute]);
2019-10-29 19:26:42 +01:00
// 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-10-29 19:26:42 +01:00
// const inputErrors = get(errors, keys, []);
// const withOptionPlaceholder = get(attribute, 'type', '') === 'enumeration';
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-10-29 19:26:42 +01:00
// errors={errors}
// errors={inputErrors}
// inputDescription={description}
description={description}
// inputStyle={inputStyle}
2019-07-25 13:44:35 +02:00
customInputs={{
2019-10-29 19:26:42 +01:00
media: InputFileWithErrors,
2019-07-25 13:44:35 +02:00
json: InputJSONWithErrors,
wysiwyg: WysiwygWithErrors,
}}
multiple={get(attribute, 'multiple', false)}
name={name}
2019-08-21 18:05:11 +02:00
onBlur={onBlur}
onChange={onChange}
2019-10-29 19:26:42 +01:00
options={get(attribute, 'enum', [])}
type={getInputType(type)}
2019-10-29 19:26:42 +01:00
// validations={null}
// validations={validations}
value={value}
2019-10-29 19:26:42 +01:00
// withOptionPlaceholder={withOptionPlaceholder}
2019-07-12 15:39:18 +02:00
/>
);
}
2019-07-17 12:06:19 +02:00
Inputs.defaultProps = {
autoFocus: false,
2019-08-21 18:05:11 +02:00
onBlur: null,
2019-07-17 12:06:19 +02:00
};
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,
name: PropTypes.string.isRequired,
2019-08-21 18:05:11 +02:00
onBlur: PropTypes.func,
onChange: PropTypes.func.isRequired,
2019-07-12 15:39:18 +02:00
};
2019-07-17 17:39:43 +02:00
export default memo(Inputs);