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

DataViews: Fix reordering fields in list and grid layouts #67777

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
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
Loading