Skip to content

Commit

Permalink
Pass item & value to sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 30, 2024
1 parent bcf814a commit eaa0b63
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
18 changes: 10 additions & 8 deletions packages/dataviews/src/field-types/integer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import type { SortDirection } from '../types';

function sort< Item >(
itemA: Item,
itemB: Item,
direction: SortDirection,
getValue: ( args: { item: Item } ) => any
a: {
item: Item;
value: any;
},
b: {
item: Item;
value: any;
},
direction: SortDirection
) {
const valueA = getValue( { item: itemA } );
const valueB = getValue( { item: itemB } );

return direction === 'asc' ? valueA - valueB : valueB - valueA;
return direction === 'asc' ? a.value - b.value : b.value - a.value;
}

export default {
Expand Down
18 changes: 12 additions & 6 deletions packages/dataviews/src/filter-and-sort-data-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,24 @@ export function filterSortAndPaginate< Item >(
} );
if ( fieldToSort ) {
filteredData.sort( ( a, b ) => {
const valueA = fieldToSort.getValue( { item: a } ) ?? '';
const valueB = fieldToSort.getValue( { item: b } ) ?? '';

if ( fieldToSort.type === 'integer' ) {
return fieldToSort.sort(
a,
b,
view.sort?.direction ?? 'desc',
fieldToSort.getValue
{
item: a,
value: valueA,
},
{
item: b,
value: valueB,
},
view.sort?.direction ?? 'desc'
);
}

// When/if types become required, we can remove the following logic.
const valueA = fieldToSort.getValue( { item: a } ) ?? '';
const valueB = fieldToSort.getValue( { item: b } ) ?? '';
if (
typeof valueA === 'number' &&
typeof valueB === 'number'
Expand Down
26 changes: 18 additions & 8 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ export type Field< Item > = {
* Callback used to sort the field.
*/
sort?: (
a: Item,
b: Item,
direction: SortDirection,
getValue: ( args: { item: Item } ) => any
a: {
item: Item;
value: any;
},
b: {
item: Item;
value: any;
},
direction: SortDirection
) => number;

/**
Expand Down Expand Up @@ -135,10 +140,15 @@ export type NormalizedField< Item > = Field< Item > & {
getValue: ( args: { item: Item } ) => any;
render: ComponentType< { item: Item } >;
sort: (
a: Item,
b: Item,
direction: SortDirection,
getValue: ( args: { item: Item } ) => any
a: {
item: Item;
value: any;
},
b: {
item: Item;
value: any;
},
direction: SortDirection
) => number;
};

Expand Down
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ function usePostFields( viewType ) {
label: name,
} ) ) || [],
render: PostAuthorField,
sort: ( fieldA, fieldB, direction ) => {
const nameA = fieldA._embedded?.author?.[ 0 ]?.name || '';
const nameB = fieldB._embedded?.author?.[ 0 ]?.name || '';
sort: ( { item: a }, { item: b }, direction ) => {
const nameA = a._embedded?.author?.[ 0 ]?.name || '';
const nameB = b._embedded?.author?.[ 0 ]?.name || '';

return direction === 'asc'
? nameA.localeCompare( nameB )
Expand Down

0 comments on commit eaa0b63

Please sign in to comment.