creates a better pattern for how to render table cells

This commit is contained in:
James
2020-09-27 15:32:36 -04:00
parent 77096cb252
commit 15a488c03f
5 changed files with 71 additions and 49 deletions

View File

@@ -1,11 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import format from 'date-fns/format';
import { useConfig } from '../../../../providers/Config';
import RenderCustomComponent from '../../../../utilities/RenderCustomComponent';
import Thumbnail from '../../../../elements/Thumbnail';
import Relationship from './Relationship';
import cellComponents from './types';
const DefaultCell = (props) => {
const {
@@ -13,17 +11,10 @@ const DefaultCell = (props) => {
colIndex,
collection: {
slug,
upload: {
staticURL,
adminThumbnail,
} = {},
},
cellData,
rowData: {
id,
filename,
mimeType,
sizes,
} = {},
} = props;
@@ -38,49 +29,24 @@ const DefaultCell = (props) => {
wrapElementProps.to = `${admin}/collections/${slug}/${id}`;
}
if (field.type === 'thumbnail') {
return (
<WrapElement {...wrapElementProps}>
<Thumbnail
size="small"
{...{
mimeType, adminThumbnail, sizes, staticURL, filename,
}}
/>
</WrapElement>
);
}
const CellComponent = cellComponents[field.type];
if (field.type === 'relationship') {
if (!CellComponent) {
return (
<WrapElement {...wrapElementProps}>
<Relationship
field={field}
cellData={cellData}
/>
</WrapElement>
);
}
if (field.type === 'date' && cellData) {
return (
<WrapElement {...wrapElementProps}>
<span>
{format(new Date(cellData), 'MMMM do yyyy, h:mma')}
</span>
{typeof cellData === 'string' && cellData}
{typeof cellData === 'number' && cellData}
{typeof cellData === 'object' && JSON.stringify(cellData)}
</WrapElement>
);
}
return (
<WrapElement {...wrapElementProps}>
{field.type !== 'date' && (
<React.Fragment>
{typeof cellData === 'string' && cellData}
{typeof cellData === 'number' && cellData}
{typeof cellData === 'object' && JSON.stringify(cellData)}
</React.Fragment>
)}
<CellComponent
field={field}
data={cellData}
/>
</WrapElement>
);
};

View File

@@ -0,0 +1,32 @@
import React from 'react';
import PropTypes from 'prop-types';
const BlocksCell = ({ data, field }) => {
const selectedBlocks = data ? data.map(({ blockType }) => blockType) : [];
let label = `0 ${field.label}.`;
if (selectedBlocks.length > 0) {
label = `${selectedBlocks.length} ${field.label}`;
}
return (
<span>{label}</span>
);
};
BlocksCell.defaultProps = {
data: [],
};
BlocksCell.propTypes = {
data: PropTypes.arrayOf(
PropTypes.shape({}),
),
field: PropTypes.shape({
singularLabel: PropTypes.string,
label: PropTypes.string,
}).isRequired,
};
export default BlocksCell;

View File

@@ -0,0 +1,15 @@
import React from 'react';
import PropTypes from 'prop-types';
import format from 'date-fns/format';
const DateCell = ({ data }) => (
<span>
{format(new Date(data), 'MMMM do yyyy, h:mma')}
</span>
);
DateCell.propTypes = {
data: PropTypes.string.isRequired,
};
export default DateCell;

View File

@@ -1,9 +1,9 @@
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { useConfig } from '../../../../../providers/Config';
import { useConfig } from '../../../../../../providers/Config';
const RelationshipCell = (props) => {
const { field, cellData } = props;
const { field, data: cellData } = props;
const { relationTo } = field;
const { collections } = useConfig();
const [data, setData] = useState();
@@ -39,7 +39,7 @@ const RelationshipCell = (props) => {
}
}
}
}, [cellData, relationTo, field]);
}, [cellData, relationTo, field, collections]);
return (
<React.Fragment>
@@ -49,11 +49,11 @@ const RelationshipCell = (props) => {
};
RelationshipCell.defaultProps = {
cellData: undefined,
data: undefined,
};
RelationshipCell.propTypes = {
cellData: PropTypes.oneOfType([
data: PropTypes.oneOfType([
PropTypes.shape({}),
PropTypes.array,
PropTypes.string,

View File

@@ -0,0 +1,9 @@
import relationship from './Relationship';
import date from './Date';
import blocks from './Blocks';
export default {
relationship,
date,
blocks,
};