Skip to content
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

feat(ChipsSelect): add prop sortFn #8119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/vkui/src/components/ChipsSelect/ChipsSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ describe('ChipsSelect', () => {
expect(within(dropdownLocator).getByRole('option', { name: 'Синий' })).toBeTruthy();
});

it('should sort options by sortFn prop', async () => {
type Option = { label: string; value: number };
const options: Option[] = [
{ label: '1', value: 1 },
{ label: '3', value: 3 },
{ label: '2', value: 2 },
];
const byAsc = (a: Option, b: Option) => a.label.localeCompare(b.label);
const byDesc = (a: Option, b: Option) => b.label.localeCompare(a.label);

const checkOptionsOrder = async (order: string[]) => {
await userEvent.click(screen.getByRole('combobox'));
const dropdownLocator = screen.getByTestId('dropdown');
const optionsValues = within(dropdownLocator)
.getAllByRole('option')
.map((element) => element.textContent);
expect(optionsValues).toEqual(order);
};

// Сортируем по возрастанию
const { rerender } = render(
<ChipsSelect options={options} defaultValue={[]} sortFn={byAsc} dropdownTestId="dropdown" />,
);
await checkOptionsOrder(['1', '2', '3']);

// Сортируем по убыванию
rerender(
<ChipsSelect options={options} defaultValue={[]} sortFn={byDesc} dropdownTestId="dropdown" />,
);
await checkOptionsOrder(['3', '2', '1']);
});

it('shows spinner if fetching', async () => {
const result = render(<ChipsSelect fetching defaultValue={[]} dropdownTestId="dropdown" />);
await userEvent.click(result.getByRole('combobox'));
Expand Down
2 changes: 2 additions & 0 deletions packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const ChipsSelect = <Option extends ChipOption>({
forceDropdownPortal,
noMaxHeight = false,
filterFn = defaultFilterFn,
sortFn = false,
dropdownTestId,
onClose,
onOpen,
Expand Down Expand Up @@ -231,6 +232,7 @@ export const ChipsSelect = <Option extends ChipOption>({
emptyText,
creatable,
filterFn,
sortFn,
selectedBehavior,
onClose,
onOpen,
Expand Down
13 changes: 12 additions & 1 deletion packages/vkui/src/components/ChipsSelect/useChipsSelect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { isEqual } from '@vkontakte/vkjs';
import { type SimulateReactInputTargetState } from '../../lib/react';
import { defaultFilterFn, type FilterFn } from '../../lib/select';
import { defaultFilterFn, type FilterFn, type SortFn } from '../../lib/select';
import { useIsomorphicLayoutEffect } from '../../lib/useIsomorphicLayoutEffect';
import {
transformValue,
Expand Down Expand Up @@ -39,6 +39,7 @@
*/
selectedBehavior?: 'hide' | 'highlight';
filterFn?: false | FilterFn<O>;
sortFn?: false | SortFn<O>;
/**
* Будет вызвано в момент скрытия выпадающего списка
*/
Expand Down Expand Up @@ -70,6 +71,7 @@
creatable = false,
emptyText = DEFAULT_EMPTY_TEXT,
filterFn = defaultFilterFn,
sortFn = false,

Check warning on line 74 in packages/vkui/src/components/ChipsSelect/useChipsSelect.ts

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/useChipsSelect.ts#L74

Added line #L74 was not covered by tests
selectedBehavior = DEFAULT_SELECTED_BEHAVIOR,
options: optionsProp = DEFAULT_VALUE,
onClose,
Expand Down Expand Up @@ -130,6 +132,7 @@
emptyText,
creatable,
filterFn,
sortFn,
options: optionsProp,
selectedBehavior,
})
Expand Down Expand Up @@ -173,6 +176,7 @@
emptyText,
creatable,
filterFn,
sortFn,
options: optionsProp,
selectedBehavior,
});
Expand All @@ -194,6 +198,7 @@
creatable,
selectedBehavior,
filterFn,
sortFn,
],
);

Expand Down Expand Up @@ -225,6 +230,7 @@
inputValue = DEFAULT_INPUT_VALUE,
emptyText = DEFAULT_EMPTY_TEXT,
creatable = false,
sortFn = false,

Check warning on line 233 in packages/vkui/src/components/ChipsSelect/useChipsSelect.ts

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/useChipsSelect.ts#L233

Added line #L233 was not covered by tests
filterFn = defaultFilterFn,
options: optionsProp = DEFAULT_VALUE,
selectedBehavior = DEFAULT_SELECTED_BEHAVIOR,
Expand All @@ -237,6 +243,7 @@
| 'emptyText'
| 'creatable'
| 'filterFn'
| 'sortFn'
| 'options'
| 'selectedBehavior'
>) {
Expand All @@ -251,6 +258,10 @@
return [{ placeholder: emptyText }];
}

if (sortFn) {
filteredOptionsProp.sort((optionA, optionB) => sortFn(optionA, optionB, inputValue));
}

const parsedOptions = transformValue(filteredOptionsProp, getOptionValue, getOptionLabel);

if (selectedBehavior === 'hide') {
Expand Down
2 changes: 2 additions & 0 deletions packages/vkui/src/lib/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type FilterFn<O extends Option> = (
getOptionsLabel?: GetOptionLabel<O>,
) => boolean;

export type SortFn<O extends Option> = (optionA: O, optionB: O, inputValue: string) => number;

function getOptionLabelDefault<O extends Option>(option: O): O['label'] {
return option.label;
}
Expand Down
Loading