跳到主要内容

Modal

一种会中断用户操作、用于展示重要信息或请求用户决策的对话框浮层。

来源:antd Modal 进行封装,新增了 draggable 属性。完整文档: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)}>
Open Modal
</Button>
<Modal
title="Basic Modal"
open={open}
onOk={() => setOpen(false)}
onCancel={() => setOpen(false)}
>
<p>Modal content goes here.</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)}>Open</Button>
<Modal
title="Save Changes"
open={open}
onOk={handleOk}
confirmLoading={loading}
onCancel={() => setOpen(false)}
>
<p>Are you sure you want to save?</p>
</Modal>
</>
);
}

确认对话框

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

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

function handleDelete(id: number) {
Modal.confirm({
title: 'Delete Record',
content: 'This action cannot be undone. Continue?',
okText: 'Delete',
okType: 'danger',
onOk: () => deleteRecord(id),
});
}

对于行内的确认按钮,建议改用 ActionButton 配合 confirmable 属性。

自定义页脚

<Modal
title="Custom Footer"
open={open}
footer={[
<Button key="back" onClick={() => setOpen(false)}>Return</Button>,
<Button key="submit" type="primary" onClick={handleOk}>Submit</Button>,
]}
>
<p>Content</p>
</Modal>

无页脚

<Modal title="Info" open={open} footer={null} onCancel={() => setOpen(false)}>
<p>Read-only content.</p>
</Modal>

可拖拽

VEF 新增了 draggable 属性(@default false),开启后用户可以通过拖拽对话框头部来移动它。

<Modal title="Draggable Modal" open={open} draggable onCancel={() => setOpen(false)}>
<p>Drag the header to move this modal.</p>
</Modal>

API

PropTypeDefault说明
openbooleanfalse对话框是否可见
draggablebooleanfalse是否可以通过头部拖拽对话框(VEF 新增)
titleReactNode对话框标题
childrenReactNode对话框主体内容
footerReactNode | null自定义页脚;null 表示隐藏页脚
onOk(e) => void确定按钮点击回调
onCancel(e) => void取消 / 关闭回调
okTextReactNode'OK'确定按钮文案
cancelTextReactNode'Cancel'取消按钮文案
okTypeButtonType'primary'确定按钮类型
confirmLoadingbooleanfalse在确定按钮上显示加载状态
widthnumber | string520对话框宽度
centeredbooleanfalse垂直居中显示
closablebooleantrue显示关闭图标
maskClosablebooleantrue点击蒙层是否可以关闭
destroyOnHiddenbooleanfalse关闭时卸载子元素(destroyOnClose 是废弃别名)
forceRenderbooleanfalse预渲染内容
keyboardbooleantrue支持按下 Esc 键关闭
zIndexnumber1000层级
afterClose() => void关闭动画结束后的回调
afterOpenChange(open) => void打开状态变化后的回调

最佳实践

  • 当对话框内包含表单时设置 destroyOnHidden,使关闭后表单状态被重置。
  • 避免多层嵌套的对话框;次级面板请使用 Drawer
  • 简单的是/否确认使用 Modal.confirm;表格中的行内操作使用 ActionButton 配合 confirmable