Skip to content

Commit

Permalink
Add check for empty fields
Browse files Browse the repository at this point in the history
Signed-off-by: brookewp <[email protected]>
  • Loading branch information
brookewp committed Feb 27, 2025
1 parent 73a6bfc commit dc33a98
Showing 1 changed file with 16 additions and 2 deletions.
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 ) {

Check failure on line 133 in src/blocks/remote-data-container/components/item-list/ItemList.tsx

View workflow job for this annotation

GitHub Actions / eslint, prettier, wp-scripts

'view.fields' is possibly 'undefined'.
setView( currentView => ( {
...currentView,
fields: tableFields,
} ) );
}
}, [ tableFields ] );

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

0 comments on commit dc33a98

Please sign in to comment.