Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- DataForm: Translate the "(Required)" and "(Optional)" suffixes of date and date time field labels together with the label, so translators can change their order ([#87487](https://github.com/WordPress/gutenberg/pull/87487)).
- Table layout: When the title isn't shown, name clickable media after the item's title, or "Navigate to item" if there's none, like the grid layout does ([#87233](https://github.com/WordPress/gutenberg/pull/87233)).

### Internal

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ComponentProps, ReactElement } from 'react';
import { __ } from '@wordpress/i18n';
import { Stack } from '@wordpress/ui';
import type { MediaAspectRatio, NormalizedField } from '../../../types';
import { ItemClickWrapper } from '../utils/item-click-wrapper';
Expand All @@ -9,6 +10,7 @@ function ColumnPrimary< Item >( {
titleField,
mediaField,
mediaAspectRatio,
mediaLabel,
descriptionField,
onClickItem,
renderItemLink,
Expand All @@ -19,6 +21,7 @@ function ColumnPrimary< Item >( {
titleField?: NormalizedField< Item >;
mediaField?: NormalizedField< Item >;
mediaAspectRatio?: MediaAspectRatio;
mediaLabel?: string;
descriptionField?: NormalizedField< Item >;
onClickItem?: ( item: Item ) => void;
renderItemLink?: (
Expand All @@ -43,6 +46,13 @@ function ColumnPrimary< Item >( {
Math.round( ( 32 * ratioWidth ) / ratioHeight )
) }px`;
}
const isMediaClickable =
isItemClickable( item ) && ( !! onClickItem || !! renderItemLink );
// Named after the item like the grid media, as it's the only control for
// the item when the title isn't shown.
const mediaAriaLabel = isMediaClickable
? mediaLabel || __( 'Navigate to item' )
: undefined;
return (
<Stack direction="row" gap="md" align="flex-start" justify="flex-start">
{ mediaField && (
Expand All @@ -52,13 +62,7 @@ function ColumnPrimary< Item >( {
onClickItem={ onClickItem }
renderItemLink={ renderItemLink }
className="dataviews-view-table__cell-content-wrapper dataviews-column-primary__media"
aria-label={
isItemClickable( item ) &&
( !! onClickItem || !! renderItemLink ) &&
!! titleField
? titleField.getValue?.( { item } )
: undefined
}
aria-label={ mediaAriaLabel }
>
<mediaField.render
item={ item }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ function TableRow< Item >( {
titleField={ showTitle ? titleField : undefined }
mediaField={ showMedia ? mediaField : undefined }
mediaAspectRatio={ mediaAspectRatio }
mediaLabel={
showMedia && mediaField
? titleField?.getValueFormatted( {
item,
field: titleField,
} )
: undefined
}
descriptionField={
showDescription ? descriptionField : undefined
}
Expand Down
37 changes: 37 additions & 0 deletions packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,43 @@ describe( 'DataViews component', () => {
}
} );

it( 'names clickable media after the item when the title is hidden', () => {
render(
<DataViewWrapper
view={ {
...DEFAULT_VIEW,
fields: [],
titleField: 'title',
mediaField: 'image',
showTitle: false,
} }
isItemClickable={ () => true }
onClickItem={ () => {} }
/>
);
expect(
screen.getByRole( 'button', { name: data[ 0 ].title } )
).toHaveClass( 'dataviews-column-primary__media' );
} );

it( 'names clickable media "Navigate to item" when there is no title field', () => {
render(
<DataViewWrapper
view={ {
...DEFAULT_VIEW,
fields: [],
titleField: undefined,
mediaField: 'image',
} }
isItemClickable={ () => true }
onClickItem={ () => {} }
/>
);
expect(
screen.getAllByRole( 'button', { name: 'Navigate to item' } )
).toHaveLength( data.length );
} );

it( 'should render actions column if actions are supported and passed in', () => {
render( <DataViewWrapper actions={ actions } /> );
expect( screen.getByText( 'Actions' ) ).toBeInTheDocument();
Expand Down
Loading