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: switch to onClick from onMouseDown in modal components #16847

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,20 @@ describe('ComposedModal', () => {
expect(container.firstChild).toHaveClass(`${prefix}--modal--slug`);
});
});

it('should handle onClick events', async () => {
const onClick = jest.fn();
render(
<ComposedModal open onClick={onClick}>
<p>
Custom domains direct requests for your apps in this Cloud Foundry
organization to a URL that you own. A custom domain can be a shared
domain, a shared subdomain, or a shared domain and host.
</p>
</ComposedModal>
);
const modal = screen.getByRole('dialog');
await userEvent.click(modal);
expect(onClick).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import wrapFocus, {
import { usePrefix } from '../../internal/usePrefix';
import { keys, match } from '../../internal/keyboard';
import { useFeatureFlag } from '../FeatureFlags';
import { composeEventHandlers } from '../../tools/events';

export interface ModalBodyProps extends HTMLAttributes<HTMLDivElement> {
/** Specify the content to be placed in the ModalBody. */
Expand Down Expand Up @@ -288,7 +289,7 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
onKeyDown?.(event);
}

function handleMousedown(evt: React.MouseEvent<HTMLDivElement>) {
function handleOnClick(evt: React.MouseEvent<HTMLDivElement>) {
const target = evt.target as Node;
evt.stopPropagation();
if (
Expand Down Expand Up @@ -423,7 +424,7 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
ref={ref}
aria-hidden={!open}
onBlur={!focusTrapWithoutSentinels ? handleBlur : () => {}}
onMouseDown={handleMousedown}
onClick={composeEventHandlers([rest?.onClick, handleOnClick])}
onKeyDown={handleKeyDown}
className={modalClass}>
<div
Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/components/Modal/Modal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,22 @@ describe('events', () => {
expect(onRequestClose).toHaveBeenCalled();
});

it('should handle onClick events', async () => {
const onClick = jest.fn();
render(
<Modal open onClick={onClick}>
<p>
Custom domains direct requests for your apps in this Cloud Foundry
organization to a URL that you own. A custom domain can be a shared
domain, a shared subdomain, or a shared domain and host.
</p>
</Modal>
);
const modal = screen.getByRole('dialog');
await userEvent.click(modal);
expect(onClick).toHaveBeenCalled();
});

it('should handle submit keyDown events with shouldSubmitOnEnter enabled', async () => {
const onRequestSubmit = jest.fn();
render(
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Text } from '../Text';
import { ReactAttr } from '../../types/common';
import { InlineLoadingStatus } from '../InlineLoading/InlineLoading';
import { useFeatureFlag } from '../FeatureFlags';
import { composeEventHandlers } from '../../tools/events';

const getInstanceId = setupGetInstanceId();

Expand Down Expand Up @@ -312,7 +313,7 @@ const Modal = React.forwardRef(function Modal(
}
}

function handleMousedown(evt: React.MouseEvent<HTMLDivElement>) {
function handleOnClick(evt: React.MouseEvent<HTMLDivElement>) {
const target = evt.target as Node;
evt.stopPropagation();
if (
Expand Down Expand Up @@ -586,7 +587,7 @@ const Modal = React.forwardRef(function Modal(
{...rest}
level={0}
onKeyDown={handleKeyDown}
onMouseDown={handleMousedown}
onClick={composeEventHandlers([rest?.onClick, handleOnClick])}
onBlur={!focusTrapWithoutSentinels ? handleBlur : () => {}}
className={modalClasses}
role="presentation"
Expand Down
Loading