Modal对话框

模态对话框。

何时使用

需要用户处理事务,又不希望跳转页面以致打断工作流程时,可以使用 Modal 在当前页面正中打开一个浮层,承载相应的操作。

另外当需要一个简洁的确认框询问用户时,可以使用精心封装好的 Modal.confirm() 等方法。

代码演示

Modal 对话框 - 图1

基本

第一个对话框。

  1. import { Modal, Button } from 'choerodon-ui';
  2. class App extends React.Component {
  3. state = { visible: false }
  4. showModal = () => {
  5. this.setState({
  6. visible: true,
  7. });
  8. }
  9. handleOk = (e) => {
  10. console.log(e);
  11. this.setState({
  12. visible: false,
  13. });
  14. }
  15. handleCancel = (e) => {
  16. console.log(e);
  17. this.setState({
  18. visible: false,
  19. });
  20. }
  21. render() {
  22. return (
  23. <div>
  24. <Button type="primary" onClick={this.showModal}>Open</Button>
  25. <Modal
  26. title="Basic Modal"
  27. visible={this.state.visible}
  28. onOk={this.handleOk}
  29. onCancel={this.handleCancel}
  30. center
  31. >
  32. <p>Some contents...</p>
  33. <p>Some contents...</p>
  34. <p>Some contents...</p>
  35. </Modal>
  36. </div>
  37. );
  38. }
  39. }
  40. ReactDOM.render(<App />, mountNode);

Modal 对话框 - 图2

自定义页脚

更复杂的例子,自定义了页脚的按钮,点击提交后进入 loading 状态,完成后关闭。

不需要默认确定取消按钮时,你可以把 footer 设为 null

  1. import { Modal, Button } from 'choerodon-ui';
  2. class App extends React.Component {
  3. state = {
  4. loading: false,
  5. visible: false,
  6. }
  7. showModal = () => {
  8. this.setState({
  9. visible: true,
  10. });
  11. }
  12. handleOk = () => {
  13. this.setState({ loading: true });
  14. setTimeout(() => {
  15. this.setState({ loading: false, visible: false });
  16. }, 3000);
  17. }
  18. handleCancel = () => {
  19. this.setState({ visible: false });
  20. }
  21. render() {
  22. const { visible, loading } = this.state;
  23. return (
  24. <div>
  25. <Button type="primary" onClick={this.showModal}>
  26. Open
  27. </Button>
  28. <Modal
  29. visible={visible}
  30. title="Title"
  31. onOk={this.handleOk}
  32. onCancel={this.handleCancel}
  33. footer={[
  34. <Button key="back" onClick={this.handleCancel}>Return</Button>,
  35. <Button key="submit" type="primary" loading={loading} onClick={this.handleOk}>
  36. Submit
  37. </Button>,
  38. ]}
  39. >
  40. <p>Some contents...</p>
  41. <p>Some contents...</p>
  42. <p>Some contents...</p>
  43. <p>Some contents...</p>
  44. <p>Some contents...</p>
  45. </Modal>
  46. </div>
  47. );
  48. }
  49. }
  50. ReactDOM.render(<App />, mountNode);

Modal 对话框 - 图3

确认对话框

使用 confirm() 可以快捷地弹出确认框。onCancel/onOk 返回 promise 可以延迟关闭

  1. import { Modal, Button } from 'choerodon-ui';
  2. const confirm = Modal.confirm;
  3. function showConfirm() {
  4. confirm({
  5. title: 'Do you want to delete these items?',
  6. content: 'When clicked the OK button, this dialog will be closed after 1 second',
  7. onOk() {
  8. return new Promise((resolve, reject) => {
  9. setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
  10. }).catch(() => console.log('Oops errors!'));
  11. },
  12. onCancel() {},
  13. });
  14. }
  15. ReactDOM.render(<Button onClick={showConfirm}>Confirm</Button>, mountNode);

Modal 对话框 - 图4

国际化

