diff --git a/packages/edit-site/src/components/dataviews/dataviews.js b/packages/edit-site/src/components/dataviews/dataviews.js index b488836a91f3e..90240d2818e5b 100644 --- a/packages/edit-site/src/components/dataviews/dataviews.js +++ b/packages/edit-site/src/components/dataviews/dataviews.js @@ -29,7 +29,7 @@ export default function DataViews( { const _fields = useMemo( () => { return fields.map( ( field ) => ( { ...field, - render: field.render || field.getValue, + render: field.render?.( field.getValue ) || field.getValue, } ) ); }, [ fields ] ); return ( diff --git a/packages/edit-site/src/components/dataviews/field-types.js b/packages/edit-site/src/components/dataviews/field-types.js new file mode 100644 index 0000000000000..b941e00e2d7e6 --- /dev/null +++ b/packages/edit-site/src/components/dataviews/field-types.js @@ -0,0 +1,25 @@ +/** + * Internal dependencies + */ +import Media from '../../components/media'; + +export const coreFieldTypes = { + string: { + render: + ( getValue ) => + ( { item } ) => + getValue( { item } ), + }, + date: { + render: + ( getValue ) => + ( { item } ) => , + }, + image: { + render: + ( getValue ) => + ( { item } ) => ( + + ), + }, +}; diff --git a/packages/edit-site/src/components/dataviews/index.js b/packages/edit-site/src/components/dataviews/index.js index 422d128b1461d..371f1713e2d90 100644 --- a/packages/edit-site/src/components/dataviews/index.js +++ b/packages/edit-site/src/components/dataviews/index.js @@ -1 +1,2 @@ export { default as DataViews } from './dataviews'; +export { coreFieldTypes } from './field-types'; diff --git a/packages/edit-site/src/components/dataviews/view-grid.js b/packages/edit-site/src/components/dataviews/view-grid.js index 30a36b68e2c09..85e1a2b27befd 100644 --- a/packages/edit-site/src/components/dataviews/view-grid.js +++ b/packages/edit-site/src/components/dataviews/view-grid.js @@ -20,7 +20,7 @@ export function ViewGrid( { data, fields, view, actions } ) { ); const visibleFields = fields.filter( ( field ) => - ! view.hiddenFields.includes( field.id ) && + ! view.hiddenFields?.includes( field.id ) && field.id !== view.layout.mediaField ); return ( diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js index dc77439217c80..824d7e7176116 100644 --- a/packages/edit-site/src/components/page-pages/index.js +++ b/packages/edit-site/src/components/page-pages/index.js @@ -16,7 +16,7 @@ import { dateI18n, getDate, getSettings } from '@wordpress/date'; */ import Page from '../page'; import Link from '../routes/link'; -import { DataViews } from '../dataviews'; +import { DataViews, coreFieldTypes } from '../dataviews'; import useTrashPostAction from '../actions/trash-post'; import Media from '../media'; @@ -96,43 +96,64 @@ export default function PagePages() { id: 'featured-image', header: __( 'Featured Image' ), getValue: ( { item } ) => item.featured_media, - render: ( { item, view: currentView } ) => - !! item.featured_media ? ( - - ) : null, + ...coreFieldTypes.image, + render: + ( getValue ) => + ( { item, view: currentView } ) => { + const value = getValue( { item } ); + if ( ! value ) { + return null; + } + return ( + + ); + }, enableSorting: false, }, { header: __( 'Title' ), id: 'title', getValue: ( { item } ) => item.title?.rendered || item.slug, - render: ( { item } ) => { - return ( - - - - { decodeEntities( - item.title?.rendered || item.slug - ) || __( '(no title)' ) } - - - - ); - }, + ...coreFieldTypes.string, + render: + ( getValue ) => + ( { item } ) => { + return ( + + + + { decodeEntities( + getValue( { item } ) + ) || __( '(no title)' ) } + + + + ); + }, filters: [ { id: 'search', type: 'search' } ], maxWidth: 400, sortingFn: 'alphanumeric', @@ -142,14 +163,17 @@ export default function PagePages() { header: __( 'Author' ), id: 'author', getValue: ( { item } ) => item._embedded?.author[ 0 ]?.name, - render: ( { item } ) => { - const author = item._embedded?.author[ 0 ]; - return ( - - { author.name } - - ); - }, + ...coreFieldTypes.string, + render: + ( getValue ) => + ( { item } ) => { + const author = item._embedded?.author[ 0 ]; + return ( + + { getValue( { item } ) } + + ); + }, filters: [ { id: 'author', type: 'enumeration' } ], elements: [ { @@ -167,6 +191,7 @@ export default function PagePages() { id: 'status', getValue: ( { item } ) => postStatuses[ item.status ] ?? item.status, + ...coreFieldTypes.string, filters: [ { type: 'enumeration', id: 'status' } ], elements: [ { label: __( 'All' ), value: 'publish,draft' }, @@ -187,13 +212,17 @@ export default function PagePages() { header: __( 'Date' ), id: 'date', getValue: ( { item } ) => item.date, - render: ( { item } ) => { - const formattedDate = dateI18n( - getSettings().formats.datetimeAbbreviated, - getDate( item.date ) - ); - return ; - }, + ...coreFieldTypes.date, + render: + ( getValue ) => + ( { item } ) => { + const formattedDate = dateI18n( + getSettings().formats.datetimeAbbreviated, + getDate( getValue( { item } ) ) + ); + return ; + }, + enableSorting: false, }, ], [ postStatuses, authors ]