Skip to content

Commit d8d9f67

Browse files
authored
fix: update compareItems type in MultiSelect (#18599)
* fix: update compareItems type in MultiSelect * fix: update compareItems type in SortItemsOptions
1 parent babf238 commit d8d9f67

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

packages/react/src/components/MultiSelect/MultiSelectPropTypes.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ interface SharedOptions {
4646
locale: string;
4747
}
4848

49+
interface CompareItems {
50+
(itemA: string, itemB: string, options: SharedOptions): number;
51+
}
52+
4953
export interface SortItemsOptions<ItemType>
5054
extends SharedOptions,
5155
DownshiftTypedProps<ItemType> {
52-
compareItems(
53-
item1: ItemType,
54-
item2: ItemType,
55-
options: SharedOptions
56-
): number;
56+
compareItems: CompareItems;
5757
selectedItems: ItemType[];
5858
}
5959

@@ -62,11 +62,7 @@ export interface MultiSelectSortingProps<ItemType> {
6262
* Provide a compare function that is used to determine the ordering of
6363
* options. See 'sortItems' for more control.
6464
*/
65-
compareItems?(
66-
item1: ItemType,
67-
item2: ItemType,
68-
options: SharedOptions
69-
): number;
65+
compareItems?: CompareItems;
7066

7167
/**
7268
* Provide a method that sorts all options in the control. Overriding this

packages/react/src/components/MultiSelect/tools/__tests__/sorting-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,17 @@ describe('defaultSortItems', () => {
128128
]);
129129
});
130130
});
131+
132+
describe('defaultCompareItems', () => {
133+
it('should return a negative number if the first string comes before the second', () => {
134+
expect(defaultCompareItems('a', 'b', { locale: 'en' })).toBeLessThan(0);
135+
});
136+
137+
it('should return a positive number if the first string comes after the second', () => {
138+
expect(defaultCompareItems('z', 'y', { locale: 'en' })).toBeGreaterThan(0);
139+
});
140+
141+
it('should return 0 if both strings are equal', () => {
142+
expect(defaultCompareItems('same', 'same', { locale: 'en' })).toEqual(0);
143+
});
144+
});

packages/react/src/components/MultiSelect/tools/sorting.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
*/
77

88
/**
9-
* Use the local `localCompare` with the `numeric` option to sort two,
10-
* potentially alpha-numeric, strings in a list of items.
9+
* Use the locale `localeCompare` with the `numeric` option to sort two
10+
* alpha-numeric strings.
1111
*
12-
* @param {ItemType} itemA
13-
* @param {ItemType} itemB
14-
* @param {object} options
15-
* @param {string} options.locale
16-
* @returns {number}
12+
* @param {string} itemA - The first string to compare.
13+
* @param {string} itemB - The second string to compare.
14+
* @param {object} options - Options for comparing.
15+
* @param {string} options.locale - The locale to use for comparison.
16+
* @returns {number} A negative number if itemA comes before itemB, a positive
17+
* number if itemA comes after itemB, or 0 if they are equal.
1718
*/
1819
export const defaultCompareItems = (itemA, itemB, { locale }) =>
1920
itemA.localeCompare(itemB, locale, {

0 commit comments

Comments
 (0)