Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemList: Add check for empty fields #396

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions src/blocks/remote-data-container/components/item-list/ItemList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useInstanceId } from '@wordpress/compose';
import { Action, DataViews, View } from '@wordpress/dataviews/wp';
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

import { ItemListField } from '@/blocks/remote-data-container/components/item-list/ItemListField';
Expand Down Expand Up @@ -128,10 +128,24 @@ export function ItemList( props: ItemListProps ) {
selection: selectedItems,
} );

// Only update if tableFields has content and view.fields is empty
useEffect( () => {
if ( tableFields.length > 0 && view.fields?.length === 0 ) {
setView( currentView => ( {
...currentView,
fields: tableFields,
} ) );
}
}, [ tableFields ] );
Copy link
Member

Choose a reason for hiding this comment

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

The effect runs whenever tableFields changes. When does that happen? Or does it only run once?


function onChangeView( newView: View ) {
setPage( newView.page ?? 1 );
setSearchInput( newView.search ?? '' );
setView( { ...newView, selection: selectedItems } );
setView( {
...newView,
fields: newView.fields ?? tableFields,
selection: selectedItems,
} );
}

const defaultLayouts = mediaField
Expand Down
Loading