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(ActionSheet, Alert): add stopPropagation ignore #8166

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 36 additions & 0 deletions packages/vkui/src/components/ActionSheet/ActionSheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,40 @@ describe(ActionSheet, () => {
// desktop Android
expect(screen.queryByText('Отмена')).toBeFalsy();
});

describe('handle allowClickPropagation correctly', () => {
it.each([
['menu', ActionSheetMenu],
['sheet', ActionSheetSheet],
])('%s', async (_name, ActionSheet) => {
const onClose = jest.fn();
const onClick = jest.fn();
const { rerender } = render(
<div onClick={onClick}>
<ActionSheet data-testid="container" onClose={onClose}>
<div data-testid="content" />
</ActionSheet>
</div>,
);
await waitForFloatingPosition();
act(jest.runAllTimers);

await userEvent.click(screen.getByTestId('content'));
expect(onClick).not.toHaveBeenCalled();

rerender(
<div onClick={onClick}>
<ActionSheet data-testid="container" onClose={onClose} allowClickPropagation>
<div data-testid="content" />
</ActionSheet>
</div>,
);

await waitForFloatingPosition();
act(jest.runAllTimers);

await userEvent.click(screen.getByTestId('content'));
expect(onClick).toHaveBeenCalled();
});
});
});
5 changes: 4 additions & 1 deletion packages/vkui/src/components/ActionSheet/ActionSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export interface ActionSheetOnCloseOptions {
}

export interface ActionSheetProps
extends Pick<SharedDropdownProps, 'toggleRef' | 'popupOffsetDistance' | 'placement'>,
extends Pick<
SharedDropdownProps,
'toggleRef' | 'popupOffsetDistance' | 'placement' | 'allowClickPropagation'
>,
Omit<UseFocusTrapProps, 'onClose'>,
Omit<React.HTMLAttributes<HTMLDivElement>, 'autoFocus' | 'title'> {
title?: React.ReactNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { useEventListener } from '../../hooks/useEventListener';
import { usePlatform } from '../../hooks/usePlatform';
import { useDOM } from '../../lib/dom';
import { isRefObject } from '../../lib/isRefObject';
import { mergeCalls } from '../../lib/mergeCalls';
import { stopPropagation } from '../../lib/utils';
import { warnOnce } from '../../lib/warnOnce';
import { FocusTrap } from '../FocusTrap/FocusTrap';
import { Popper } from '../Popper/Popper';
Expand All @@ -30,6 +32,8 @@ export const ActionSheetDropdownMenu = ({
placement,
onAnimationStart,
onAnimationEnd,
allowClickPropagation = false,
onClick,
...restProps
}: SharedDropdownProps): React.ReactNode => {
const { document } = useDOM();
Expand Down Expand Up @@ -57,8 +61,6 @@ export const ActionSheetDropdownMenu = ({
});
}, [bodyClickListener, document]);

const onClick = React.useCallback((e: React.MouseEvent<HTMLElement>) => e.stopPropagation(), []);

