-
Notifications
You must be signed in to change notification settings - Fork 839
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
[DataGrid] Enhance comparator signature to support advanced use cases #7406
Changes from all commits
861fde2
3ae64e8
72231f5
21a3c72
54d4ab0
9f9e854
0da563f
f420243
08284c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Updated `EuiDataGridSchemaDetector`'s comparator arguments to include entry indexes |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I might actually open a new PR for this. I think we should have typed this as
Suggested change
to make it clearer it's extra information that's optional for developer usage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦 Jumped the gun on the merge, my bad! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a developer I read that as A similar approach is used also for all JS filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[]; The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh yeah that's a super good point about the ts |
||||||
) => -1 | 0 | 1; | ||||||
/** | ||||||
* The icon used to visually represent this data type. Accepts any `EuiIcon IconType`. | ||||||
*/ | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for creating this test file! ❤️ You rock Marco, the test looks really great! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the PR has already merged, but just curious - from a developer experience perspective, do you think the type/prop docs are sufficient, or should we add/update an existing EuiDataGrid documentation example as well?