跳到主要内容

Modal 对话框

模态对话框,在不离开当前页面的情况下展示重要信息或请求用户决策。

来源: 直接从 antd 重新导出。完整文档:Ant Design Modal

何时使用

  • 需要用户处理事务,又不希望跳转页面以致打断工作流程时。
  • 需要用户确认某个破坏性操作时。
  • 收集少量用户输入时。

基础用法

import { Modal, Button } from '@vef-framework-react/components';
import { useState } from 'react';

export default function Demo() {
const [open, setOpen] = useState(false);

return (
<>
<Button type="primary" onClick={() => setOpen(true)}>
打开对话框
</Button>
<Modal
title="基础对话框"
open={open}
onOk={() => setOpen(false)}
onCancel={() => setOpen(false)}
>
<p>对话框内容。</p>
</Modal>
</>
);
}

异步确认

import { Modal, Button } from '@vef-framework-react/components';
import { useState } from 'react';

export default function Demo() {
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);

const handleOk = async () => {
setLoading(true);
await saveData();
setLoading(false);
setOpen(false);
};

return (
<>
<Button onClick={() => setOpen(true)}>打开</Button>
<Modal
title="保存更改"
open={open}
onOk={handleOk}
confirmLoading={loading}
onCancel={() => setOpen(false)}
>
<p>确定要保存吗?</p>
</Modal>
</>
);
}

确认对话框

对于简单的确认操作,使用静态方法 Modal.confirm

import { Modal } from '@vef-framework-react/components';

function handleDelete(id: number) {
Modal.confirm({
title: '删除记录',
content: '此操作不可撤销,是否继续?',
okText: '删除',
okType: 'danger',
onOk: () => deleteRecord(id),
});
}

对于表格行内的确认按钮,建议使用 ActionButtonconfirmable 属性。

自定义页脚

<Modal
title="自定义页脚"
open={open}
footer={[
<Button key="back" onClick={() => setOpen(false)}>返回</Button>,
<Button key="submit" type="primary" onClick={handleOk}>提交</Button>,
]}
>
<p>内容</p>
</Modal>

无页脚

<Modal title="信息" open={open} footer={null} onCancel={() => setOpen(false)}>
<p>只读内容。</p>
</Modal>

API

属性类型默认值说明
openbooleanfalse对话框是否可见
titleReactNode标题
childrenReactNode内容
footerReactNode | null底部内容;null 不显示
onOk(e) => void点击确定回调
onCancel(e) => void点击遮罩层或右上角叉或取消按钮的回调
okTextReactNode'确定'确认按钮文字
cancelTextReactNode'取消'取消按钮文字
okTypeButtonType'primary'确认按钮类型
confirmLoadingbooleanfalse确定按钮 loading
widthnumber | string520宽度
centeredbooleanfalse垂直居中展示
closablebooleantrue是否显示右上角的关闭按钮
maskClosablebooleantrue点击蒙层是否允许关闭
destroyOnClosebooleanfalse关闭时销毁子元素
forceRenderbooleanfalse强制渲染
keyboardbooleantrue是否支持键盘 esc 关闭
zIndexnumber1000设置 z-index
afterClose() => void完全关闭后的回调
afterOpenChange(open) => void打开和关闭的回调

最佳实践

  • 对话框内含表单时设置 destroyOnClose,关闭时重置表单状态。
  • 避免嵌套多层对话框;次级面板使用 Drawer
  • 简单的是/否确认使用 Modal.confirm;表格行内操作使用 ActionButtonconfirmable