Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data views]: Normalize render fields function #55411

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -132,18 +132,11 @@ function ViewList( {
paginationInfo,
} ) {
const columns = useMemo( () => {
const _columns = [
...fields.map( ( field ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was an extra spreading of the array here.

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 ) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem is that this is not a component anymore, I think make it a component have some advantages. So I'd rather change the signature of accessorFn. In fact I'd rename accessorFn to something like getValue or something. accessorFn is too tanstaky and too confusing to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm planning on doing this today.

!! 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
Loading