2019-07-25 09:36:47 +02:00
|
|
|
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';
|
2020-03-05 14:43:00 +01:00
|
|
|
import { useStrapi } from 'strapi-helper-plugin';
|
2019-07-12 15:39:18 +02:00
|
|
|
|
2019-10-30 15:32:29 +01:00
|
|
|
import useDataManager from '../../hooks/useDataManager';
|
2020-03-05 16:21:36 +01:00
|
|
|
import InputJSONWithErrors from '../InputJSONWithErrors';
|
2019-11-04 17:11:45 +01:00
|
|
|
import SelectWrapper from '../SelectWrapper';
|
2019-07-17 17:39:43 +02:00
|
|
|
import WysiwygWithErrors from '../WysiwygWithErrors';
|
2020-02-21 11:03:39 +01:00
|
|
|
import InputUID from '../InputUID';
|
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';
|
2020-04-28 17:36:59 +02:00
|
|
|
case 'biginteger':
|
|
|
|
return 'text';
|
2019-07-12 15:39:18 +02:00
|
|
|
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';
|
2020-02-26 17:19:53 +01:00
|
|
|
case 'uid':
|
|
|
|
return 'uid';
|
2019-07-12 15:39:18 +02:00
|
|
|
default:
|
2020-04-17 12:01:55 +02:00
|
|
|
return type || 'text';
|
2019-07-12 15:39:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-04 16:52:28 +01:00
|
|
|
function Inputs({ autoFocus, keys, layout, name, onBlur }) {
|
2020-03-05 14:43:00 +01:00
|
|
|
const {
|
|
|
|
strapi: { fieldApi },
|
|
|
|
} = useStrapi();
|
|
|
|
|
2020-02-21 11:03:39 +01:00
|
|
|
const { didCheckErrors, formErrors, modifiedData, onChange } = useDataManager();
|
|
|
|
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]);
|
2020-04-07 10:41:46 +02:00
|
|
|
const regexpString = useMemo(() => get(attribute, 'regex', null), [attribute]);
|
2020-04-14 11:09:20 +02:00
|
|
|
const value = get(modifiedData, keys, null);
|
|
|
|
const temporaryErrorIdUntilBuffetjsSupportsFormattedMessage = 'app.utils.defaultMessage';
|
|
|
|
const errorId = get(
|
|
|
|
formErrors,
|
|
|
|
[keys, 'id'],
|
|
|
|
temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
|
|
|
|
);
|
|
|
|
|
|
|
|
let validationsToOmit = [
|
2019-10-30 15:32:29 +01:00
|
|
|
'type',
|
|
|
|
'model',
|
|
|
|
'via',
|
|
|
|
'collection',
|
|
|
|
'default',
|
|
|
|
'plugin',
|
|
|
|
'enum',
|
2020-04-07 10:41:46 +02:00
|
|
|
'regex',
|
2020-04-14 11:09:20 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const validations = omit(attribute, validationsToOmit);
|
2020-04-07 10:41:46 +02:00
|
|
|
|
|
|
|
if (regexpString) {
|
|
|
|
const regexp = new RegExp(regexpString);
|
|
|
|
|
|
|
|
if (regexp) {
|
|
|
|
validations.regex = regexp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:16:44 +02:00
|
|
|
const { description, visible } = metadatas;
|
2019-07-15 14:41:43 +02:00
|
|
|
|
|
|
|
if (visible === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-04-14 11:09:20 +02:00
|
|
|
|
2020-02-21 11:03:39 +01:00
|
|
|
const isRequired = get(validations, ['required'], false);
|
2019-10-29 19:26:42 +01:00
|
|
|
|
2019-11-04 17:11:45 +01:00
|
|
|
if (type === 'relation') {
|
|
|
|
return (
|
2020-02-28 02:21:21 +08:00
|
|
|
<div key={keys}>
|
2019-11-04 17:11:45 +01:00
|
|
|
<SelectWrapper
|
|
|
|
{...metadatas}
|
|
|
|
name={keys}
|
|
|
|
plugin={attribute.plugin}
|
|
|
|
relationType={attribute.relationType}
|
|
|
|
targetModel={attribute.targetModel}
|
|
|
|
value={get(modifiedData, keys)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:52:12 +01:00
|
|
|
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') {
|
2019-12-18 17:03:35 +01:00
|
|
|
step = 'any';
|
2020-02-07 15:07:44 +01:00
|
|
|
} else if (type === 'time' || type === 'datetime') {
|
|
|
|
step = 30;
|
2019-12-18 15:39:08 +01:00
|
|
|
} else {
|
2019-12-18 17:03:35 +01:00
|
|
|
step = '1';
|
2019-12-18 15:39:08 +01:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:43:07 +01:00
|
|
|
const options = get(attribute, 'enum', []).map(v => {
|
|
|
|
return (
|
|
|
|
<option key={v} value={v}>
|
|
|
|
{v}
|
|
|
|
</option>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const enumOptions = [
|
2020-02-21 11:03:39 +01:00
|
|
|
<FormattedMessage id="components.InputSelect.option.placeholder" key="__enum_option_null">
|
2019-12-19 15:43:07 +01:00
|
|
|
{msg => (
|
|
|
|
<option disabled={isRequired} hidden={isRequired} value="">
|
|
|
|
{msg}
|
|
|
|
</option>
|
|
|
|
)}
|
|
|
|
</FormattedMessage>,
|
|
|
|
...options,
|
|
|
|
];
|
|
|
|
|
2019-07-12 15:39:18 +02:00
|
|
|
return (
|
2019-10-30 18:46:19 +01:00
|
|
|
<FormattedMessage id={errorId}>
|
|
|
|
{error => {
|
|
|
|
return (
|
|
|
|
<InputsIndex
|
|
|
|
{...metadatas}
|
2020-02-27 11:20:04 +01:00
|
|
|
autoComplete="new-password"
|
2019-10-30 18:46:19 +01:00
|
|
|
autoFocus={autoFocus}
|
|
|
|
didCheckErrors={didCheckErrors}
|
|
|
|
disabled={disabled}
|
|
|
|
error={
|
2020-02-21 11:03:39 +01:00
|
|
|
isEmpty(error) || errorId === temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
|
2019-10-30 18:46:19 +01:00
|
|
|
? null
|
|
|
|
: error
|
|
|
|
}
|
|
|
|
inputDescription={description}
|
|
|
|
description={description}
|
2020-02-26 17:19:53 +01:00
|
|
|
contentTypeUID={layout.uid}
|
2019-10-30 18:46:19 +01:00
|
|
|
customInputs={{
|
2020-03-05 16:21:36 +01:00
|
|
|
json: InputJSONWithErrors,
|
2019-10-30 18:46:19 +01:00
|
|
|
wysiwyg: WysiwygWithErrors,
|
2020-02-26 17:19:53 +01:00
|
|
|
uid: InputUID,
|
2020-03-05 14:43:00 +01:00
|
|
|
...fieldApi.getFields(),
|
2019-10-30 18:46:19 +01:00
|
|
|
}}
|
|
|
|
multiple={get(attribute, 'multiple', false)}
|
2020-02-26 17:19:53 +01:00
|
|
|
attribute={attribute}
|
2019-11-04 16:52:28 +01:00
|
|
|
name={keys}
|
2019-10-30 18:46:19 +01:00
|
|
|
onBlur={onBlur}
|
|
|
|
onChange={onChange}
|
2019-12-19 15:43:07 +01:00
|
|
|
options={enumOptions}
|
2019-12-18 15:39:08 +01:00
|
|
|
step={step}
|
2019-10-30 18:46:19 +01:00
|
|
|
type={getInputType(type)}
|
|
|
|
validations={validations}
|
2019-11-07 16:52:12 +01:00
|
|
|
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,
|
2019-07-15 14:41:43 +02:00
|
|
|
keys: PropTypes.string.isRequired,
|
|
|
|
layout: PropTypes.object.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
2019-08-21 18:05:11 +02:00
|
|
|
onBlur: PropTypes.func,
|
2019-07-15 14:41:43 +02:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2019-07-12 15:39:18 +02:00
|
|
|
};
|
|
|
|
|
2019-07-17 17:39:43 +02:00
|
|
|
export default memo(Inputs);
|