162 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-07-08 15:40:01 +02:00
import React, { Fragment, useCallback, useRef } from 'react';
2019-07-05 14:59:31 +02:00
import PropTypes from 'prop-types';
2019-07-05 16:27:04 +02:00
import { get } from 'lodash';
2019-07-08 15:40:01 +02:00
import { DropTarget } from 'react-dnd';
2019-07-05 16:27:04 +02:00
import { InputsIndex as Input } from 'strapi-helper-plugin';
2019-07-05 14:59:31 +02:00
import pluginId from '../../pluginId';
2019-07-05 16:27:04 +02:00
import FormWrapper from '../../components/SettingFormWrapper';
2019-07-08 15:40:01 +02:00
import { Wrapper } from './components';
2019-07-05 15:45:16 +02:00
import Add from './Add';
2019-07-05 14:59:31 +02:00
import ListField from './ListField';
2019-07-08 15:40:01 +02:00
import ItemTypes from './itemsTypes';
2019-07-05 15:45:16 +02:00
function ListLayout({
addField,
availableData,
2019-07-08 15:40:01 +02:00
connectDropTarget,
2019-07-05 15:45:16 +02:00
displayedData,
fieldToEditIndex,
2019-07-05 16:27:04 +02:00
modifiedData,
2019-07-08 15:40:01 +02:00
moveListField,
2019-07-05 16:27:04 +02:00
onChange,
2019-07-05 15:45:16 +02:00
onClick,
onRemove,
}) {
2019-07-08 15:40:01 +02:00
const ref = useRef(null);
2019-07-05 14:59:31 +02:00
const handleRemove = index => {
if (displayedData.length > 1) {
onRemove(index);
return;
}
strapi.notification.info(`${pluginId}.notification.info.minimumFields`);
};
2019-07-08 15:40:01 +02:00
2019-07-05 16:27:04 +02:00
const fieldName = displayedData[fieldToEditIndex];
const fieldPath = ['metadata', fieldName, 'list'];
const form = [
{
label: { id: 'content-manager.form.Input.label' },
customBootstrapClass: 'col-md-7',
didCheckErrors: false,
errors: [],
inputDescription: {
id: 'content-manager.form.Input.label.inputDescription',
},
name: `${fieldPath.join('.')}.label`,
type: 'string',
validations: {},
},
{
label: { id: 'content-manager.form.Input.sort.field' },
customBootstrapClass: 'col-md-6',
didCheckErrors: false,
errors: [],
name: `${fieldPath.join('.')}.sortable`,
style: { marginTop: '18px' },
type: 'toggle',
validations: {},
},
];
2019-07-08 15:40:01 +02:00
const findField = useCallback(
id => {
const field = displayedData.filter(current => current === id)[0];
return {
field,
index: displayedData.indexOf(field),
};
},
[displayedData]
);
const move = useCallback(
(id, atIndex) => {
const { index } = findField(id);
moveListField(index, atIndex);
},
[displayedData]
);
connectDropTarget(ref);
2019-07-05 14:59:31 +02:00
return (
<>
2019-07-08 15:40:01 +02:00
<div className="col-lg-5 col-md-12" ref={ref}>
2019-07-05 14:59:31 +02:00
{displayedData.map((data, index) => (
2019-07-08 15:40:01 +02:00
<Fragment key={data}>
<Wrapper style={{ display: 'flex' }}>
<div>{index + 1}.</div>
<ListField
findField={findField}
index={index}
isSelected={fieldToEditIndex === index}
move={move}
name={data}
label={get(
modifiedData,
['metadata', data, 'list', 'label'],
''
)}
onClick={onClick}
onRemove={handleRemove}
/>
</Wrapper>
<div style={{ marginBottom: '6px' }}></div>
</Fragment>
2019-07-05 14:59:31 +02:00
))}
2019-07-05 15:45:16 +02:00
<Add data={availableData} onClick={addField} />
2019-07-05 14:59:31 +02:00
</div>
2019-07-05 16:27:04 +02:00
<div className="col-lg-7 col-md-12">
<FormWrapper>
{form.map(input => {
return (
<Input
key={input.name}
{...input}
onChange={onChange}
value={get(modifiedData, input.name)}
/>
);
})}
</FormWrapper>
</div>
2019-07-05 14:59:31 +02:00
</>
);
}
ListLayout.defaultProps = {
2019-07-05 15:45:16 +02:00
addField: () => {},
availableData: [],
2019-07-05 14:59:31 +02:00
displayedData: [],
2019-07-05 15:45:16 +02:00
fieldToEditIndex: 0,
2019-07-05 16:27:04 +02:00
modifiedData: {},
onChange: () => {},
2019-07-05 15:45:16 +02:00
onClick: () => {},
2019-07-05 14:59:31 +02:00
onRemove: () => {},
};
ListLayout.propTypes = {
2019-07-05 15:45:16 +02:00
addField: PropTypes.func,
availableData: PropTypes.array,
2019-07-08 15:40:01 +02:00
connectDropTarget: PropTypes.func.isRequired,
2019-07-05 14:59:31 +02:00
displayedData: PropTypes.array,
2019-07-05 15:45:16 +02:00
fieldToEditIndex: PropTypes.number,
2019-07-05 16:27:04 +02:00
modifiedData: PropTypes.object,
2019-07-08 15:40:01 +02:00
moveListField: PropTypes.func.isRequired,
2019-07-05 16:27:04 +02:00
onChange: PropTypes.func,
2019-07-05 15:45:16 +02:00
onClick: PropTypes.func,
2019-07-05 14:59:31 +02:00
onRemove: PropTypes.func,
};
2019-07-08 15:40:01 +02:00
export default DropTarget(ItemTypes.FIELD, {}, connect => ({
connectDropTarget: connect.dropTarget(),
}))(ListLayout);