Modal对话框
模态对话框。
何时使用
需要用户处理事务,又不希望跳转页面以致打断工作流程时,可以使用 Modal 在当前页面正中打开一个浮层,承载相应的操作。
另外当需要一个简洁的确认框询问用户时,可以使用 ModalService.Confirm() 等语法糖方法。
Examples
Modal API#
| 属性 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| AfterClose | 属性Callback after modal is closed. | EventCallback | -- |
| AfterOpen | 属性Callback after modal is opened. | EventCallback | -- |
| BodyStyle | 属性模态主体元素的主体样式。比如高度,填充等 | String | -- |
| CancelText | 属性Cancel 按钮的 Text 或 RenderFragment,它将覆盖 ModalLocale | OneOf<String, RenderFragment?? | -- |
| Centered | 属性居中 | Boolean | -- |
| Closable | 属性关闭 (x) 按钮是否在模态对话框的右上角可见 | Boolean | true |
| Draggable | 属性模态对话框是否被拖动 | Boolean | -- |
| DragInViewport | 属性仅在视口内拖放 | Boolean | true |
| CloseIcon | 属性关闭图标RenderFragment,默认是一个“X” | RenderFragment | close-outline |
| ConfirmLoading | 属性是否为确定按钮应用加载视觉效果 | Boolean | -- |
| DestroyOnClose | 属性onClose 时是否卸载子组件 | Boolean | false |
| Header | 属性标题内容 | RenderFragment | -- |
| Footer | 属性页脚内容,不需要默认按钮时设置为Footer=null | OneOf<String, RenderFragment?? | -- |
| GetContainer | 属性获取或设置模态父 DOM,默认为 null:指定 document.body | ElementReference? | -- |
| Keyboard | 属性是否支持按esc关闭 | Boolean | true |
| Mask | 属性是否显示掩码 | Boolean | true |
| MaskClosable | 属性单击遮罩(模态之外的区域)时是否关闭模态对话框 | Boolean | true |
| MaskStyle | 属性模态遮罩元素的样式 | String | -- |
| OkText | 属性确定按钮的 RenderFragment 文本,它将覆盖 ModalLocale | OneOf<String, RenderFragment?? | null |
| OkType | 属性确定按钮的按钮类型 | ButtonType | ButtonType.Primary |
| Title | 属性模态对话框的标题 | String | -- |
| TitleTemplate | 属性模态对话框的标题。优先于标题。 | RenderFragment | -- |
| Visible | 属性模态对话框是否可见 | Boolean | -- |
| VisibleChanged | 属性指定模式对话框可见或不可见时的函数调用 | EventCallback<Boolean> | -- |
| Width | 属性模态对话框的宽度,默认值为520 | OneOf<String, Double> | 520 |
| WrapClassName | 属性模态对话框容器的类名 | String | -- |
| ZIndex | 属性模态的 z-index | Int32 | 1000 |
| OnCancel | 属性指定当用户单击蒙版、右上角的关闭按钮或取消按钮时将调用的函数 | EventCallback<MouseEventArgs> | -- |
| OnOk | 属性指定当用户单击“确定”按钮时将调用的函数 | EventCallback<MouseEventArgs> | -- |
| OkButtonProps | 属性确定按钮道具 | ButtonProps | -- |
| CancelButtonProps | 属性取消按钮道具 | ButtonProps | -- |
| ChildContent | 属性子内容 | RenderFragment | -- |
| Rtl | 是RTL | Boolean | -- |
| Locale | 属性模态语言环境 | ModalLocale | LocaleProvider.CurrentLocacle.Modal |
| MaxBodyHeight | 属性最大模态内容主体高度 | String | -- |
| Maximizable | 属性显示模态最大化按钮 | Boolean | false |
| MaximizeBtnIcon | 属性模态框正常状态下最大化按钮的图标 | RenderFragment | fullscreen-outline |
| RestoreBtnIcon | 属性模态最大化时最大化按钮的图标 | RenderFragment | fullscreen-exit-outline |
| DefaultMaximized | 属性在组件初始化时最大化 Modal,它会忽略 Maximizable 值。 | Boolean | false |
| Resizable | 属性可调整大小(仅限水平方向) | Boolean | -- |
| ForceRender | 属性是否强制在打开之前渲染模态 dom。 | Boolean | -- |
| Id | 属性组件 HTML 的 ID | String | Uniquely Generated ID |
| Class | 属性为 DOM 元素指定一个或多个类名。 | String | -- |
| Style | 属性指定 DOM 元素的内联样式。 | String | -- |
| RefBack | 属性一个 ForwardRef 实例。您可以使用 AntDesign.ForwardRef.Current 获取对内部 DOM 的引用。 |
ForwardRef | -- |
ModalService API#
| 方法签名 | 返回类型 | 描述 |
|---|---|---|
| Confirm(ConfirmOptions props) | ConfirmRef | 创建并打开一个 OK-Cancel Confirm 对话框 |
| Info(ConfirmOptions options) | ConfirmRef | 创建并打开带有信息图标的 OK-Cancel Confirm 对话框 |
| Success(ConfirmOptions options) | ConfirmRef | 创建并打开带有成功图标的 OK-Cancel Confirm 对话框 |
| Error(ConfirmOptions options) | ConfirmRef | 创建并打开带有错误图标的 OK-Cancel Confirm 对话框 |
| Warning(ConfirmOptions options) | ConfirmRef | 创建并打开带有警告图标的 OK-Cancel Confirm 对话框 |
| ConfirmAsync(ConfirmOptions props) | Task<Boolean> | 创建并打开一个 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮 |
| InfoAsync(ConfirmOptions options) | Task<Boolean> | 创建并打开带有信息图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮 |
| SuccessAsync(ConfirmOptions options) | Task<Boolean> | 创建并打开带有成功图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮 |
| ErrorAsync(ConfirmOptions options) | Task<Boolean> | 创建并打开带有错误图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮 |
| WarningAsync(ConfirmOptions options) | Task<Boolean> | 创建并打开带有警告图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮 |
| UpdateConfirmAsync(ConfirmRef confirmRef) | Task | 更新确认哪个 Visible=true |
| DestroyConfirmAsync(ConfirmRef confirmRef) | Task | 关闭确认对话框 |
| DestroyAllConfirmAsync() | Task | 关闭所有确认对话框 |
| CreateConfirm(ConfirmOptions config) | ConfirmRef | 创建并开启一个OK-Cancel Confirm异步 |
| CreateConfirm(ConfirmOptions config, TComponentOptions componentOptions) | ConfirmRef<TResult> | 创建并打开模板确认对话框 |
| CreateConfirm(ConfirmOptions config, TComponentOptions componentOptions) | ConfirmRef | |
| CreateConfirmAsync(ConfirmOptions config) | Task<ConfirmRef> | Create and open a OK-Cancel Confirm asynchronously and wait until confirm is closed |
| CreateConfirmAsync(ConfirmOptions config, TComponentOptions componentOptions) | Task<ConfirmRef<TResult>> | Create and open template Confirm dialog asynchronously and wait until confirm is closed |
| CreateConfirmAsync(ConfirmOptions config, TComponentOptions componentOptions) | Task<ConfirmRef> | Create and open template Confirm dialog asynchronously and wait until confirm is closed |
| CreateModal(ModalOptions options) | ModalRef | 创建并打开模态 |
| CreateModal(ModalOptions options) | ModalRef<TResult> | 创建并打开模态 |
| CreateModal(ModalOptions options, TComponentOptions componentOptions) | ModalRef | 使用模板创建并打开模态 |
| CreateModal(ModalOptions options, TComponentOptions componentOptions) | ModalRef<TResult> | 创建并打开 Modal with template 组件 |
| CreateModalAsync(ModalOptions options) | Task<ModalRef> | 创建并打开模态 |
| CreateModalAsync(ModalOptions options) | Task<ModalRef<TResult>> | 创建并打开模态 |
| CreateModalAsync(ModalOptions options, TComponentOptions componentOptions) | Task<ModalRef> | 使用模板创建并打开模态 |
| CreateModalAsync(ModalOptions options, TComponentOptions componentOptions) | Task<ModalRef<TResult>> | 使用模板创建并打开模态 |
ConfirmService API#
| 方法签名 | 返回类型 | 描述 |
|---|---|---|
| Show(OneOf<String, RenderFragment> content, OneOf<String, RenderFragment> title, ConfirmButtons confirmButtons, ConfirmIcon confirmIcon, ConfirmButtonOptions options, ConfirmAutoFocusButton? autoFocusButton) | Task<ConfirmResult> | 显示确认对话框,如 Windows 的 MessageBox |
| Show(OneOf<String, RenderFragment> content, OneOf<String, RenderFragment> title, ConfirmButtons confirmButtons, ConfirmIcon confirmIcon) | Task<ConfirmResult> | 显示确认对话框,如 Windows 的 MessageBox |
ConfirmOptions API#
| 属性 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| Locale | 确认语言环境 | ConfirmLocale | -- |
| ClassName | “.ant-modal”元素的类名 | String | -- |
| Visible | Boolean | -- | |
| Content | 儿童内容 | OneOf<String, RenderFragment> | -- |
| Icon | 确认左上角图标 | RenderFragment | -- |
| Style | .ant-modal 元素的样式 | String | -- |
| AutoFocusButton | ConfirmAutoFocusButton | -- | |
| OkType | 为最左边的按钮设置 OK 按钮类型:OK 或 Yes 按钮 | ButtonType | -- |
| OkText | 为最左边的按钮设置 OK 按钮内容:OK 或 Yes 按钮,它将覆盖 ConfirmLocale | OneOf<String, RenderFragment?? | -- |
| CancelText | 为左侧第二个按钮设置取消按钮内容:取消或否按钮,它将覆盖 ConfirmLocale | OneOf<String, RenderFragment?? | -- |
| OnCancel | 对于 OK-Cancel 确认对话框,取消按钮点击回调。仅在ModalService方式创建的Confirm中触发 | Func<ModalClosingEventArgs, Task> | -- |
| OnOk | 对于 OK-Cancel Confirm 对话框,OK 按钮点击回调。仅在ModalService方式创建的Confirm中触发 | Func<ModalClosingEventArgs, Task> | -- |
| OkButtonProps | OK-Cancel Confirm 对话框的 OK 按钮属性。它相当于 Button1Props。 | ButtonProps | -- |
| CancelButtonProps | OK-Cancel 确认对话框的取消按钮道具。它相当于 Button2Props。 | ButtonProps | -- |
| Button1Props | LTR 布局中最左边的按钮 | ButtonProps | -- |
| Button2Props | 左边第二个按钮是LTR布局 | ButtonProps | -- |
| Button3Props | LTR 布局中最右边的按钮 | ButtonProps | -- |
| OkCancel | 显示 OK-Cancel Confirm 对话框的取消按钮 | Boolean | -- |
| PrefixCls | 类名前缀 | String | -- |
| CancelText | 模态默认页脚取消文本 | OneOf<String, RenderFragment> | -- |
| Centered | 是否中置显示 | Boolean | -- |
| GetContainer | 获取或设置模态父 DOM | ElementReference? | -- |
| Keyboard | 是否支持按esc关闭 | Boolean | -- |
| Mask | 是否显示掩码 | Boolean | -- |
| MaskClosable | 单击遮罩(模态之外的区域)时是否关闭模态对话框 | Boolean | -- |
| MaskStyle | 对话框的掩码元素的样式 | String | -- |
| OkText | 确定按钮的文本 | OneOf<String, RenderFragment> | -- |
| Title | 模态对话框的字符串标题 | String | -- |
| TitleTemplate | RenderFragment 模态对话框的标题 | RenderFragment | -- |
| Width | 模态对话框的宽度 | OneOf<String, Double> | -- |
| ZIndex | 模态的 z-index | Int32 | -- |
| Rtl | 是RTL | Boolean | -- |
常问问题#
为什么继承自FeedbackComponent<>的自定义组件在关闭ModalService.CreateModalAsync<>创建的Modal时不执行Dispose方法?
Modal关闭后默认不会从DOM中移除,所以不会执行自定义组件的Dispose方法。 您可以通过设置 modalOptions.DestroyOnClose=true 来更改此默认行为。