Skip to content

Commit

Permalink
DataViews: Fix field rendering (#63452)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2024
1 parent ebb5788 commit d6eecb9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
26 changes: 10 additions & 16 deletions packages/dataviews/src/layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
<mediaField.render item={ item } />
) : null;
const renderedPrimaryField = primaryField?.render ? (
<primaryField.render item={ item } />
) : null;
return (
<VStack
spacing={ 0 }
Expand All @@ -76,7 +82,7 @@ function GridItem< Item >( {
} }
>
<div className="dataviews-view-grid__media">
{ mediaField?.render( { item } ) }
{ renderedMediaField }
</div>
<HStack
justify="space-between"
Expand All @@ -91,7 +97,7 @@ function GridItem< Item >( {
disabled={ ! hasBulkAction }
/>
<HStack className="dataviews-view-grid__primary-field">
{ primaryField?.render( { item } ) }
{ renderedPrimaryField }
</HStack>
<ItemActions item={ item } actions={ actions } isCompact />
</HStack>
Expand All @@ -104,18 +110,12 @@ function GridItem< Item >( {
justify="flex-start"
>
{ badgeFields.map( ( field ) => {
const renderedValue = field.render( {
item,
} );
if ( ! renderedValue ) {
return null;
}
return (
<FlexItem
key={ field.id }
className="dataviews-view-grid__field-value"
>
{ renderedValue }
<field.render item={ item } />
</FlexItem>
);
} ) }
Expand All @@ -124,12 +124,6 @@ function GridItem< Item >( {
{ !! visibleFields?.length && (
<VStack className="dataviews-view-grid__fields" spacing={ 1 }>
{ visibleFields.map( ( field ) => {
const renderedValue = field.render( {
item,
} );
if ( ! renderedValue ) {
return null;
}
return (
<Flex
className={ clsx(
Expand Down Expand Up @@ -157,7 +151,7 @@ function GridItem< Item >( {
className="dataviews-view-grid__field-value"
style={ { maxHeight: 'none' } }
>
{ renderedValue }
<field.render item={ item } />
</FlexItem>
</>
</Flex>
Expand Down
18 changes: 13 additions & 5 deletions packages/dataviews/src/layouts/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ function ListItem< Item >( {
? primaryAction.label
: primaryAction.label( [ item ] ) );

const renderedMediaField = mediaField?.render ? (
<mediaField.render item={ item } />
) : (
<div className="dataviews-view-list__media-placeholder"></div>
);

const renderedPrimaryField = primaryField?.render ? (
<primaryField.render item={ item } />
) : null;

return (
<CompositeRow
ref={ itemRef }
Expand Down Expand Up @@ -147,9 +157,7 @@ function ListItem< Item >( {
alignment="flex-start"
>
<div className="dataviews-view-list__media-wrapper">
{ mediaField?.render( { item } ) || (
<div className="dataviews-view-list__media-placeholder"></div>
) }
{ renderedMediaField }
</div>
<VStack
spacing={ 1 }
Expand All @@ -159,7 +167,7 @@ function ListItem< Item >( {
className="dataviews-view-list__primary-field"
id={ labelId }
>
{ primaryField?.render( { item } ) }
{ renderedPrimaryField }
</span>
<div
className="dataviews-view-list__fields"
Expand All @@ -177,7 +185,7 @@ function ListItem< Item >( {
{ field.header }
</VisuallyHidden>
<span className="dataviews-view-list__field-value">
{ field.render( { item } ) }
<field.render item={ item } />
</span>
</div>
) ) }
Expand Down
24 changes: 8 additions & 16 deletions packages/dataviews/src/layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,23 +360,15 @@ function TableColumnField< Item >( {
item,
field,
}: TableColumnFieldProps< Item > ) {
const value = field.render( {
item,
} );
return (
!! value && (
<div
className={ clsx(
'dataviews-view-table__cell-content-wrapper',
{
'dataviews-view-table__primary-field':
primaryField?.id === field.id,
}
) }
>
{ value }
</div>
)
<div
className={ clsx( 'dataviews-view-table__cell-content-wrapper', {
'dataviews-view-table__primary-field':
primaryField?.id === field.id,
} ) }
>
<field.render { ...{ item } } />
</div>
);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/dataviews/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import type { ReactElement, ReactNode } from 'react';
import type { ReactElement, ComponentType } from 'react';

/**
* Internal dependencies
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 } >;
};

/**
Expand Down

0 comments on commit d6eecb9

Please sign in to comment.