设置 okTextcancelText 以自定义按钮文字。

  1. import { Modal, Button } from 'choerodon-ui';
  2. class LocalizedModal extends React.Component {
  3. state = { visible: false };
  4. showModal = () => {
  5. this.setState({
  6. visible: true,
  7. });
  8. };
  9. hideModal = () => {
  10. this.setState({
  11. visible: false,
  12. });
  13. };
  14. render() {
  15. return (
  16. <div>
  17. <Button type="primary" onClick={this.showModal}>
  18. Modal
  19. </Button>
  20. <Modal
  21. title="Modal"
  22. visible={this.state.visible}
  23. onOk={this.hideModal}
  24. onCancel={this.hideModal}
  25. okText="确认"
  26. cancelText="取消"
  27. >
  28. <p>Bla bla ...</p>
  29. <p>Bla bla ...</p>
  30. <p>Bla bla ...</p>
  31. </Modal>
  32. </div>
  33. );
  34. }
  35. }
  36. function confirm() {
  37. Modal.confirm({
  38. title: 'Confirm',
  39. content: 'Bla bla ...',
  40. okText: '确认',
  41. cancelText: '取消',
  42. });
  43. }
  44. ReactDOM.render(
  45. <div>
  46. <LocalizedModal />
  47. <br />
  48. <Button onClick={confirm}>Confirm</Button>
  49. </div>,
  50. mountNode,
  51. );

Modal 对话框 - 图5

自定义位置

1.0 之后,Modal 的 align 属性被移除,您可以直接使用 style.top 或配合其他样式来设置对话框位置。

  1. import { Modal, Button } from 'choerodon-ui';
  2. class App extends React.Component {
  3. state = {
  4. modal1Visible: false,
  5. modal2Visible: false,
  6. }
  7. setModal1Visible(modal1Visible) {
  8. this.setState({ modal1Visible });
  9. }
  10. setModal2Visible(modal2Visible) {
  11. this.setState({ modal2Visible });
  12. }
  13. render() {
  14. return (
  15. <div>
  16. <Button type="primary" onClick={() => this.setModal1Visible(true)}>Display a modal dialog at 20px to Top</Button>
  17. <Modal
  18. title="20px to Top"
  19. style={{ top: 20 }}
  20. visible={this.state.modal1Visible}
  21. onOk={() => this.setModal1Visible(false)}
  22. onCancel={() => this.setModal1Visible(false)}
  23. >
  24. <p>some contents...</p>
  25. <p>some contents...</p>
  26. <p>some contents...</p>
  27. </Modal>
  28. <br /><br />
  29. <Button type="primary" onClick={() => this.setModal2Visible(true)}>Vertically centered modal dialog</Button>
  30. <Modal
  31. title="Vertically centered modal dialog"
  32. wrapClassName="vertical-center-modal"
  33. visible={this.state.modal2Visible}
  34. onOk={() => this.setModal2Visible(false)}
  35. onCancel={() => this.setModal2Visible(false)}
  36. >
  37. <p>some contents...</p>
  38. <p>some contents...</p>
  39. <p>some contents...</p>
  40. </Modal>
  41. </div>
  42. );
  43. }
  44. }
  45. ReactDOM.render(<App />, mountNode);
  1. /* use css to set position of modal */
  2. .vertical-center-modal {
  3. text-align: center;
  4. white-space: nowrap;
  5. }
  6. .vertical-center-modal:before {
  7. content: '';
  8. display: inline-block;
  9. height: 100%;
  10. vertical-align: middle;
  11. width: 0;
  12. }
  13. .vertical-center-modal .c7n-modal {
  14. display: inline-block;
  15. vertical-align: middle;
  16. top: 0;
  17. text-align: left;
  18. }
  19. /*
  20. // Use flex which not working in IE
  21. .vertical-center-modal {
  22. display: flex;
  23. align-items: center;
  24. justify-content: center;
  25. }
  26. .vertical-center-modal .c7n-modal {
  27. top: 0;
  28. }
  29. */

Modal 对话框 - 图6

