Skip to content

Commit

Permalink
Fix Popconfirm not response, close ant-design#4606
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Jan 14, 2017
1 parent fc32c75 commit 55da11d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
30 changes: 30 additions & 0 deletions components/popconfirm/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { mount } from 'enzyme';
import Popconfirm from '..';

describe('Popconfirm', () => {
it('should popup Popconfirm dialog', () => {
const onVisibleChange = jest.fn();

const wrapper = mount(
<Popconfirm
title={<span className="popconfirm-test">Are you sure delete this task?</span>}
okText="Yes"
cancelText="No"
mouseEnterDelay={0}
mouseLeaveDelay={0}
onVisibleChange={onVisibleChange}
>
<span>Delete</span>
</Popconfirm>
);

const triggerNode = wrapper.find('span').at(0);
triggerNode.simulate('click');
expect(onVisibleChange).toBeCalledWith(true);
expect(document.querySelectorAll('.popconfirm-test').length).toBe(1);

triggerNode.simulate('click');
expect(onVisibleChange).toBeCalledWith(false);
});
});
13 changes: 6 additions & 7 deletions components/tooltip/__tests__/tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,26 @@ describe('Tooltip', () => {
expect(onVisibleChange).not.toHaveBeenCalled();
expect(wrapper.ref('tooltip').prop('visible')).toBe(false);


// update `title` value.
wrapper.setProps({ title: 'Have a nice day!' });
wrapper.simulate('mouseenter');
expect(onVisibleChange).toBeCalledWith(true); // mock.calls.length++
expect(onVisibleChange).toHaveBeenLastCalledWith(true);
expect(wrapper.ref('tooltip').prop('visible')).toBe(true);

wrapper.simulate('mouseleave');
expect(onVisibleChange).toBeCalledWith(false); // mock.calls.length++
expect(onVisibleChange).toHaveBeenLastCalledWith(false);
expect(wrapper.ref('tooltip').prop('visible')).toBe(false);


// add `visible` props.
wrapper.setProps({ visible: false });
wrapper.simulate('mouseenter');
expect(onVisibleChange.mock.calls.length).toBe(2);
expect(onVisibleChange).toHaveBeenLastCalledWith(true);
const lastCount = onVisibleChange.mock.calls.length;
expect(wrapper.ref('tooltip').prop('visible')).toBe(false);

// always trigger onVisibleChange
wrapper.simulate('mouseleave');
expect(onVisibleChange.mock.calls.length).toBe(2);
expect(onVisibleChange.mock.calls.length).toBe(lastCount); // no change with lastCount
expect(wrapper.ref('tooltip').prop('visible')).toBe(false);
});
});

26 changes: 12 additions & 14 deletions components/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,12 @@ export default class Tooltip extends React.Component<TooltipProps, any> {
}

onVisibleChange = (visible) => {
const { props, state } = this;
const { title, overlay, onVisibleChange } = props;

if (!('visible' in props)) {
if (!title && !overlay) {
visible = false;
}

this.setState({ visible });

if (onVisibleChange && (visible !== state.visible)) {
onVisibleChange(visible);
}
const { onVisibleChange } = this.props;
if (!('visible' in this.props)) {
this.setState({ visible: this.isNoTitle() ? false : visible });
}
if (onVisibleChange && !this.isNoTitle()) {
onVisibleChange(visible);
}
}

Expand All @@ -90,6 +83,11 @@ export default class Tooltip extends React.Component<TooltipProps, any> {
});
}

isNoTitle() {
const { title, overlay } = this.props;
return !title && !overlay; // overlay for old version compatibility
}

// 动态设置动画点
onPopupAlign = (domNode, align) => {
const placements = this.getPlacements();
Expand Down Expand Up @@ -128,7 +126,7 @@ export default class Tooltip extends React.Component<TooltipProps, any> {
const children = props.children as React.ReactElement<any>;
let visible = state.visible;
// Hide tooltip when there is no title
if (!('visible' in props) && !title && !overlay) {
if (!('visible' in props) && this.isNoTitle()) {
visible = false;
}

Expand Down

0 comments on commit 55da11d

Please sign in to comment.