179 lines
4.2 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-30 18:46:19 +01:00
import { get, isEmpty, omit, toLower } from 'lodash';
import { FormattedMessage } from 'react-intl';
2019-10-29 19:26:42 +01:00
import { Inputs as InputsIndex } from '@buffetjs/custom';
2019-07-12 15:39:18 +02:00
2019-10-30 15:32:29 +01:00
import useDataManager from '../../hooks/useDataManager';
2019-07-17 17:39:43 +02:00
import InputJSONWithErrors from '../InputJSONWithErrors';
2019-11-15 15:54:31 +01:00
import InputFileWithErrors from '../InputFileWithErrors';
2019-11-04 17:11:45 +01:00
import SelectWrapper from '../SelectWrapper';
2019-07-17 17:39:43 +02:00
import WysiwygWithErrors from '../WysiwygWithErrors';
2019-07-12 15:39:18 +02:00
const getInputType = (type = '') => {
2019-10-30 18:46:19 +01:00
switch (toLower(type)) {
2019-07-12 15:39:18 +02:00
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-11-04 16:52:28 +01:00
function Inputs({ autoFocus, keys, layout, name, onBlur }) {
2019-10-30 15:32:29 +01:00
const {
didCheckErrors,
2019-10-30 18:46:19 +01:00
formErrors,
2019-10-30 15:32:29 +01:00
modifiedData,
onChange,
} = useDataManager();
2019-10-30 18:46:19 +01:00
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
2019-10-30 15:32:29 +01:00
const validations = omit(attribute, [
'type',
'model',
'via',
'collection',
'default',
'plugin',
'enum',
]);
2019-07-24 10:16:44 +02:00
const { description, visible } = metadatas;
2019-10-30 15:32:29 +01:00
const value = get(modifiedData, keys, null);
if (visible === false) {
return null;
}
2019-10-30 18:46:19 +01:00
const temporaryErrorIdUntilBuffetjsSupportsFormattedMessage =
'app.utils.defaultMessage';
const errorId = get(
formErrors,
[keys, 'id'],
temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
);
2019-10-29 19:26:42 +01:00
2019-11-04 17:11:45 +01:00
if (type === 'relation') {
return (
<div className="col-6" key={keys}>
<SelectWrapper
{...metadatas}
name={keys}
plugin={attribute.plugin}
relationType={attribute.relationType}
targetModel={attribute.targetModel}
value={get(modifiedData, keys)}
/>
</div>
);
}
let inputValue = value;
// Fix for input file multipe
if (type === 'media' && !value) {
inputValue = [];
}
2019-12-18 15:39:08 +01:00
let step;
if (type === 'float' || type === 'decimal') {
step = 0.01;
} else {
step = 1;
}
2019-07-12 15:39:18 +02:00
return (
2019-10-30 18:46:19 +01:00
<FormattedMessage id={errorId}>
{error => {
return (
<InputsIndex
{...metadatas}
autoFocus={autoFocus}
didCheckErrors={didCheckErrors}
disabled={disabled}
error={
isEmpty(error) ||
errorId === temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
? null
: error
}
inputDescription={description}
description={description}
customInputs={{
media: InputFileWithErrors,
json: InputJSONWithErrors,
wysiwyg: WysiwygWithErrors,
}}
multiple={get(attribute, 'multiple', false)}
2019-11-04 16:52:28 +01:00
name={keys}
2019-10-30 18:46:19 +01:00
onBlur={onBlur}
onChange={onChange}
options={get(attribute, 'enum', [])}
2019-12-18 15:39:08 +01:00
step={step}
2019-10-30 18:46:19 +01:00
type={getInputType(type)}
validations={validations}
value={inputValue}
2019-11-04 17:24:48 +01:00
withDefaultValue={false}
2019-10-30 18:46:19 +01:00
/>
);
2019-07-25 13:44:35 +02:00
}}
2019-10-30 18:46:19 +01:00
</FormattedMessage>
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);