Skip to content

Commit

Permalink
DataViews: Render data async conditionally (#56851)
Browse files Browse the repository at this point in the history
* DataViews: Render data async conditionally

* rename to `deferredRendering`
  • Loading branch information
ntsekouras authored Dec 7, 2023
1 parent 40c233f commit 26a5d6d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/dataviews/src/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function DataViews( {
paginationInfo,
supportedLayouts,
onSelectionChange,
deferredRendering,
} ) {
const [ selection, setSelection ] = useState( [] );

Expand Down Expand Up @@ -82,6 +83,7 @@ export default function DataViews( {
isLoading={ isLoading }
onSelectionChange={ onSetSelection }
selection={ selection }
deferredRendering={ deferredRendering }
/>
<Pagination
view={ view }
Expand Down
5 changes: 4 additions & 1 deletion packages/dataviews/src/view-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ function FieldsVisibilityMenu( { view, onChangeView, fields } ) {
? view.hiddenFields.filter(
( id ) => id !== field.id
)
: [ ...view.hiddenFields, field.id ],
: [
...( view.hiddenFields || [] ),
field.id,
],
} );
} }
>
Expand Down
12 changes: 10 additions & 2 deletions packages/dataviews/src/view-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import { useAsyncList } from '@wordpress/compose';
*/
import ItemActions from './item-actions';

export default function ViewGrid( { data, fields, view, actions, getItemId } ) {
export default function ViewGrid( {
data,
fields,
view,
actions,
getItemId,
deferredRendering,
} ) {
const mediaField = fields.find(
( field ) => field.id === view.layout.mediaField
);
Expand All @@ -29,14 +36,15 @@ export default function ViewGrid( { data, fields, view, actions, getItemId } ) {
)
);
const shownData = useAsyncList( data, { step: 3 } );
const usedData = deferredRendering ? shownData : data;
return (
<Grid
gap={ 8 }
columns={ 2 }
alignment="top"
className="dataviews-grid-view"
>
{ shownData.map( ( item, index ) => (
{ usedData.map( ( item, index ) => (
<VStack
spacing={ 3 }
key={ getItemId?.( item ) || index }
Expand Down
4 changes: 3 additions & 1 deletion packages/dataviews/src/view-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export default function ViewList( {
getItemId,
onSelectionChange,
selection,
deferredRendering,
} ) {
const shownData = useAsyncList( data, { step: 3 } );
const usedData = deferredRendering ? shownData : data;
const mediaField = fields.find(
( field ) => field.id === view.layout.mediaField
);
Expand All @@ -45,7 +47,7 @@ export default function ViewList( {

return (
<ul className="dataviews-list-view">
{ shownData.map( ( item, index ) => {
{ usedData.map( ( item, index ) => {
return (
<li key={ getItemId?.( item ) || index }>
<div
Expand Down
6 changes: 4 additions & 2 deletions packages/dataviews/src/view-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ function ViewTable( {
getItemId,
isLoading = false,
paginationInfo,
deferredRendering,
} ) {
const columns = useMemo( () => {
const _columns = fields.map( ( field ) => {
Expand Down Expand Up @@ -367,7 +368,7 @@ function ViewTable( {
}

return _columns;
}, [ fields, actions, view ] );
}, [ fields, actions ] );

const columnVisibility = useMemo( () => {
if ( ! view.hiddenFields?.length ) {
Expand Down Expand Up @@ -435,8 +436,9 @@ function ViewTable( {
} );

const shownData = useAsyncList( data );
const usedData = deferredRendering ? shownData : data;
const dataView = useReactTable( {
data: shownData,
data: usedData,
columns,
manualSorting: true,
manualFiltering: true,
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export default function PagePages() {
view={ view }
onChangeView={ onChangeView }
onSelectionChange={ onSelectionChange }
deferredRendering={ false }
/>
</Page>
{ view.type === LAYOUT_LIST && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export default function DataviewsTemplates() {
view={ view }
onChangeView={ onChangeView }
supportedLayouts={ [ LAYOUT_TABLE, LAYOUT_GRID ] }
deferredRendering={ ! view.hiddenFields?.includes( 'preview' ) }
/>
</Page>
);
Expand Down

1 comment on commit 26a5d6d

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 26a5d6d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7126835976
📝 Reported issues:

Please sign in to comment.