异步关闭

点击确定后异步关闭对话框,例如提交表单。

  1. import { Modal, Button } from 'choerodon-ui';
  2. class App extends React.Component {
  3. state = {
  4. ModalText: 'Content of the modal',
  5. visible: false,
  6. confirmLoading: false,
  7. }
  8. showModal = () => {
  9. this.setState({
  10. visible: true,
  11. });
  12. }
  13. handleOk = () => {
  14. this.setState({
  15. ModalText: 'The modal will be closed after two seconds',
  16. confirmLoading: true,
  17. });
  18. setTimeout(() => {
  19. this.setState({
  20. visible: false,
  21. confirmLoading: false,
  22. });
  23. }, 2000);
  24. }
  25. handleCancel = () => {
  26. console.log('Clicked cancel button');
  27. this.setState({
  28. visible: false,
  29. });
  30. }
  31. render() {
  32. const { visible, confirmLoading, ModalText } = this.state;
  33. return (
  34. <div>
  35. <Button type="primary" onClick={this.showModal}>Open</Button>
  36. <Modal title="Title"
  37. visible={visible}
  38. onOk={this.handleOk}
  39. confirmLoading={confirmLoading}
  40. onCancel={this.handleCancel}
  41. >
  42. <p>{ModalText}</p>
  43. </Modal>
  44. </div>
  45. );
  46. }
  47. }
  48. ReactDOM.render(<App />, mountNode);

Modal 对话框 - 图7

确认对话框

使用 confirm() 可以快捷地弹出确认框。

  1. import { Modal, Button } from 'choerodon-ui';
  2. const confirm = Modal.confirm;
  3. function showConfirm() {
  4. confirm({
  5. title: 'Do you Want to delete these items?',
  6. content: 'Some descriptions',
  7. onOk() {
  8. console.log('OK');
  9. },
  10. onCancel() {
  11. console.log('Cancel');
  12. },
  13. });
  14. }
  15. function showDeleteConfirm() {
  16. confirm({
  17. title: 'Are you sure delete this task?',
  18. content: 'Some descriptions',
  19. okText: 'Yes',
  20. okType: 'danger',
  21. cancelText: 'No',
  22. onOk() {
  23. console.log('OK');
  24. },
  25. onCancel() {
  26. console.log('Cancel');
  27. },
  28. });
  29. }
  30. ReactDOM.render(
  31. <div>
  32. <Button onClick={showConfirm}>Confirm</Button>
  33. <Button onClick={showDeleteConfirm} type="dashed">
  34. Delete
  35. </Button>
  36. </div>,
  37. mountNode,
  38. );

Modal 对话框 - 图8

信息提示

各种类型的信息提示,只提供一个按钮用于关闭。

  1. import { Modal, Button } from 'choerodon-ui';
  2. function info() {
  3. Modal.info({
  4. title: 'This is a notification message',
  5. content: (
  6. <div>
  7. <p>some messages...some messages...</p>
  8. <p>some messages...some messages...</p>
  9. </div>
  10. ),
  11. onOk() {},
  12. });
  13. }
  14. function success() {
  15. Modal.success({
  16. title: 'This is a success message',
  17. content: 'some messages...some messages...',
  18. });
  19. }
  20. function error() {
  21. Modal.error({
  22. title: 'This is an error message',
  23. content: 'some messages...some messages...',
  24. });
  25. }
  26. function warning() {
  27. Modal.warning({
  28. title: 'This is a warning message',
  29. content: 'some messages...some messages...',
  30. });
  31. }
  32. ReactDOM.render(
  33. <div>
  34. <Button onClick={info}>Info</Button>
  35. <Button onClick={success}>Success</Button>
  36. <Button onClick={error}>Error</Button>
  37. <Button onClick={warning}>Warning</Button>
  38. </div>,
  39. mountNode,
  40. );

Modal 对话框 - 图9

手动移除

