Browse Source

fix Modal.confirm error when onCancel not set (#5294)

* fix Modal.confirm error when onCancel not set

* add test case
pull/5215/merge
ddcat1115 8 years ago
committed by 偏右
parent
commit
24e6fb4ba6
  1. 59
      components/modal/__tests__/comfirm.test.js
  2. 8
      components/modal/confirm.tsx
  3. 4
      components/modal/demo/confirm.md

59
components/modal/__tests__/comfirm.test.js

@ -0,0 +1,59 @@
import Modal from '..';
const confirm = Modal.confirm;
describe('Modal.confirm triggers callbacks correctly', () => {
function $$(className) {
return document.body.querySelectorAll(className);
}
function open(args) {
confirm({
title: 'Want to delete these items?',
content: 'some descriptions',
...args,
});
}
it('trigger onCancel once when click on cancel button', () => {
const onCancel = jest.fn();
const onOk = jest.fn();
open({
onCancel,
onOk,
});
// first Modal
$$('.ant-btn')[0].click();
expect(onCancel.mock.calls.length).toBe(1);
expect(onOk.mock.calls.length).toBe(0);
});
it('trigger onOk once when click on ok button', () => {
const onCancel = jest.fn();
const onOk = jest.fn();
open({
onCancel,
onOk,
});
// second Modal
$$('.ant-btn-primary')[1].click();
expect(onCancel.mock.calls.length).toBe(0);
expect(onOk.mock.calls.length).toBe(1);
});
it('should allow Modal.comfirm without onCancel been set', () => {
open();
// Third Modal
$$('.ant-btn')[4].click();
// eslint-disable-next-line
expect(console.error.calls).toBe(undefined);
});
it('should allow Modal.comfirm without onOk been set', () => {
open();
// Fourth Modal
$$('.ant-btn-primary')[3].click();
// eslint-disable-next-line
expect(console.error.calls).toBe(undefined);
});
});

8
components/modal/confirm.tsx

@ -35,7 +35,11 @@ export default function confirm(config) {
if (unmountResult && div.parentNode) {
div.parentNode.removeChild(div);
}
props.onCancel(...args);
const triggerCancel = args && args.length &&
args.some(param => param && param.triggerCancel);
if (props.onCancel && triggerCancel) {
props.onCancel(...args);
}
}
let body = (
@ -75,7 +79,7 @@ export default function confirm(config) {
ReactDOM.render(
<Dialog
className={classString}
onCancel={close}
onCancel={close.bind(this, { triggerCancel: true })}
visible
title=""
transitionName="zoom"

4
components/modal/demo/confirm.md

@ -24,7 +24,9 @@ function showConfirm() {
onOk() {
console.log('OK');
},
onCancel() {},
onCancel() {
console.log('Cancel');
},
});
}

Loading…
Cancel
Save