Skip to content
Merged
Prev Previous commit
Next Next commit
Revert "Update API to use view.sort.grouped instead"
This reverts commit 90cb723.
  • Loading branch information
oandregal committed Jul 21, 2025
commit fef3502445c62ad4976eb433043155636a8b1bad
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ function SortFieldControl() {
sort: {
direction: view?.sort?.direction || 'desc',
field: value,
...( view?.sort?.grouped && {
grouped: view.sort.grouped,
} ),
},
showLevels: false,
} );
Expand Down Expand Up @@ -194,9 +191,6 @@ function SortDirectionControl() {
( field ) => field.enableSorting !== false
)?.id ||
'',
...( view?.sort?.grouped && {
grouped: view.sort.grouped,
} ),
},
showLevels: false,
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,7 @@ export const GroupedGridLayout = () => {
titleField: 'title',
descriptionField: 'description',
mediaField: 'image',
sort: {
field: 'type',
direction: 'asc',
grouped: true,
},
groupByField: 'type',
layout: {
badgeFields: [ 'satellites' ],
},
Expand Down
11 changes: 6 additions & 5 deletions packages/dataviews/src/dataviews-layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,16 @@ function ViewGrid< Item >( {
}
: {};

// Group data by sort.field if sort.grouped is true
const groupField = view.sort?.grouped
? fields.find( ( f ) => f.id === view.sort?.field )
const groupField = view.groupByField
? fields.find( ( f ) => f.id === view.groupByField )
: null;
const groupedData = view.sort?.grouped

// Group data by groupByField if specified
const groupedData = view.groupByField
? data.reduce( ( groups: { [ key: string ]: typeof data }, item ) => {
const groupValue = groupField?.getValue
? groupField.getValue( { item } )
: ( item as any )[ view.sort?.field! ];
: ( item as any )[ view.groupByField! ];
const key = String( groupValue ?? __( 'No group' ) );
if ( ! groups[ key ] ) {
groups[ key ] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ const _HeaderMenu = forwardRef( function HeaderMenu< Item >(
sort: {
field: fieldId,
direction,
...( view?.sort
?.grouped && {
grouped:
view.sort
.grouped,
} ),
},
showLevels: false,
} );
Expand Down
10 changes: 5 additions & 5 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,6 @@ interface ViewBase {
* The direction to sort by.
*/
direction: SortDirection;

/**
* Whether to group the data by this field.
*/
grouped?: boolean;
};

/**
Expand Down Expand Up @@ -424,6 +419,11 @@ interface ViewBase {
* Whether to show the hierarchical levels.
*/
showLevels?: boolean;

/**
* The field to group by.
*/
groupByField?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Any thoughts on a UI to configure this? (not necessary for this PR)

Also would be good to document this in the README.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done at d97929e

Copy link
Member Author

Choose a reason for hiding this comment

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

Any thoughts on a UI to configure this? (not necessary for this PR)

The obvious way is to add a new select in the view config somewhere. Alternatively, we could explore making it part of the field list (similarly to what we have for the media field).

}

export interface ColumnStyle {
Expand Down