Skip to content

Commit 23207f0

Browse files
authored
fix: stop mutating the mask config object passed by the user (#59233)
`normalizeMaskConfig` returned the caller's `mask` object when it was a plain object, and `ConfirmDialog` and `useMergedMask` then wrote `closable` onto it. A `mask` object that is not re-created on every render therefore keeps the first `closable` value, so reusing it on another Modal or Drawer silently disables closing on mask click. Copy the object so the fill stays local.
1 parent e825c6d commit 23207f0

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎components/_util/hooks/useMergedMask.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const normalizeMaskConfig = (mask?: MaskType, maskClosable?: boolean): Ma
1313
let maskConfig: MaskConfig = {};
1414

1515
if (isPlainObject(mask)) {
16-
maskConfig = mask;
16+
// Copy so that filling `closable` below never writes back to the user's `mask` object
17+
maskConfig = { ...mask };
1718
}
1819
if (typeof mask === 'boolean') {
1920
maskConfig = {

‎components/modal/__tests__/confirm.test.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { warning } from '@rc-component/util';
44

55
import type { ModalFuncProps } from '..';
66
import Modal from '..';
7+
import type { MaskType } from '../../_util/hooks';
78
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
89
import App from '../../app';
910
import ConfigProvider, { defaultPrefixCls } from '../../config-provider';
@@ -1336,6 +1337,28 @@ describe('Modal.confirm triggers callbacks correctly', () => {
13361337
expect(document.querySelector('.custom-error-icon')).toBeTruthy();
13371338
});
13381339

1340+
it('should not mutate the mask config object', async () => {
1341+
const mask: MaskType = { blur: true };
1342+
await open({ mask });
1343+
expect(mask).toEqual({ blur: true });
1344+
});
1345+
1346+
it('should keep mask closable when the mask config object is reused', async () => {
1347+
const mask: MaskType = { blur: true };
1348+
await open({ mask });
1349+
Modal.destroyAll();
1350+
await waitFakeTimer();
1351+
1352+
const onCancel = jest.fn();
1353+
render(<Modal open mask={mask} onCancel={onCancel} />);
1354+
await waitFakeTimer();
1355+
1356+
const wrap = $$('.ant-modal-wrap')[0];
1357+
fireEvent.mouseDown(wrap);
1358+
fireEvent.click(wrap);
1359+
expect(onCancel).toHaveBeenCalled();
1360+
});
1361+
13391362
it('should be able to config warning icon', async () => {
13401363
const Confirm = () => {
13411364
const { modal } = App.useApp();

0 commit comments

Comments
 (0)