手动关闭 modal。

  1. import { Modal, Button } from 'choerodon-ui';
  2. function success() {
  3. const modal = Modal.success({
  4. title: 'This is a notification message',
  5. content: 'This modal will be destroyed after 1 second',
  6. });
  7. setTimeout(() => modal.destroy(), 1000);
  8. }
  9. ReactDOM.render(<Button onClick={success}>Success</Button>, mountNode);

Modal 对话框 - 图10

侧边弹出

侧边弹出。

  1. import { Modal, Button } from 'choerodon-ui';
  2. const { Sidebar } = Modal;
  3. class App extends React.Component {
  4. state = { visible: false };
  5. showModal = () => {
  6. this.setState({
  7. visible: true,
  8. });
  9. };
  10. handleOk = () => {
  11. this.setState({
  12. visible: false,
  13. });
  14. };
  15. handleCancel = () => {
  16. this.setState({
  17. visible: false,
  18. });
  19. };
  20. render() {
  21. return (
  22. <div>
  23. <Button type="primary" onClick={this.showModal}>
  24. Open
  25. </Button>
  26. <Sidebar
  27. title="Basic Modal"
  28. visible={this.state.visible}
  29. onOk={this.handleOk}
  30. onCancel={this.handleCancel}
  31. cancelText="取消"
  32. okText="确定"
  33. width={400}
  34. closable
  35. >
  36. <p>Some contents...</p>
  37. <p>Some contents...</p>
  38. <p>Some contents...</p>
  39. </Sidebar>
  40. </div>
  41. );
  42. }
  43. }
  44. ReactDOM.render(<App />, mountNode);

API

参数说明类型默认值
afterCloseModal 完全关闭后的回调function
bodyStyleModal body 样式object{}
cancelText取消按钮文字string取消
closable是否显示右上角的关闭按钮booleantrue
confirmLoading确定按钮 loadingboolean
destroyOnClose关闭时销毁 Modal 里的子元素booleanfalse
footer底部内容,当不需要默认底部按钮时,可以设为 footer={null}string|ReactNode确定取消按钮
getContainer指定 Modal 挂载的 HTML 节点(instance): HTMLElement() => document.body
mask是否展示遮罩Booleantrue
maskClosable点击蒙层是否允许关闭booleantrue
maskStyle遮罩样式object{}
okText确认按钮文字string确定
okType确认按钮类型stringprimary
disableOkOk按钮是否禁用booleanfalse
disableCancelCancel按钮是否禁用booleanfalse
style可用于设置浮层的样式,调整浮层位置等object-
title标题string|ReactNode
visible对话框是否可见boolean
width宽度string|number520
wrapClassName对话框外层容器的类名string-
zIndex设置 Modal 的 z-indexNumber1000
onCancel点击遮罩层或右上角叉或取消按钮的回调function(e)
onOk点击确定回调function(e)
funcType按钮功能string

注意

<Modal /> 默认关闭后状态不会自动清空, 如果希望每次打开都是新内容,请设置 destroyOnClose

Modal.SideBar

侧边栏弹出窗,表中无说明的API同Modal| 参数 | 说明 | 类型 | 默认值 || —- | —- | —- | —- || alwaysCanCancel | 取消按钮是否一直可按,无论是否有confirmLoading | boolean | false |

Modal.method()

包括:

  • Modal.info

  • Modal.success

  • Modal.error

  • Modal.warning

  • Modal.confirm

以上均为一个函数,参数为 object,具体属性如下:

参数说明类型默认值
cancelText取消按钮文字string取消
className容器类名string-
content内容string|ReactNode
iconType图标 Icon 类型stringquestion-circle
maskClosable点击蒙层是否允许关闭Booleanfalse
okText确认按钮文字string确定
okType确认按钮类型stringprimary
title标题string|ReactNode
width宽度string|number416
zIndex设置 Modal 的 z-indexNumber1000
onCancel取消回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function
onOk点击确定回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function

以上函数调用后,会返回一个引用,可以通过该引用关闭弹窗。

  1. const ref = Modal.info();
  2. ref.destroy();