const targetRef = React.useMemo(() => {
if (isRefObject<SharedDropdownProps['toggleRef'], HTMLElement>(toggleRef)) {
return toggleRef;
Expand All @@ -67,6 +69,14 @@ export const ActionSheetDropdownMenu = ({
return { current: toggleRef as HTMLElement };
}, [toggleRef]);

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
if (!allowClickPropagation) {
stopPropagation(event);
}
};

const clickHandlers = mergeCalls({ onClick: handleClick }, { onClick });

return (
<Popper
targetRef={targetRef}
Expand All @@ -86,7 +96,7 @@ export const ActionSheetDropdownMenu = ({
onAnimationStart={onAnimationStart}
onAnimationEnd={onAnimationEnd}
>
<FocusTrap onClose={onClose} {...restProps} onClick={onClick}>
<FocusTrap onClose={onClose} {...restProps} {...clickHandlers}>
{children}
</FocusTrap>
</Popper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { useAdaptivityWithJSMediaQueries } from '../../hooks/useAdaptivityWithJSMediaQueries';
import { usePlatform } from '../../hooks/usePlatform';
import { mergeCalls } from '../../lib/mergeCalls';
import { stopPropagation } from '../../lib/utils';
import { FocusTrap } from '../FocusTrap/FocusTrap';
import type { SharedDropdownProps } from './types';
import styles from './ActionSheet.module.css';

const stopPropagation: React.MouseEventHandler = (e) => e.stopPropagation();

export type ActionSheetDropdownProps = Omit<
SharedDropdownProps,
'popupDirection' | 'popupOffsetDistance' | 'placement'
Expand All @@ -21,15 +21,25 @@ export const ActionSheetDropdownSheet = ({
// these 2 props are only omitted - ActionSheetDesktop compat
toggleRef,
className,
onClick,
allowClickPropagation = false,
...restProps
}: SharedDropdownProps): React.ReactNode => {
const { sizeY } = useAdaptivityWithJSMediaQueries();
const platform = usePlatform();

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
if (!allowClickPropagation) {
stopPropagation(event);
}
};

const clickHandlers = mergeCalls({ onClick: handleClick }, { onClick });

return (
<FocusTrap
{...restProps}
onClick={stopPropagation}
{...clickHandlers}
className={classNames(
styles.host,
platform === 'ios' && styles.ios,
Expand Down
4 changes: 4 additions & 0 deletions packages/vkui/src/components/ActionSheet/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export interface SharedDropdownProps extends FocusTrapProps {
* Отступ, где заданное кол-во единиц равняется пикселям
* */
popupOffsetDistance?: number;
/**
* По умолчанию событие onClick не всплывает
*/
allowClickPropagation?: boolean;
}
28 changes: 28 additions & 0 deletions packages/vkui/src/components/Alert/Alert.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,32 @@ describe('Alert', () => {
descriptionClassNames.forEach((className) => expect(textElement).toHaveClass(className));
},
);

it('handle allowClickPropagation correctly', async () => {
const onClose = jest.fn();
const onClick = jest.fn();
const action = {
'title': 'Item',
'data-testid': '__action__',
'autoCloseDisabled': true,
'mode': 'default' as const,
};
const result = render(
<div onClick={onClick}>
<Alert onClose={onClose} actions={[action]} />
</div>,
);

await userEvent.click(result.getByTestId('__action__'));
expect(onClick).not.toHaveBeenCalled();

result.rerender(
<div onClick={onClick}>
<Alert onClose={onClose} actions={[action]} allowClickPropagation />
</div>,
);

await userEvent.click(result.getByTestId('__action__'));
expect(onClick).toHaveBeenCalledTimes(1);
});
});
17 changes: 16 additions & 1 deletion packages/vkui/src/components/Alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useAdaptivityWithJSMediaQueries } from '../../hooks/useAdaptivityWithJS
import { type UseFocusTrapProps } from '../../hooks/useFocusTrap';
import { usePlatform } from '../../hooks/usePlatform';
import { useCSSKeyframesAnimationController } from '../../lib/animation';
import { mergeCalls } from '../../lib/mergeCalls';
import { stopPropagation } from '../../lib/utils';
import type {
AlignType,
Expand Down Expand Up @@ -73,6 +74,10 @@ export interface AlertProps
*/
dismissButtonTestId?: string;
usePortal?: AppRootPortalProps['usePortal'];
/**
* По умолчанию событие onClick не всплывает
*/
allowClickPropagation?: boolean;
}

/**
Expand All @@ -94,6 +99,8 @@ export const Alert = ({
dismissButtonTestId,
getRootRef,
usePortal,
onClick,
allowClickPropagation = false,
...restProps
}: AlertProps): React.ReactNode => {
const generatedId = React.useId();
Expand Down Expand Up @@ -139,6 +146,14 @@ export const Alert = ({
[close],
);

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
if (!allowClickPropagation) {
stopPropagation(event);
}
};

const clickHandlers = mergeCalls({ onClick: handleClick }, { onClick });
inomdzhon marked this conversation as resolved.
Show resolved Hide resolved

useScrollLock();

return (
Expand All @@ -153,8 +168,8 @@ export const Alert = ({
<FocusTrap
{...restProps}
{...animationHandlers}
{...clickHandlers}
getRootRef={elementRef}
onClick={stopPropagation}
onClose={close}
autoFocus={animationState === 'entered'}
className={classNames(
Expand Down
Loading