Skip to content

Commit

Permalink
DataViews's filterSortAndPaginate utility: support sorting by number (#…
Browse files Browse the repository at this point in the history
…63187)

Co-authored-by: oandregal <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
3 people committed Jul 5, 2024
1 parent 31a3c5d commit 6034e90
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/dataviews/src/filter-and-sort-data-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function filterSortAndPaginate< Item >(
} );
}

if ( view.filters.length > 0 ) {
if ( view.filters?.length > 0 ) {
view.filters.forEach( ( filter ) => {
const field = _fields.find(
( _field ) => _field.id === filter.field
Expand Down Expand Up @@ -142,6 +142,16 @@ export function filterSortAndPaginate< Item >(
filteredData.sort( ( a, b ) => {
const valueA = fieldToSort.getValue( { item: a } ) ?? '';
const valueB = fieldToSort.getValue( { item: b } ) ?? '';

if (
typeof valueA === 'number' &&
typeof valueB === 'number'
) {
return view.sort?.direction === 'asc'
? valueA - valueB
: valueB - valueA;
}

return view.sort?.direction === 'asc'
? valueA.localeCompare( valueB )
: valueB.localeCompare( valueA );
Expand Down
16 changes: 16 additions & 0 deletions packages/dataviews/src/stories/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Not a planet',
categories: [ 'Space', 'NASA' ],
satellites: 0,
},
{
id: 2,
Expand All @@ -30,6 +31,7 @@ export const data = [
image: 'https://live.staticflickr.com/5678/21911065441_92e2d44708_b.jpg',
type: 'Not a planet',
categories: [ 'Space' ],
satellites: 0,
},
{
id: 3,
Expand All @@ -38,6 +40,7 @@ export const data = [
image: 'https://live.staticflickr.com/742/21712365770_8f70a2c91e_b.jpg',
type: 'Not a planet',
categories: [ 'NASA' ],
satellites: 0,
},
{
id: 4,
Expand All @@ -46,6 +49,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Ice giant',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 14,
},
{
id: 5,
Expand All @@ -54,6 +58,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 0,
},
{
id: 6,
Expand All @@ -62,6 +67,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 0,
},
{
id: 7,
Expand All @@ -70,6 +76,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 1,
},
{
id: 8,
Expand All @@ -78,6 +85,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 2,
},
{
id: 9,
Expand All @@ -86,6 +94,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Gas giant',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 95,
},
{
id: 10,
Expand All @@ -94,6 +103,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Gas giant',
categories: [ 'Space', 'Planet', 'Solar system' ],
satellites: 146,
},
{
id: 11,
Expand All @@ -102,6 +112,7 @@ export const data = [
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Ice giant',
categories: [ 'Space', 'Ice giant', 'Solar system' ],
satellites: 28,
},
];

Expand Down Expand Up @@ -178,6 +189,11 @@ export const fields = [
{ value: 'Gas giant', label: 'Gas giant' },
],
},
{
header: 'Satellites',
id: 'satellites',
enableSorting: true,
},
{
header: 'Description',
id: 'description',
Expand Down
17 changes: 16 additions & 1 deletion packages/dataviews/src/test/filter-and-sort-data-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe( 'filters', () => {
} );

describe( 'sorting', () => {
it( 'should sort', () => {
it( 'should sort by string', () => {
const { data: result } = filterSortAndPaginate(
data,
{
Expand All @@ -252,6 +252,21 @@ describe( 'sorting', () => {
expect( result[ 0 ].title ).toBe( 'Uranus' );
expect( result[ 1 ].title ).toBe( 'Neptune' );
} );

it( 'should sort by number', () => {
const { data: result } = filterSortAndPaginate(
data,
{
sort: { field: 'satellites', direction: 'desc' },
},
fields
);

expect( result ).toHaveLength( 11 );
expect( result[ 0 ].title ).toBe( 'Saturn' );
expect( result[ 1 ].title ).toBe( 'Jupiter' );
expect( result[ 2 ].title ).toBe( 'Uranus' );
} );
} );

describe( 'pagination', () => {
Expand Down

0 comments on commit 6034e90

Please sign in to comment.