Modal对话框

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

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

    基本

    第一个对话框。

    Modal对话框 - 图2

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

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

    1. import { Modal, Button } from 'antd';
    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 Modal with customized footer
    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}>
    35. Return
    36. </Button>,
    37. <Button key="submit" type="primary" loading={loading} onClick={this.handleOk}>
    38. Submit
    39. </Button>,
    40. ]}
    41. >
    42. <p>Some contents...</p>
    43. <p>Some contents...</p>
    44. <p>Some contents...</p>
    45. <p>Some contents...</p>
    46. <p>Some contents...</p>
    47. </Modal>
    48. </div>
    49. );
    50. }
    51. }
    52. ReactDOM.render(<App />, mountNode);

    确认对话框

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

    1. import { Modal, Button } from 'antd';
    2. const { confirm } = Modal;
    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

    1. import { Modal, Button } from 'antd';
    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. content: 'Bla bla ...',
    39. okText: '确认',
    40. cancelText: '取消',
    41. });
    42. }
    43. <div>
    44. <LocalizedModal />
    45. <br />
    46. <Button onClick={confirm}>Confirm</Button>
    47. </div>,
    48. mountNode,
    49. );

    自定义位置

    使用 centered 或类似 style.top 的样式来设置对话框位置。

    Modal对话框 - 图6

    传入 okButtonPropscancelButtonProps 可分别自定义确定按钮和取消按钮的 props。

    1. import { Modal, Button } from 'antd';
    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}>
    25. Open Modal with customized button props
    26. </Button>
    27. <Modal
    28. title="Basic Modal"
    29. visible={this.state.visible}
    30. onOk={this.handleOk}
    31. onCancel={this.handleCancel}
    32. okButtonProps={{ disabled: true }}
    33. cancelButtonProps={{ disabled: true }}
    34. >
    35. <p>Some contents...</p>
    36. <p>Some contents...</p>
    37. <p>Some contents...</p>
    38. </Modal>
    39. </div>
    40. );
    41. }
    42. }
    43. ReactDOM.render(<App />, mountNode);

    异步关闭

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

    1. import { Modal, Button } from 'antd';
    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}>
    36. Open Modal with async logic
    37. </Button>
    38. <Modal
    39. title="Title"
    40. visible={visible}
    41. onOk={this.handleOk}
    42. confirmLoading={confirmLoading}
    43. onCancel={this.handleCancel}
    44. >
    45. <p>{ModalText}</p>
    46. </Modal>
    47. </div>
    48. );
    49. }
    50. }
    51. ReactDOM.render(<App />, mountNode);

    Modal对话框 - 图8

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

    1. import { Modal, Button } from 'antd';
    2. const { confirm } = Modal;
    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. function showDeleteConfirm() {
    15. confirm({
    16. title: 'Are you sure delete this task?',
    17. content: 'Some descriptions',
    18. okText: 'Yes',
    19. okType: 'danger',
    20. cancelText: 'No',
    21. onOk() {
    22. console.log('OK');
    23. },
    24. onCancel() {
    25. console.log('Cancel');
    26. },
    27. });
    28. }
    29. function showPropsConfirm() {
    30. confirm({
    31. title: 'Are you sure delete this task?',
    32. content: 'Some descriptions',
    33. okText: 'Yes',
    34. okType: 'danger',
    35. okButtonProps: {
    36. disabled: true,
    37. },
    38. cancelText: 'No',
    39. onOk() {
    40. console.log('OK');
    41. },
    42. onCancel() {
    43. console.log('Cancel');
    44. },
    45. });
    46. }
    47. ReactDOM.render(
    48. <div>
    49. <Button onClick={showConfirm}>Confirm</Button>
    50. <Button onClick={showDeleteConfirm} type="dashed">
    51. Delete
    52. </Button>
    53. <Button onClick={showPropsConfirm} type="dashed">
    54. With extra props
    55. </Button>
    56. </div>,
    57. mountNode,
    58. );

    信息提示

    Modal对话框 - 图10

    手动更新和关闭 Modal.method 方式创建的对话框。

    1. import { Modal, Button } from 'antd';
    2. function countDown() {
    3. let secondsToGo = 5;
    4. const modal = Modal.success({
    5. title: 'This is a notification message',
    6. content: `This modal will be destroyed after ${secondsToGo} second.`,
    7. });
    8. const timer = setInterval(() => {
    9. secondsToGo -= 1;
    10. modal.update({
    11. content: `This modal will be destroyed after ${secondsToGo} second.`,
    12. });
    13. }, 1000);
    14. setTimeout(() => {
    15. clearInterval(timer);
    16. modal.destroy();
    17. }, secondsToGo * 1000);
    18. }
    19. ReactDOM.render(<Button onClick={countDown}>Open modal to close in 5s</Button>, mountNode);

    销毁确认对话框

    使用 Modal.destroyAll() 可以销毁弹出的确认窗。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题。

    1. import { Modal, Button } from 'antd';
    2. function destroyAll() {
    3. Modal.destroyAll();
    4. }
    5. const { confirm } = Modal;
    6. function showConfirm() {
    7. for (let i = 0; i < 3; i += 1) {
    8. setTimeout(() => {
    9. confirm({
    10. content: <Button onClick={destroyAll}>Click to destroy all</Button>,
    11. onOk() {
    12. console.log('OK');
    13. },
    14. onCancel() {
    15. console.log('Cancel');
    16. },
    17. });
    18. }, i * 500);
    19. }
    20. }
    21. ReactDOM.render(
    22. <div>
    23. <Button onClick={showConfirm}>Confirm</Button>
    24. </div>,
    25. mountNode,
    26. );

    注意

    包括:

    • Modal.info

    • Modal.success

    • Modal.error

    • Modal.warning

    • Modal.confirm

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

    参数说明类型默认值
    autoFocusButton指定自动获得焦点的按钮null|string: ok cancelok
    cancelText设置 Modal.confirm 取消按钮文字string取消
    centered垂直居中展示 ModalBooleanfalse
    className容器类名string-
    content内容string|ReactNode
    icon自定义图标(3.12.0 新增)string|ReactNode<Icon type="question-circle">
    iconType图标类型(3.12.0 后废弃,请使用 iconstringquestion-circle
    mask是否展示遮罩Booleantrue
    maskClosable点击蒙层是否允许关闭Booleanfalse
    okText确认按钮文字string确定
    okType确认按钮类型stringprimary
    okButtonPropsok 按钮 propsButtonProps-
    cancelButtonPropscancel 按钮 props-
    title标题string|ReactNode
    width宽度string|number416
    zIndex设置 Modal 的 z-indexNumber1000
    onCancel取消回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function
    onOk点击确定回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function
    1. const modal = Modal.info();
    2. modal.update({
    3. title: '修改的标题',
    4. content: '修改的内容',
    5. });
    • Modal.destroyAll

    使用 可以销毁弹出的确认窗(即上述的 Modal.info、Modal.success、Modal.error、Modal.warning、Modal.confirm)。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题,而不用各处去使用实例的返回值进行关闭(modal.destroy() 适用于主动关闭,而不是路由这样被动关闭)