From ba566116acba7f69b9000d8d933d2ae0e3d21981 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 17 Oct 2023 16:06:16 +0300 Subject: [PATCH 1/2] [Data views]: Normalize `render` fields function --- .../src/components/dataviews/dataviews.js | 9 ++++++++- .../src/components/dataviews/view-grid.js | 7 ++----- .../src/components/dataviews/view-list.js | 18 ++++++------------ .../src/components/page-pages/index.js | 8 ++++---- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/edit-site/src/components/dataviews/dataviews.js b/packages/edit-site/src/components/dataviews/dataviews.js index 755ce8a2ea348d..da0d1735b6a3a6 100644 --- a/packages/edit-site/src/components/dataviews/dataviews.js +++ b/packages/edit-site/src/components/dataviews/dataviews.js @@ -5,6 +5,7 @@ import { __experimentalVStack as VStack, __experimentalHStack as HStack, } from '@wordpress/components'; +import { useMemo } from '@wordpress/element'; /** * Internal dependencies @@ -25,6 +26,12 @@ export default function DataViews( { paginationInfo, } ) { const ViewComponent = view.type === 'list' ? ViewList : ViewGrid; + const _fields = useMemo( () => { + return fields.map( ( field ) => ( { + ...field, + render: field.render || field.accessorFn, + } ) ); + }, [ fields ] ); return (
@@ -37,7 +44,7 @@ export default function DataViews( { />
- { ( mediaField && - mediaField.render( { item, view } ) ) || ( + { mediaField?.render( item, view ) || ( { visibleFields.map( ( field ) => (
- { field.render - ? field.render( { item, view } ) - : field.accessorFn( item ) } + { field.render( item, view ) }
) ) } diff --git a/packages/edit-site/src/components/dataviews/view-list.js b/packages/edit-site/src/components/dataviews/view-list.js index c8d4ee1efd063d..884f9d4f5f5101 100644 --- a/packages/edit-site/src/components/dataviews/view-list.js +++ b/packages/edit-site/src/components/dataviews/view-list.js @@ -132,18 +132,12 @@ function ViewList( { paginationInfo, } ) { const columns = useMemo( () => { - const _columns = [ - ...fields.map( ( field ) => { - const column = { ...field }; - delete column.render; - column.cell = ( props ) => { - return field.render - ? field.render( { item: props.row.original, view } ) - : field.accessorFn( props.row.original ); - }; - return column; - } ), - ]; + const _columns = fields.map( ( field ) => { + const column = { ...field }; + delete column.render; + column.cell = ( props ) => field.render( props.row.original, view ); + return column; + } ); if ( actions?.length ) { _columns.push( { header: { __( 'Actions' ) }, diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js index 602d14b8ad2e3b..502e03f49b3e88 100644 --- a/packages/edit-site/src/components/page-pages/index.js +++ b/packages/edit-site/src/components/page-pages/index.js @@ -89,7 +89,7 @@ export default function PagePages() { id: 'featured-image', header: __( 'Featured Image' ), accessorFn: ( page ) => page.featured_media, - render: ( { item, view: currentView } ) => + render: ( item, currentView ) => !! item.featured_media ? ( page.title?.rendered || page.slug, - render: ( { item: page } ) => { + render: ( page ) => { return ( @@ -134,7 +134,7 @@ export default function PagePages() { header: __( 'Author' ), id: 'author', accessorFn: ( page ) => page._embedded?.author[ 0 ]?.name, - render: ( { item } ) => { + render: ( item ) => { const author = item._embedded?.author[ 0 ]; return ( @@ -153,7 +153,7 @@ export default function PagePages() { { header: 'Date', id: 'date', - render: ( { item } ) => { + render: ( item ) => { const formattedDate = dateI18n( getSettings().formats.datetimeAbbreviated, getDate( item.date ) From 87c8a9cbdcb30327851117de0a314b8f15c97d9f Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Tue, 17 Oct 2023 19:05:54 +0300 Subject: [PATCH 2/2] Update packages/edit-site/src/components/dataviews/view-list.js Co-authored-by: Miguel Fonseca <150562+mcsf@users.noreply.github.com> --- packages/edit-site/src/components/dataviews/view-list.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/edit-site/src/components/dataviews/view-list.js b/packages/edit-site/src/components/dataviews/view-list.js index 884f9d4f5f5101..628ff281737ba6 100644 --- a/packages/edit-site/src/components/dataviews/view-list.js +++ b/packages/edit-site/src/components/dataviews/view-list.js @@ -133,9 +133,8 @@ function ViewList( { } ) { const columns = useMemo( () => { const _columns = fields.map( ( field ) => { - const column = { ...field }; - delete column.render; - column.cell = ( props ) => field.render( props.row.original, view ); + const { render, ...column } = field; + column.cell = ( props ) => render( props.row.original, view ); return column; } ); if ( actions?.length ) {