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

fix(ChipsSelect): add close dropdown when click dropdown icon #7462

Merged
merged 2 commits into from
Aug 28, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ export const ChipsInputBase = <O extends ChipOption>({
};

const handleRootClick = (event: React.MouseEvent<HTMLDivElement>) => {
if (contains(event.currentTarget, getActiveElementByAnotherElement(event.currentTarget))) {
if (
event.defaultPrevented ||
contains(event.currentTarget, getActiveElementByAnotherElement(event.currentTarget))
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
}

.ChipsSelect__dropdown-icon {
cursor: pointer;
}

.ChipsSelect__dropdown-icon--withOffset {
margin-inline-end: 8px;
}
14 changes: 14 additions & 0 deletions packages/vkui/src/components/ChipsSelect/ChipsSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,18 @@ describe('ChipsSelect', () => {
// дропдаун открыт вниз и класс для границ выставлен верно
expect(document.querySelector(`.${styles['ChipsSelect--pop-down']}`)).not.toBeNull();
});

it('check close dropdown when click to dropdown icon', async () => {
const result = render(
<ChipsSelect creatable="Добавить новую опцию" options={[]} dropdownTestId="dropdown" />,
);
const getDropdownIcon = () =>
result.container.getElementsByClassName(styles['ChipsSelect__dropdown-icon'])[0];
expect(screen.queryByTestId('dropdown')).toBeFalsy();
fireEvent.click(getDropdownIcon());
await waitForFloatingPosition();
expect(screen.queryByTestId('dropdown')).toBeTruthy();
fireEvent.click(getDropdownIcon());
expect(screen.queryByTestId('dropdown')).toBeFalsy();
});
});
17 changes: 16 additions & 1 deletion packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type MouseEventHandler } from 'react';
import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { useExternRef } from '../../hooks/useExternRef';
Expand Down Expand Up @@ -426,6 +427,16 @@ export const ChipsSelect = <Option extends ChipOption>({
opened ? dropdownScrollBoxRef : null,
);

const onDropdownIconClick: MouseEventHandler<SVGSVGElement> = React.useCallback(
(e) => {
if (opened) {
e.preventDefault();
setOpened(false);
}
},
[opened, setOpened],
);

const dropdownContent = React.useMemo(() => {
const defaultDropdownContent = options.map((option, index) => {
const dropdownItemId = `${dropdownId}-${index}`;
Expand Down Expand Up @@ -549,7 +560,11 @@ export const ChipsSelect = <Option extends ChipOption>({
dropdownIconProp || (
<DropdownIcon
opened={opened}
className={clearButtonShown ? styles['ChipsSelect__dropdown-icon'] : undefined}
onClick={onDropdownIconClick}
className={classNames(
styles['ChipsSelect__dropdown-icon'],
clearButtonShown && styles['ChipsSelect__dropdown-icon--withOffset'],
)}
/>
)
}
Expand Down