Skip to content

Commit

Permalink
[Data views]: Normalize render fields function
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Oct 17, 2023
1 parent 017d1ad commit ba56611
Show file tree
Hide file tree
Showing 4 changed files with 20 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
18 changes: 6 additions & 12 deletions packages/edit-site/src/components/dataviews/view-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: <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

0 comments on commit ba56611

Please sign in to comment.