diff --git a/packages/core/upload/admin/src/components/TableList/CellContent.js b/packages/core/upload/admin/src/components/TableList/CellContent.js new file mode 100644 index 0000000000..4d82f9e20f --- /dev/null +++ b/packages/core/upload/admin/src/components/TableList/CellContent.js @@ -0,0 +1,72 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { useIntl } from 'react-intl'; +import { getFileExtension } from '@strapi/helper-plugin'; +import { Typography } from '@strapi/design-system/Typography'; + +import { PreviewCell } from './PreviewCell'; +import { formatBytes } from '../../utils'; + +export const CellContent = ({ + alternativeText, + content, + cellType, + elementType, + mime, + fileExtension, + thumbnailURL, + url, +}) => { + const { formatDate } = useIntl(); + switch (cellType) { + case 'image': + return ( + + ); + case 'date': + return {formatDate(content)}; + + case 'size': + if (elementType === 'folder') return -; + + return {formatBytes(content)}; + + case 'ext': + if (elementType === 'folder') return -; + + return {getFileExtension(content).toUpperCase()}; + + case 'text': + return {content}; + + default: + return -; + } +}; + +CellContent.defaultProps = { + alternativeText: null, + content: '', + fileExtension: '', + mime: '', + thumbnailURL: null, + url: null, +}; + +CellContent.propTypes = { + alternativeText: PropTypes.string, + content: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + fileExtension: PropTypes.string, + mime: PropTypes.string, + thumbnailURL: PropTypes.string, + cellType: PropTypes.string.isRequired, + elementType: PropTypes.string.isRequired, + url: PropTypes.string, +}; diff --git a/packages/core/upload/admin/src/components/TableList/TableRows.js b/packages/core/upload/admin/src/components/TableList/TableRows.js index 9340e8f2ba..540311cc3b 100644 --- a/packages/core/upload/admin/src/components/TableList/TableRows.js +++ b/packages/core/upload/admin/src/components/TableList/TableRows.js @@ -7,30 +7,17 @@ import { IconButton } from '@strapi/design-system/IconButton'; import { Tbody, Td, Tr } from '@strapi/design-system/Table'; import Pencil from '@strapi/icons/Pencil'; -import { PreviewCell } from './PreviewCell'; -import { TextCell } from './TextCell'; -import { AssetDefinition, FolderDefinition } from '../../constants'; -import { formatBytes, getTrad } from '../../utils'; +import { CellContent } from './CellContent'; +import { AssetDefinition, FolderDefinition, tableHeaders as cells } from '../../constants'; +import { getTrad } from '../../utils'; export const TableRows = ({ onEditAsset, onEditFolder, onSelectOne, rows, selected }) => { - const { formatDate, formatMessage } = useIntl(); + const { formatMessage } = useIntl(); return ( {rows.map((element) => { - const { - alternativeText, - id, - name, - ext, - size, - createdAt, - updatedAt, - url, - mime, - formats, - type, - } = element; + const { alternativeText, id, name, ext, url, mime, formats, type: elementType } = element; const isSelected = !!selected.find((currentRow) => currentRow.id === id); @@ -40,49 +27,43 @@ export const TableRows = ({ onEditAsset, onEditFolder, onSelectOne, rows, select onSelectOne({ ...element, type })} + onValueChange={() => onSelectOne({ ...element, elementType })} checked={isSelected} /> - - - - - - - - - - - - - - - - - - - {((type === 'asset' && onEditAsset) || (type === 'folder' && onEditFolder)) && ( + {cells.map(({ name, type: cellType }) => { + return ( + + + + ); + })} + {((elementType === 'asset' && onEditAsset) || + (elementType === 'folder' && onEditFolder)) && ( (type === 'asset' ? onEditAsset(element) : onEditFolder(element))} + onClick={() => + elementType === 'asset' ? onEditAsset(element) : onEditFolder(element) + } noBorder > diff --git a/packages/core/upload/admin/src/components/TableList/TextCell.js b/packages/core/upload/admin/src/components/TableList/TextCell.js deleted file mode 100644 index 1e48eebf6a..0000000000 --- a/packages/core/upload/admin/src/components/TableList/TextCell.js +++ /dev/null @@ -1,19 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import { Typography } from '@strapi/design-system/Typography'; - -export const TextCell = ({ content }) => { - if (content) { - return {content}; - } - - return -; -}; - -TextCell.defaultProps = { - content: '', -}; - -TextCell.propTypes = { - content: PropTypes.string, -}; diff --git a/packages/core/upload/admin/src/components/TableList/tests/CellContent.test.js b/packages/core/upload/admin/src/components/TableList/tests/CellContent.test.js new file mode 100644 index 0000000000..e077938888 --- /dev/null +++ b/packages/core/upload/admin/src/components/TableList/tests/CellContent.test.js @@ -0,0 +1,78 @@ +import React from 'react'; +import { IntlProvider } from 'react-intl'; +import { render } from '@testing-library/react'; +import { ThemeProvider, lightTheme } from '@strapi/design-system'; + +import { CellContent } from '../CellContent'; + +const PROPS_FIXTURE = { + alternativeText: 'alternative alt', + cellType: 'image', + elementType: 'asset', + content: 'michka-picture-url-default.jpeg', + fileExtension: '.jpeg', + mime: 'image/jpeg', + thumbnailURL: 'michka-picture-url-thumbnail.jpeg', + url: 'michka-picture-url-default.jpeg', +}; + +const ComponentFixture = (props) => { + const customProps = { + ...PROPS_FIXTURE, + ...props, + }; + + return ( + + + + + + ); +}; + +const setup = (props) => render(); + +describe('TableList | CellContent', () => { + it('should render image cell type when element type is asset and mime includes image', () => { + const { getByRole } = setup(); + + expect(getByRole('img', { name: 'alternative alt' })).toBeInTheDocument(); + }); + + it('should render image cell type when element type is asset and mime does not include image', () => { + const { getByText } = setup({ mime: 'application/pdf', fileExtension: 'pdf' }); + + expect(getByText('pdf')).toBeInTheDocument(); + }); + + it('should render image cell type when element type is folder', () => { + const { container } = setup({ elementType: 'folder' }); + + expect(container.querySelector('path')).toBeInTheDocument(); + }); + + it('should render text cell type', () => { + const { getByText } = setup({ cellType: 'text', content: 'some text' }); + + expect(getByText('some text')).toBeInTheDocument(); + }); + + it('should render extension cell type', () => { + const { getByText } = setup({ cellType: 'ext', content: '.pdf' }); + + expect(getByText('PDF')).toBeInTheDocument(); + }); + + it('should render size cell type', () => { + const { getByText } = setup({ cellType: 'size', content: '20.5435' }); + + expect(getByText('21KB')).toBeInTheDocument(); + }); + + it('should render date cell type', () => { + const { getByText } = setup({ cellType: 'date', content: '2022-11-18T12:08:02.202Z' }); + + expect(getByText('11/18/2022')).toBeInTheDocument(); + }); +}); diff --git a/packages/core/upload/admin/src/components/TableList/tests/TextCell.test.js b/packages/core/upload/admin/src/components/TableList/tests/TextCell.test.js deleted file mode 100644 index 77a524516f..0000000000 --- a/packages/core/upload/admin/src/components/TableList/tests/TextCell.test.js +++ /dev/null @@ -1,27 +0,0 @@ -import React from 'react'; -import { render } from '@testing-library/react'; -import { ThemeProvider, lightTheme } from '@strapi/design-system'; - -import { TextCell } from '../TextCell'; - -const ComponentFixture = (props) => ( - - - -); - -const setup = (props) => render(); - -describe('TableList | TextCell', () => { - it('should render content', () => { - const { getByText } = setup({ content: 'michka' }); - - expect(getByText('michka')).toBeInTheDocument(); - }); - - it('should render a default content', () => { - const { getByText } = setup(); - - expect(getByText('-')).toBeInTheDocument(); - }); -}); diff --git a/packages/core/upload/admin/src/constants.js b/packages/core/upload/admin/src/constants.js index 819c90edc5..009e0a4640 100644 --- a/packages/core/upload/admin/src/constants.js +++ b/packages/core/upload/admin/src/constants.js @@ -105,6 +105,7 @@ export const tableHeaders = [ label: { id: getTrad('list-table-header-preview'), defaultMessage: 'preview' }, sortable: false, }, + type: 'image', }, { name: 'name', @@ -113,6 +114,7 @@ export const tableHeaders = [ label: { id: getTrad('list-table-header-name'), defaultMessage: 'name' }, sortable: true, }, + type: 'text', }, { name: 'ext', @@ -121,6 +123,7 @@ export const tableHeaders = [ label: { id: getTrad('list-table-header-ext'), defaultMessage: 'extension' }, sortable: false, }, + type: 'ext', }, { name: 'size', @@ -129,6 +132,7 @@ export const tableHeaders = [ label: { id: getTrad('list-table-header-size'), defaultMessage: 'size' }, sortable: false, }, + type: 'size', }, { name: 'createdAt', @@ -137,6 +141,7 @@ export const tableHeaders = [ label: { id: getTrad('list-table-header-createdAt'), defaultMessage: 'created' }, sortable: true, }, + type: 'date', }, { name: 'updatedAt', @@ -145,6 +150,7 @@ export const tableHeaders = [ label: { id: getTrad('list-table-header-updatedAt'), defaultMessage: 'last update' }, sortable: true, }, + type: 'date', }, ];