Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DataViewsContextType< Item > = {
actions?: Action< Item >[];
data: Item[];
isLoading?: boolean;
keepPreviousData?: boolean;
paginationInfo: {
totalItems: number;
totalPages: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function DataViewsLayout() {
setOpenedFilter,
onClickItem,
isItemClickable,
keepPreviousData,
} = useContext( DataViewsContext );

const ViewComponent = VIEW_LAYOUTS.find( ( v ) => v.type === view.type )
Expand All @@ -43,6 +44,7 @@ export default function DataViewsLayout() {
getItemId={ getItemId }
getItemLevel={ getItemLevel }
isLoading={ isLoading }
keepPreviousData={ keepPreviousData }
onChangeView={ onChangeView }
onChangeSelection={ onChangeSelection }
selection={ selection }
Expand Down
3 changes: 3 additions & 0 deletions packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type DataViewsProps< Item > = {
actions?: Action< Item >[];
data: Item[];
isLoading?: boolean;
keepPreviousData?: boolean;
paginationInfo: {
totalItems: number;
totalPages: number;
Expand Down Expand Up @@ -68,6 +69,7 @@ export default function DataViews< Item >( {
getItemId = defaultGetItemId,
getItemLevel,
isLoading = false,
keepPreviousData = false,
paginationInfo,
defaultLayouts,
selection: selectionProperty,
Expand Down Expand Up @@ -121,6 +123,7 @@ export default function DataViews< Item >( {
actions,
data,
isLoading,
keepPreviousData,
paginationInfo,
selection: _selection,
onChangeSelection: setSelectionWithChange,
Expand Down
71 changes: 44 additions & 27 deletions packages/dataviews/src/dataviews-layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import clsx from 'clsx';
import { __ } from '@wordpress/i18n';
import { Spinner } from '@wordpress/components';
import { useEffect, useId, useRef, useState } from '@wordpress/element';
import { usePrevious } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -210,6 +211,7 @@ function ViewTable< Item >( {
getItemId,
getItemLevel,
isLoading = false,
keepPreviousData,
onChangeView,
onChangeSelection,
selection,
Expand All @@ -234,6 +236,8 @@ function ViewTable< Item >( {
} );

const tableNoticeId = useId();
// We need to keep the previous data to render the table when the new data is loading.
const previousData = usePrevious( data );

if ( nextHeaderMenuToFocus ) {
// If we need to force focus, we short-circuit rendering here
Expand Down Expand Up @@ -278,6 +282,40 @@ function ViewTable< Item >( {
}
};

/**
* Render the table rows.
*
* @param items The items to render.
*
* @return The table rows.
*/
const renderTableRows = ( items: Item[] ) => {
return items.map( ( item, index ) => (
<TableRow
key={ getItemId( item ) }
item={ item }
level={
view.showLevels && typeof getItemLevel === 'function'
? getItemLevel( item )
: undefined
}
hasBulkActions={ hasBulkActions }
actions={ actions }
fields={ fields }
id={ getItemId( item ) || index.toString() }
view={ view }
titleField={ titleField }
mediaField={ mediaField }
descriptionField={ descriptionField }
selection={ selection }
getItemId={ getItemId }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
isItemClickable={ isItemClickable }
/>
) );
};

return (
<>
<table
Expand Down Expand Up @@ -363,33 +401,12 @@ function ViewTable< Item >( {
) }
</tr>
</thead>
<tbody>
{ hasData &&
data.map( ( item, index ) => (
<TableRow
key={ getItemId( item ) }
item={ item }
level={
view.showLevels &&
typeof getItemLevel === 'function'
? getItemLevel( item )
: undefined
}
hasBulkActions={ hasBulkActions }
actions={ actions }
fields={ fields }
id={ getItemId( item ) || index.toString() }
view={ view }
titleField={ titleField }
mediaField={ mediaField }
descriptionField={ descriptionField }
selection={ selection }
getItemId={ getItemId }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
isItemClickable={ isItemClickable }
/>
) ) }
<tbody aria-busy={ keepPreviousData && isLoading }>
{ keepPreviousData &&
isLoading &&
previousData &&
renderTableRows( previousData ) }
{ hasData && ! isLoading && renderTableRows( data ) }
</tbody>
</table>
<div
Expand Down
5 changes: 5 additions & 0 deletions packages/dataviews/src/dataviews-layouts/table/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
.components-v-stack > .dataviews-view-table__cell-content-wrapper:not(:first-child) {
min-height: 0;
}

&[aria-busy="true"] {
opacity: 0.5;
pointer-events: none;
}
}
.dataviews-view-table-header-button {
padding: $grid-unit-05 $grid-unit-10;
Expand Down
1 change: 1 addition & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ export interface ViewBaseProps< Item > {
getItemId: ( item: Item ) => string;
getItemLevel?: ( item: Item ) => number;
isLoading?: boolean;
keepPreviousData?: boolean;
onChangeView: ( view: View ) => void;
onChangeSelection: SetSelection;
selection: string[];
Expand Down