From d6eecb92d364ca066ff63f484076655986342d48 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 12 Jul 2024 10:14:58 +0200 Subject: [PATCH] DataViews: Fix field rendering (#63452) Co-authored-by: youknowriad Co-authored-by: ntsekouras --- packages/dataviews/src/layouts/grid/index.tsx | 26 +++++++------------ packages/dataviews/src/layouts/list/index.tsx | 18 +++++++++---- .../dataviews/src/layouts/table/index.tsx | 24 ++++++----------- packages/dataviews/src/style.scss | 6 +++++ packages/dataviews/src/types.ts | 6 ++--- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/packages/dataviews/src/layouts/grid/index.tsx b/packages/dataviews/src/layouts/grid/index.tsx index 6e49c619a95ab..fbcf93124190d 100644 --- a/packages/dataviews/src/layouts/grid/index.tsx +++ b/packages/dataviews/src/layouts/grid/index.tsx @@ -53,6 +53,12 @@ function GridItem< Item >( { const hasBulkAction = useHasAPossibleBulkAction( actions, item ); const id = getItemId( item ); const isSelected = selection.includes( id ); + const renderedMediaField = mediaField?.render ? ( + + ) : null; + const renderedPrimaryField = primaryField?.render ? ( + + ) : null; return ( ( { } } >
- { mediaField?.render( { item } ) } + { renderedMediaField }
( { disabled={ ! hasBulkAction } /> - { primaryField?.render( { item } ) } + { renderedPrimaryField } @@ -104,18 +110,12 @@ function GridItem< Item >( { justify="flex-start" > { badgeFields.map( ( field ) => { - const renderedValue = field.render( { - item, - } ); - if ( ! renderedValue ) { - return null; - } return ( - { renderedValue } + ); } ) } @@ -124,12 +124,6 @@ function GridItem< Item >( { { !! visibleFields?.length && ( { visibleFields.map( ( field ) => { - const renderedValue = field.render( { - item, - } ); - if ( ! renderedValue ) { - return null; - } return ( ( { className="dataviews-view-grid__field-value" style={ { maxHeight: 'none' } } > - { renderedValue } + diff --git a/packages/dataviews/src/layouts/list/index.tsx b/packages/dataviews/src/layouts/list/index.tsx index bb38f20c02b61..5642af5bdb152 100644 --- a/packages/dataviews/src/layouts/list/index.tsx +++ b/packages/dataviews/src/layouts/list/index.tsx @@ -112,6 +112,16 @@ function ListItem< Item >( { ? primaryAction.label : primaryAction.label( [ item ] ) ); + const renderedMediaField = mediaField?.render ? ( + + ) : ( +
+ ); + + const renderedPrimaryField = primaryField?.render ? ( + + ) : null; + return ( ( { alignment="flex-start" >
- { mediaField?.render( { item } ) || ( -
- ) } + { renderedMediaField }
( { className="dataviews-view-list__primary-field" id={ labelId } > - { primaryField?.render( { item } ) } + { renderedPrimaryField }
( { { field.header } - { field.render( { item } ) } +
) ) } diff --git a/packages/dataviews/src/layouts/table/index.tsx b/packages/dataviews/src/layouts/table/index.tsx index a842140e64285..da6658d3f903e 100644 --- a/packages/dataviews/src/layouts/table/index.tsx +++ b/packages/dataviews/src/layouts/table/index.tsx @@ -360,23 +360,15 @@ function TableColumnField< Item >( { item, field, }: TableColumnFieldProps< Item > ) { - const value = field.render( { - item, - } ); return ( - !! value && ( -
- { value } -
- ) +
+ +
); } diff --git a/packages/dataviews/src/style.scss b/packages/dataviews/src/style.scss index a8f6312cece3b..46b4b09d317ce 100644 --- a/packages/dataviews/src/style.scss +++ b/packages/dataviews/src/style.scss @@ -244,6 +244,12 @@ } } +.dataviews-view-table__cell-content-wrapper:empty, +.dataviews-view-grid__field-value:empty, +.dataviews-view-grid__field:empty { + display: none; +} + .dataviews-view-list__primary-field, .dataviews-view-grid__primary-field, .dataviews-view-table__primary-field { diff --git a/packages/dataviews/src/types.ts b/packages/dataviews/src/types.ts index 8e2626245682e..b7947ae8883a3 100644 --- a/packages/dataviews/src/types.ts +++ b/packages/dataviews/src/types.ts @@ -1,7 +1,7 @@ /** * External dependencies */ -import type { ReactElement, ReactNode } from 'react'; +import type { ReactElement, ComponentType } from 'react'; /** * Internal dependencies @@ -73,7 +73,7 @@ export type Field< Item > = { /** * Callback used to render the field. Defaults to `field.getValue`. */ - render?: ( args: { item: Item } ) => ReactNode; + render?: ComponentType< { item: Item } >; /** * Whether the field is sortable. @@ -118,7 +118,7 @@ export type Field< Item > = { export type NormalizedField< Item > = Field< Item > & { header: string; getValue: ( args: { item: Item } ) => any; - render: ( args: { item: Item } ) => ReactNode; + render: ComponentType< { item: Item } >; }; /**