Skip to content

Commit

Permalink
DataViews: Fix reordering fields in list and grid layouts (WordPress#…
Browse files Browse the repository at this point in the history
…67777)

Co-authored-by: youknowriad <[email protected]>
Co-authored-by: oandregal <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent 7649b99 commit 207dfe3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
19 changes: 8 additions & 11 deletions packages/dataviews/src/dataviews-layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,18 @@ export default function ViewGrid< Item >( {
( field ) => field.id === view?.descriptionField
);
const otherFields = view.fields ?? [];
const { regularFields, badgeFields } = fields.reduce(
( accumulator: Record< string, NormalizedField< Item >[] >, field ) => {
if (
! otherFields.includes( field.id ) ||
[
view?.mediaField,
view?.titleField,
view?.descriptionField,
].includes( field.id )
) {
const { regularFields, badgeFields } = otherFields.reduce(
(
accumulator: Record< string, NormalizedField< Item >[] >,
fieldId
) => {
const field = fields.find( ( f ) => f.id === fieldId );
if ( ! field ) {
return accumulator;
}
// If the field is a badge field, add it to the badgeFields array
// otherwise add it to the rest visibleFields array.
const key = view.layout?.badgeFields?.includes( field.id )
const key = view.layout?.badgeFields?.includes( fieldId )
? 'badgeFields'
: 'regularFields';
accumulator[ key ].push( field );
Expand Down
16 changes: 7 additions & 9 deletions packages/dataviews/src/dataviews-layouts/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ function ListItem< Item >( {
);
}

function isDefined< T >( item: T | undefined ): item is T {
return !! item;
}

export default function ViewList< Item >( props: ViewListProps< Item > ) {
const {
actions,
Expand All @@ -346,15 +350,9 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
const descriptionField = fields.find(
( field ) => field.id === view.descriptionField
);
const otherFields = fields.filter(
( field ) =>
( view.fields ?? [] ).includes( field.id ) &&
! [
view.titleField,
view.mediaField,
view.descriptionField,
].includes( field.id )
);
const otherFields = ( view?.fields ?? [] )
.map( ( fieldId ) => fields.find( ( f ) => fieldId === f.id ) )
.filter( isDefined );

const onSelect = ( item: Item ) =>
onChangeSelection( [ getItemId( item ) ] );
Expand Down

0 comments on commit 207dfe3

Please sign in to comment.