Skip to content

Commit

Permalink
[Data views]: Normalize render fields function (#55411)
Browse files Browse the repository at this point in the history
* [Data views]: Normalize `render` fields function

* Update packages/edit-site/src/components/dataviews/view-list.js

Co-authored-by: Miguel Fonseca <150562+mcsf@users.noreply.github.com>

---------

Co-authored-by: Miguel Fonseca <150562+mcsf@users.noreply.github.com>
  • Loading branch information
ntsekouras and mcsf authored Oct 17, 2023
1 parent 023c66f commit 8dfa175
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
9 changes: 8 additions & 1 deletion packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -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 (
<div className="dataviews-wrapper">
<VStack spacing={ 4 }>
Expand All @@ -37,7 +44,7 @@ export default function DataViews( {
/>
</HStack>
<ViewComponent
fields={ fields }
fields={ _fields }
view={ view }
onChangeView={ onChangeView }
paginationInfo={ paginationInfo }
Expand Down
7 changes: 2 additions & 5 deletions packages/edit-site/src/components/dataviews/view-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export function ViewGrid( { data, fields, view, actions } ) {
return (
<VStack key={ index }>
<div className="dataviews-view-grid__media">
{ ( mediaField &&
mediaField.render( { item, view } ) ) || (
{ mediaField?.render( item, view ) || (
<Placeholder
withIllustration
style={ {
Expand All @@ -46,9 +45,7 @@ export function ViewGrid( { data, fields, view, actions } ) {
<VStack>
{ visibleFields.map( ( field ) => (
<div key={ field.id }>
{ field.render
? field.render( { item, view } )
: field.accessorFn( item ) }
{ field.render( item, view ) }
</div>
) ) }
</VStack>
Expand Down
17 changes: 5 additions & 12 deletions packages/edit-site/src/components/dataviews/view-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,11 @@ 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 { render, ...column } = field;
column.cell = ( props ) => render( props.row.original, view );
return column;
} );
if ( actions?.length ) {
_columns.push( {
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
Expand Down
8 changes: 4 additions & 4 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
<Media
className="edit-site-page-pages__featured-image"
Expand All @@ -107,7 +107,7 @@ export default function PagePages() {
header: __( 'Title' ),
id: 'title',
accessorFn: ( page ) => page.title?.rendered || page.slug,
render: ( { item: page } ) => {
render: ( page ) => {
return (
<VStack spacing={ 1 }>
<Heading as="h3" level={ 5 }>
Expand All @@ -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 (
<a href={ `user-edit.php?user_id=${ author.id }` }>
Expand All @@ -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 )
Expand Down

1 comment on commit 8dfa175

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8dfa175.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6550415918
📝 Reported issues:

Please sign in to comment.