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(Popconfirm): button props propagation #2361

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/popconfirm/Popcontent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import isString from 'lodash/isString';
import classNames from 'classnames';
import { InfoCircleFilledIcon as TdInfoCircleFilledIcon } from 'tdesign-icons-react';
import Button from '../button';
import Button, { ButtonProps } from '../button';
import noop from '../_util/noop';
import useConfig from '../hooks/useConfig';
import useGlobalIcon from '../hooks/useGlobalIcon';
Expand Down Expand Up @@ -71,6 +71,7 @@ const Popcontent = (props: PopconfirmProps & { onClose?: (context: PopconfirmVis
onClose({ e, trigger: 'cancel' });
onCancel({ e });
}}
{...(typeof cancelBtn === 'object' ? { ...(cancelBtn as ButtonProps) } : {})}
uyarn marked this conversation as resolved.
Show resolved Hide resolved
>
{isString(cancelBtn) && cancelBtn}
</Button>
Expand Down Expand Up @@ -99,6 +100,7 @@ const Popcontent = (props: PopconfirmProps & { onClose?: (context: PopconfirmVis
onClose({ e, trigger: 'confirm' });
onConfirm({ e });
}}
{...(typeof confirmBtn === 'object' ? { ...(confirmBtn as ButtonProps) } : {})}
>
{isString(confirmBtn) && confirmBtn}
</Button>
Expand Down
47 changes: 47 additions & 0 deletions src/popconfirm/__tests__/popconfirm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,53 @@ describe('Popconfirm 组件测试', () => {
expect(onConfirmMock).toHaveBeenCalledTimes(1);
});

test('确认/取消按钮透传 null/object 测试', async () => {
const onCancelMock = vi.fn();
const onConfirmMock = vi.fn();
const { getByText, queryByTestId, queryByText, queryAllByRole } = render(
<Popconfirm
placement="top"
content={<div data-testid={testId}>{text}</div>}
onCancel={onCancelMock}
onConfirm={onConfirmMock}
destroyOnClose={false}
// 传入 null
confirmBtn={null}
// 传入 object
cancelBtn={{ onClick: onCancelMock, content: '取消操作', theme: 'danger' }}
>
{triggerElement}
</Popconfirm>,
);

// 鼠标点击前,没有元素存在
const element1 = await waitFor(() => queryByTestId(testId));
expect(element1).toBeNull();

// 模拟鼠标点击
act(() => {
fireEvent.click(getByText(triggerElement));
});

// 鼠标进入后,有元素,而且内容为 popupText, 且 confirmBtn={null} 只有一个按钮
const element2 = await waitFor(() => queryByTestId(testId));
expect(element2).not.toBeNull();

// 测试只有一个按钮
const buttons = await waitFor(() => queryAllByRole('button'));
expect(buttons).toHaveLength(1);

const cancelBtn = await waitFor(() => queryByText('取消操作'));
// 查询 content 验证透传成功
expect(cancelBtn).not.toBeNull();

// 测试点击事件透传成功
act(() => {
fireEvent.click(cancelBtn);
});
expect(onCancelMock).toHaveBeenCalledTimes(1);
});

test('ref test', () => {
render(
<Popconfirm placement="top" content={<div data-testid={testId}>{text}</div>} theme="warning">
Expand Down
Loading