确认弹窗使用场景:表格,列表删除时的确认

DeleteConfirm 删除弹窗

image.png

  1. import React from 'react';
  2. import { Popconfirm, Typography } from 'antd';
  3. import { QuestionCircleOutlined, DeleteOutlined } from '@ant-design/icons';
  4. type IProps = {
  5. title: string;
  6. text?: string;
  7. onConfirm: () => Promise<void>;
  8. children?: React.ReactNode;
  9. }
  10. const DeleteConfirm: React.FC<IProps> = (props) => {
  11. const { title = '', text = '删除', children, onConfirm } = props;
  12. const element = children ?? (
  13. <Typography.Text type="danger">
  14. <DeleteOutlined/> {text}
  15. </Typography.Text>
  16. );
  17. return (
  18. <Popconfirm
  19. title={`确认是否${text}该${title}`}
  20. icon={
  21. <QuestionCircleOutlined style={{ color: 'red' }} />
  22. }
  23. okText={text}
  24. onConfirm={onConfirm}
  25. >
  26. {element}
  27. </Popconfirm>
  28. )
  29. };
  30. export default DeleteConfirm;

DeleteConfirm

image.png

  1. import { Button } from 'antd';
  2. import { DeleteConfirm } from '@/components';
  3. function App() {
  4. return (
  5. <DeleteConfirm>
  6. <Button
  7. danger
  8. size='small'
  9. onClick={e => e.preventDefault()}
  10. >
  11. 删除
  12. </Button>
  13. </DeleteConfirm>
  14. )
  15. }

TableDropdown

表格删除时的确认
https://procomponents.ant.design/components/table#tabledropdown
image.pngimage.png

  1. import { ProTable, TableDropdown } from '@ant-design/pro-components';
  2. const action = [
  3. {
  4. title: "操作",
  5. valueType: 'option',
  6. key: 'option',
  7. width: 120,
  8. fixed: 'right',
  9. render: renderAction,
  10. }
  11. ]
  12. export function renderAction(text, record, _, action) {
  13. return [
  14. <a
  15. key="editable"
  16. onClick={() => {
  17. action?.startEditable?.(record.id);
  18. }}
  19. rel="noopener noreferrer"
  20. >
  21. 编辑
  22. </a>,
  23. <a href={record.url} target="_blank" rel="noopener noreferrer" key="view">
  24. 查看
  25. </a>,
  26. <TableDropdown
  27. key="actions"
  28. onSelect={() => action?.reload()}
  29. menus={[
  30. { key: 'copy', name: '复制' },
  31. { key: 'delete', name: <DeleteConfirm title="用户" onConfirm={onConfirm}/>}
  32. // { key: 'delete', name: '删除' },
  33. ]}
  34. />,
  35. ]
  36. }