94 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-02-13 09:01:32 +01:00
import React from 'react';
import PropTypes from 'prop-types';
import { Checkbox } from '@buffetjs/core';
import { get } from 'lodash';
import { createMatrix } from '../../utils';
import Card from '../Card';
import CardControlsWrapper from '../CardControlsWrapper';
import ListWrapper from '../ListWrapper';
2020-03-25 17:55:06 +01:00
const List = ({
clickable,
data,
onChange,
onCardClick,
selectedItems,
canSelect,
renderCardControl,
}) => {
const matrix = createMatrix(data);
const handleClick = e => {
e.stopPropagation();
};
return (
<ListWrapper>
{matrix.map(({ key, rowContent }) => {
return (
<div className="row" key={key}>
{rowContent.map(item => {
const { id } = item;
const url = get(item, ['formats', 'thumbnail', 'url'], item.url);
const checked = selectedItems.findIndex(file => file.id === id) !== -1;
const fileUrl = url.startsWith('/') ? `${strapi.backendURL}${url}` : url;
return (
<div className="col-xs-12 col-md-6 col-xl-3" key={id}>
<Card
checked={checked}
{...item}
hasIcon={clickable}
url={fileUrl}
2020-03-25 17:55:06 +01:00
onClick={onCardClick}
>
{(checked || canSelect) && (
2020-03-25 17:55:06 +01:00
<>
<CardControlsWrapper leftAlign className="card-control-wrapper">
<Checkbox
name={`${id}`}
onChange={onChange}
onClick={handleClick}
value={checked}
/>
</CardControlsWrapper>
{renderCardControl && (
<CardControlsWrapper className="card-control-wrapper">
{renderCardControl(id)}
</CardControlsWrapper>
)}
</>
)}
</Card>
</div>
);
})}
</div>
);
})}
</ListWrapper>
);
2020-02-13 09:01:32 +01:00
};
List.defaultProps = {
clickable: false,
canSelect: true,
data: [],
onChange: () => {},
2020-03-25 17:55:06 +01:00
onCardClick: () => {},
renderCardControl: null,
selectedItems: [],
2020-02-13 09:01:32 +01:00
};
List.propTypes = {
clickable: PropTypes.bool,
canSelect: PropTypes.bool,
data: PropTypes.array,
onChange: PropTypes.func,
2020-03-25 17:55:06 +01:00
onCardClick: PropTypes.func,
renderCardControl: PropTypes.func,
selectedItems: PropTypes.array,
2020-02-13 09:01:32 +01:00
};
export default List;