211 lines
5.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';
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';
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';
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';
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';
case 'uid':
return 'uid';
2019-07-12 15:39:18 +02:00
default:
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 }) {
const {
strapi: { fieldApi },
} = useStrapi();
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]);
const regexpString = useMemo(() => get(attribute, 'regex', null), [attribute]);
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',
'regex',
];
const validations = omit(attribute, validationsToOmit);
if (regexpString) {
const regexp = new RegExp(regexpString);
if (regexp) {
validations.regex = regexp;
}
}
2019-07-24 10:16:44 +02:00
const { description, visible } = metadatas;
if (visible === false) {
return null;
}
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 (
<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>
);
}
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 = 'any';
} else if (type === 'time' || type === 'datetime') {
step = 30;
2019-12-18 15:39:08 +01:00
} else {
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 = [
<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}
autoComplete="new-password"
2019-10-30 18:46:19 +01:00
autoFocus={autoFocus}
didCheckErrors={didCheckErrors}
disabled={disabled}
error={
isEmpty(error) || errorId === temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
2019-10-30 18:46:19 +01:00
? null
: error
}
inputDescription={description}
description={description}
contentTypeUID={layout.uid}
2019-10-30 18:46:19 +01:00
customInputs={{
json: InputJSONWithErrors,
2019-10-30 18:46:19 +01:00
wysiwyg: WysiwygWithErrors,
uid: InputUID,
...fieldApi.getFields(),
2019-10-30 18:46:19 +01:00
}}
multiple={get(attribute, 'multiple', false)}
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}
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);