diff --git a/changelogs/upcoming/7406.md b/changelogs/upcoming/7406.md new file mode 100644 index 00000000000..3a621cc0b3f --- /dev/null +++ b/changelogs/upcoming/7406.md @@ -0,0 +1 @@ +- Updated `EuiDataGridSchemaDetector`'s comparator arguments to include entry indexes diff --git a/src/components/datagrid/data_grid_types.ts b/src/components/datagrid/data_grid_types.ts index 07f891a05f2..004e0d2f2a4 100644 --- a/src/components/datagrid/data_grid_types.ts +++ b/src/components/datagrid/data_grid_types.ts @@ -100,9 +100,14 @@ export interface EuiDataGridSchemaDetector { */ detector: (value: string) => number; /** - * A custom comparator function when performing in-memory sorting on this data type, takes `(a: string, b: string, direction: 'asc' | 'desc) => -1 | 0 | 1` - */ - comparator?: (a: string, b: string, direction: 'asc' | 'desc') => -1 | 0 | 1; + * A custom comparator function when performing in-memory sorting on this data type, takes `(a: string, b: string, direction: 'asc' | 'desc', indexes: {aIndex: number, bIndex: number}) => -1 | 0 | 1` + */ + comparator?: ( + a: string, + b: string, + direction: 'asc' | 'desc', + indexes: { aIndex: number; bIndex: number } + ) => -1 | 0 | 1; /** * The icon used to visually represent this data type. Accepts any `EuiIcon IconType`. */ diff --git a/src/components/datagrid/utils/sorting.test.ts b/src/components/datagrid/utils/sorting.test.ts new file mode 100644 index 00000000000..a997f4f5740 --- /dev/null +++ b/src/components/datagrid/utils/sorting.test.ts @@ -0,0 +1,59 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { renderHook } from '@testing-library/react'; +import { useSorting, type useSortingArgs } from './sorting'; + +function getDefaultArgs(): useSortingArgs { + return { + sorting: { + columns: [{ id: '1', direction: 'asc' }], + onSort: jest.fn(), + }, + schema: { '1': { columnType: 'number' } }, + schemaDetectors: [], + startRow: 0, + inMemoryValues: { '0': { '1': '0' }, '1': { '1': '1' } }, + inMemory: { level: 'sorting' }, + }; +} + +function testUseSorting(args: useSortingArgs) { + return renderHook(() => useSorting(args)).result.current; +} + +describe('useSorting', () => { + it('returns null if no sorting object is provided', () => { + expect( + testUseSorting({ ...getDefaultArgs(), schema: {}, inMemoryValues: {} }) + .sortedRowMap + ).toEqual([]); + }); + + it('should pass all arguments to custom comparator fn', () => { + const customComparator = jest.fn(); + testUseSorting({ + ...getDefaultArgs(), + schemaDetectors: [ + { + type: 'number', + detector: () => 1, + comparator: customComparator, + icon: 'empty', + sortTextAsc: 'ASC', + sortTextDesc: 'DESC', + }, + ], + }); + + expect(customComparator).toHaveBeenCalledWith('1', '0', 'asc', { + aIndex: 1, + bIndex: 0, + }); + }); +}); diff --git a/src/components/datagrid/utils/sorting.ts b/src/components/datagrid/utils/sorting.ts index c697b30045c..e8ff79d904f 100644 --- a/src/components/datagrid/utils/sorting.ts +++ b/src/components/datagrid/utils/sorting.ts @@ -24,6 +24,15 @@ export const DataGridSortingContext = getCorrectRowIndex: (number) => number, }); +export type useSortingArgs = { + sorting?: EuiDataGridSorting; + inMemory?: EuiDataGridInMemory; + inMemoryValues: EuiDataGridInMemoryValues; + schema: EuiDataGridSchema; + schemaDetectors: EuiDataGridSchemaDetector[]; + startRow: number; +}; + export const useSorting = ({ sorting, inMemory, @@ -31,14 +40,7 @@ export const useSorting = ({ schema, schemaDetectors, startRow, -}: { - sorting?: EuiDataGridSorting; - inMemory?: EuiDataGridInMemory; - inMemoryValues: EuiDataGridInMemoryValues; - schema: EuiDataGridSchema; - schemaDetectors: EuiDataGridSchemaDetector[]; - startRow: number; -}) => { +}: useSortingArgs) => { const sortingColumns = sorting?.columns; const sortedRowMap = useMemo(() => { @@ -80,7 +82,10 @@ export const useSorting = ({ } } - const result = comparator(aValue, bValue, column.direction); + const result = comparator(aValue, bValue, column.direction, { + aIndex: a.index, + bIndex: b.index, + }); // only return if the columns are unequal, otherwise allow the next sort-by column to run if (result !== 0) return result; }