Popconfirm#
反馈
@rockshin/tao-ui在触发器附近确认一个简短且有风险的操作。
#何时使用
删除、离开或覆盖等操作需要轻量确认时使用 Popconfirm。
推荐
- 标题直接说明将要发生的动作。
- 不可逆后果放入说明。
避免
- 复杂决策或大量说明应使用 Modal。
#代码演示
#基础用法
TSX
import { useState } from 'react';
import { Button, Popconfirm } from '@rockshin/tao-ui';
function PopconfirmExample() {
const [feedback, setFeedback] = useState('');
return (
<>
<Popconfirm
title="Delete this item?"
description="This action cannot be undone."
onConfirm={() => setFeedback('confirmed')}
>
<Button variant="destructive">Delete</Button>
</Popconfirm>
<output
aria-live="polite"
style={{
display: 'block',
marginTop: 12,
whiteSpace: 'pre-wrap',
overflowWrap: 'anywhere',
}}
>
{feedback}
</output>
</>
);
}
export default PopconfirmExample;#说明与自定义文案
TSX
import { useState } from 'react';
import { Button, Popconfirm, Stack } from '@rockshin/tao-ui';
function PopconfirmVariants() {
const [feedback, setFeedback] = useState('');
return (
<>
<Stack direction="horizontal" gap="md">
<Popconfirm
title="Delete item?"
description="This action cannot be undone."
onConfirm={() => setFeedback('delete')}
>
<Button variant="destructive">Delete</Button>
</Popconfirm>
<Popconfirm
title="Leave this page?"
description="Unsaved changes will be lost."
okText="Leave"
cancelText="Stay"
onConfirm={() => setFeedback('leave')}
>
<Button variant="secondary">Leave page</Button>
</Popconfirm>
</Stack>
<output
aria-live="polite"
style={{
display: 'block',
marginTop: 12,
whiteSpace: 'pre-wrap',
overflowWrap: 'anywhere',
}}
>
{feedback}
</output>
</>
);
}
export default PopconfirmVariants;#按钮属性与禁用状态
TSX
import { Button, Popconfirm, Stack } from '@rockshin/tao-ui';
function PopconfirmStates() {
return (
<Stack direction="horizontal" gap="md">
<Popconfirm
title="Archive project?"
okButtonProps={{ loading: true }}
cancelButtonProps={{ disabled: true }}
>
<Button variant="secondary">Loading confirm</Button>
</Popconfirm>
<Popconfirm title="Unavailable" disabled>
<Button disabled>Disabled</Button>
</Popconfirm>
</Stack>
);
}
export default PopconfirmStates;#确认与取消反馈
明确处理两个回调,在页面内显示用户选择。
TSX
import { useState } from 'react';
import { Popconfirm, Button, Stack } from '@rockshin/tao-ui';
export default function Example() {
const [result, setResult] = useState('No decision yet');
return (
<Stack direction="vertical">
<Popconfirm
title="Archive this item?"
description="This is a local demonstration."
onConfirm={() => setResult('Archived')}
onCancel={() => setResult('Kept')}
>
<Button>Archive</Button>
</Popconfirm>
<output aria-live="polite">{result}</output>
</Stack>
);
}#条件禁用
disabled 阻止弹层打开,独立开关演示可操作条件。
TSX
import { useState } from 'react';
import { Popconfirm, Button, Switch, Stack } from '@rockshin/tao-ui';
export default function Example() {
const [enabled, setEnabled] = useState(false);
return (
<Stack direction="vertical">
<Switch
aria-label="Allow archive"
checked={enabled}
onChange={setEnabled}
/>
<Popconfirm title="Archive item?" disabled={!enabled}>
<Button disabled={!enabled}>Archive item</Button>
</Popconfirm>
</Stack>
);
}#位置与图标
placement 设定初始方向,icon=null 可隐藏默认警告图标。
TSX
import { Popconfirm, Button } from '@rockshin/tao-ui';
export default function Example() {
return (
<Popconfirm
title="Apply changes?"
placement="bottomRight"
icon={null}
okText="Apply"
cancelText="Keep editing"
>
<Button>Apply changes</Button>
</Popconfirm>
);
}#API
#Popconfirm
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| title | Main question text of the confirm popup (required, antd parity). | ReactNode (必填) | - |
| description | Secondary text shown under the title. | ReactNode | - |
| onConfirm | Called when OK is clicked; the popup also closes. | ((e?: MouseEvent<HTMLElement, MouseEvent> | undefined) => void) | undefined | - |
| onCancel | Called when Cancel is clicked; the popup also closes. | ((e?: MouseEvent<HTMLElement, MouseEvent> | undefined) => void) | undefined | - |
| okText | Text of the OK button. Defaults to 'OK'. | ReactNode | - |
| cancelText | Text of the Cancel button. Defaults to 'Cancel'. | ReactNode | - |
| okButtonProps | Extra props forwarded to the OK button. | ButtonProps | undefined | - |
| cancelButtonProps | Extra props forwarded to the Cancel button. | ButtonProps | undefined | - |
| icon | Icon shown before the title. Defaults to a warning triangle; pass null to hide it. | ReactNode | - |
| disabled | When true, the trigger still renders but the popup never opens. | boolean | undefined | - |
| placement | Popup side relative to the trigger. Defaults to 'top'. | PopoverPlacement | undefined | top |
| className | Merged onto the confirm content wrapper. | string | undefined | - |
| style | 节点样式 | CSSProperties | undefined | - |
| children | Exactly one trigger element. | ReactNode (必填) | - |