Dialog 弹窗

如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

使用指南

对话框

何时使用

对话框是用于在不离开主路径的情况下,提供用户快速执行简单的操作、确认用户信息或反馈提示的辅助窗口。

API

弹窗

参数说明类型默认值
prefix样式类名的品牌前缀String'next-'
className自定义类名String-
style自定义内联样式Object-
title对话框的标题any-
footer传入底部的内容Boolean/String/ReactNode-
footerAlign底部按钮的对齐方式可选值:'left', 'center', 'right'Enum'right'
visible控制对话框是否可见Boolean-
hasMask是否需要maskBooleantrue
closable'esc, mask, close', 详见closableString/Boolean'esc,close'
shouldUpdatePosition是否强制更新dialog的位置,在isFullScreen为true且align为cc cc的时候无效Boolean-
align浮层自定义位置String/Number'cc cc'
animation配置动画的播放方式Object/Boolean{ in: 'fadeInDown', out: 'fadeOutUp' }
onClose在点击关闭按钮的时候触发的函数签名:Function() => voidFunction-
afterClose浮层关闭后触发的事件, 如果有动画,则在动画结束后触发签名:Function() => voidFunction-
onOk在点击Ok按钮的时候触发的函数签名:Function() => voidFunction() => {}
onCancel在点击Cancel按钮的时候触发的函数签名:Function() => voidFunction() => {}
minMargin当dialog过高的时候距离viewport的最小边距,在isFullScreen下无效。Number40
autoFocus当dialog弹出的时候是否自动获取焦点Booleantrue
locale自定义国际化文案对象属性:ok: {String} 确认按钮文案cancel: {String} 取消按钮文案Object-
language自定义国际化语言可选值:'en-us', 'zh-cn', 'zh-tw'Enum-
isFullScreen是否是启用使用CSS定位模式的对话框, 在该模式下面无需通过shouldUpdatePosition来进行重新定位。Booleanfalse

代码示例

对齐方式

通过footerAlign调整底部按钮的对齐方式

Dialog 弹窗 - 图1

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. footerAlign: "left",
  5. visible: false
  6. };
  7. map = ["left", "right", "center"];
  8. onClose = () => {
  9. this.setState({
  10. visible: false
  11. });
  12. };
  13. onClick = () => {
  14. let { footerAlign } = this.state,
  15. index = this.map.indexOf(footerAlign),
  16. next = index + 1;
  17. if (next >= this.map.length) {
  18. next = 0;
  19. }
  20. this.setState({
  21. footerAlign: this.map[next]
  22. });
  23. };
  24. onOpen = () => {
  25. this.setState({
  26. visible: true
  27. });
  28. };
  29. render() {
  30. return (
  31. <span>
  32. <Button onClick={this.onOpen} type="primary">
  33. Open dialog
  34. </Button>
  35. <Dialog
  36. visible={this.state.visible}
  37. onOk={this.onClose}
  38. onCancel={this.onClose}
  39. onClose={this.onClose}
  40. title="Welcome to Alibaba.com"
  41. footerAlign={this.state.footerAlign}
  42. >
  43. <Button onClick={this.onClick} type="primary">
  44. Modify footerAlign
  45. </Button>
  46. <h3>Your one-stop communication tool!</h3>
  47. <ul>
  48. <li>View messages from buyers & suppliers</li>
  49. <li>Negotiate the details of your order</li>
  50. </ul>
  51. </Dialog>
  52. </span>
  53. );
  54. }
  55. }
  56. ReactDOM.render(<App />, mountNode);

基本

第一个对话框

Dialog 弹窗 - 图2

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. visible: false
  5. };
  6. render() {
  7. return (
  8. <span>
  9. <Button onClick={this.onOpen} type="primary">
  10. Open dialog
  11. </Button>
  12. <Dialog
  13. visible={this.state.visible}
  14. onOk={this.onClose}
  15. closable="esc,mask,close"
  16. onCancel={this.onClose}
  17. onClose={this.onClose}
  18. title="Alibaba.com"
  19. >
  20. <h3>Your one-stop communication tool!</h3>
  21. <ul>
  22. <li>View messages from buyers & suppliers</li>
  23. <li>Negotiate the details of your order</li>
  24. </ul>
  25. </Dialog>
  26. </span>
  27. );
  28. }
  29. onOpen = () => {
  30. this.setState({
  31. visible: true
  32. });
  33. };
  34. onClose = () => {
  35. this.setState({
  36. visible: false
  37. });
  38. };
  39. }
  40. ReactDOM.render(<App />, mountNode);

自定义底部

默认的footer为两个按钮,你可以自定义footer的内容

Dialog 弹窗 - 图3

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. visible: false
  5. };
  6. render() {
  7. const footer = (
  8. <a onClick={this.onClose} href="javascript:;">
  9. Close
  10. </a>
  11. );
  12. return (
  13. <span>
  14. <Button onClick={this.onOpen} type="primary">
  15. Open dialog
  16. </Button>
  17. <Dialog
  18. visible={this.state.visible}
  19. footer={footer}
  20. onClose={this.onClose}
  21. title="Alibaba.com"
  22. >
  23. <h3>Your one-stop communication tool!</h3>
  24. <ul>
  25. <li>View messages from buyers & suppliers</li>
  26. <li>Negotiate the details of your order</li>
  27. </ul>
  28. </Dialog>
  29. </span>
  30. );
  31. }
  32. onOpen = () => {
  33. this.setState({
  34. visible: true
  35. });
  36. };
  37. onClose = () => {
  38. this.setState({
  39. visible: false
  40. });
  41. };
  42. }
  43. ReactDOM.render(<App />, mountNode);

FullScreen模式

设置isFullScreen为true的时候,无需通过shouldUpdatePosition进行重新定位。

Dialog 弹窗 - 图4

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. const content = <p>View messages from buyers & suppliers</p>;
  3. const largeContent = [];
  4. for (let i = 0; i < 10; i++) {
  5. largeContent.push(
  6. <span key={i}>
  7. <h3>Your one-stop communication tool!</h3>
  8. <ul>
  9. <li>View messages from buyers & suppliers</li>
  10. <li>Negotiate the details of your order</li>
  11. </ul>
  12. </span>
  13. );
  14. }
  15. class App extends React.Component {
  16. state = {
  17. visible: false,
  18. content
  19. };
  20. render() {
  21. return (
  22. <span>
  23. <Button onClick={this.onOpen} type="primary">
  24. Open Dialog
  25. </Button>
  26. <Dialog
  27. visible={this.state.visible}
  28. onOk={this.onClose}
  29. onCancel={this.onClose}
  30. isFullScreen
  31. onClose={this.onClose}
  32. title="Alibaba.com"
  33. >
  34. <Button type="primary" onClick={this.modifyContent}>
  35. Modify content.
  36. </Button>
  37. {this.state.content}
  38. </Dialog>
  39. </span>
  40. );
  41. }
  42. onOpen = () => {
  43. this.setState({
  44. visible: true
  45. });
  46. };
  47. onClose = () => {
  48. this.setState({
  49. visible: false,
  50. content
  51. });
  52. };
  53. modifyContent = () => {
  54. this.setState({
  55. content: largeContent
  56. });
  57. };
  58. }
  59. ReactDOM.render(<App />, mountNode);

内容较多的dialog

内容很多的情况,可以通过minMargin控制边距

Dialog 弹窗 - 图5

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. const content = [];
  3. for (let i = 0; i < 10; i++) {
  4. content.push(
  5. <span key={i}>
  6. <h3>Your one-stop communication tool!</h3>
  7. <ul>
  8. <li>View messages from buyers & suppliers</li>
  9. <li>Negotiate the details of your order</li>
  10. </ul>
  11. </span>
  12. );
  13. }
  14. class App extends React.Component {
  15. state = {
  16. visible: false
  17. };
  18. render() {
  19. return (
  20. <span>
  21. <Button onClick={this.onOpen} type="primary">
  22. Open dialog
  23. </Button>
  24. <Dialog
  25. visible={this.state.visible}
  26. onOk={this.onClose}
  27. onCancel={this.onClose}
  28. shouldUpdatePosition
  29. minMargin={50}
  30. onClose={this.onClose}
  31. title="Alibaba.com"
  32. >
  33. {content}
  34. </Dialog>
  35. </span>
  36. );
  37. }
  38. onOpen = () => {
  39. this.setState({
  40. visible: true
  41. });
  42. };
  43. onClose = () => {
  44. this.setState({
  45. visible: false,
  46. content
  47. });
  48. };
  49. }
  50. ReactDOM.render(<App />, mountNode);

语言

示例演示了在便捷调用和JSX模式下的语言切换方式

Dialog 弹窗 - 图6

查看源码在线预览

  1. import { Dialog, Button, LocaleProvider } from "@icedesign/base";
  2. const popupConfirm = () => {
  3. Dialog.confirm({
  4. content: "confirm",
  5. locale: {
  6. ok: "确认",
  7. cancel: "取消"
  8. }
  9. });
  10. };
  11. class App extends React.Component {
  12. state = {
  13. visible: false
  14. };
  15. render() {
  16. return (
  17. <span>
  18. <Button type="primary" onClick={this.onOpen}>
  19. Use en-us Open
  20. </Button>
  21. <Dialog
  22. visible={this.state.visible}
  23. onOk={this.onClose}
  24. onCancel={this.onClose}
  25. onClose={this.onClose}
  26. title="Alibaba.com"
  27. >
  28. <h3>Your one-stop communication tool!</h3>
  29. <ul>
  30. <li>View messages from buyers & suppliers</li>
  31. <li>Negotiate the details of your order</li>
  32. </ul>
  33. </Dialog>
  34. </span>
  35. );
  36. }
  37. onOpen = () => {
  38. LocaleProvider.set("en-us");
  39. this.setState({
  40. visible: true
  41. });
  42. };
  43. onClose = () => {
  44. this.setState({
  45. visible: false
  46. });
  47. };
  48. }
  49. ReactDOM.render(
  50. <span>
  51. <Button onClick={popupConfirm}>Confirm</Button>
  52. &nbsp;
  53. <App />
  54. </span>,
  55. mountNode
  56. );

层叠对话框

多个对话框展示。

Dialog 弹窗 - 图7

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. visible: false
  5. };
  6. render() {
  7. return (
  8. <span>
  9. <Button onClick={this.onOpen} type="primary">
  10. Open Dialog
  11. </Button>
  12. <Dialog
  13. visible={this.state.visible}
  14. onOk={this.onClose}
  15. onCancel={this.onClose}
  16. onClose={this.onClose}
  17. title="Alibaba.com"
  18. >
  19. <h3>Your one-stop communication tool!</h3>
  20. <ul>
  21. <li>View messages from buyers & suppliers</li>
  22. <li>Negotiate the details of your order</li>
  23. </ul>
  24. <App />
  25. </Dialog>
  26. </span>
  27. );
  28. }
  29. onOpen = () => {
  30. this.setState({
  31. visible: true
  32. });
  33. };
  34. onClose = () => {
  35. this.setState({
  36. visible: false
  37. });
  38. };
  39. }
  40. ReactDOM.render(<App />, mountNode);

延迟关闭

在使用alertconfirm的时候,返回promise,可以延迟执行函数

Dialog 弹窗 - 图8

查看源码在线预览

  1. import { Button, Dialog } from "@icedesign/base";
  2. const popupConfirm = () => {
  3. Dialog.confirm({
  4. content: "confirm",
  5. onOk: () => {
  6. return new Promise(resolve => {
  7. setTimeout(resolve, 2000);
  8. });
  9. }
  10. });
  11. };
  12. ReactDOM.render(
  13. <span>
  14. <Button onClick={popupConfirm} type="primary">
  15. confirm
  16. </Button>
  17. </span>,
  18. mountNode
  19. );

简单确认框

Dialog提供alertconfirm两种方式进行便捷调用

Dialog 弹窗 - 图9

查看源码在线预览

  1. import { Dialog, Button, LocaleProvider } from "@icedesign/base";
  2. const popupAlert = () => {
  3. Dialog.alert({
  4. content: "Alert content",
  5. closable: false,
  6. title: "Alert",
  7. onOk: () => {
  8. Dialog.alert({ content: "alert content" });
  9. }
  10. });
  11. };
  12. const popupConfirm = () => {
  13. Dialog.confirm({
  14. content: "Confirm content",
  15. title: "Confirm"
  16. });
  17. };
  18. const setLocale = () => {
  19. LocaleProvider.set("en-us");
  20. };
  21. const popupCustomize = () => {
  22. const dialog = Dialog.alert({
  23. needWrapper: false,
  24. content: "Alert content",
  25. title: "Alert",
  26. footer: (
  27. <Button type="primary" onClick={() => dialog.hide()}>
  28. Another button
  29. </Button>
  30. )
  31. });
  32. };
  33. ReactDOM.render(
  34. <span>
  35. <Button onClick={setLocale}>Set Locale</Button> &nbsp;
  36. <Button onClick={popupAlert}>Alert</Button> &nbsp;
  37. <Button onClick={popupConfirm}>Confirm</Button> &nbsp;
  38. <Button onClick={popupCustomize}>Customize content & button</Button>
  39. </span>,
  40. mountNode
  41. );

简单确认框

通过shouldUpdatePosition更新对话框的位置

Dialog 弹窗 - 图10

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. const content = <p>View messages from buyers & suppliers</p>;
  3. class App extends React.Component {
  4. state = {
  5. visible: false,
  6. content
  7. };
  8. render() {
  9. return (
  10. <span>
  11. <Button onClick={this.onOpen} type="primary">
  12. Open Dialog
  13. </Button>
  14. <Dialog
  15. visible={this.state.visible}
  16. onOk={this.onClose}
  17. onCancel={this.onClose}
  18. shouldUpdatePosition
  19. onClose={this.onClose}
  20. title="Alibaba.com"
  21. >
  22. <Button type="primary" onClick={this.modifyContent}>
  23. Modify content.
  24. </Button>
  25. {this.state.content}
  26. </Dialog>
  27. </span>
  28. );
  29. }
  30. onOpen = () => {
  31. this.setState({
  32. visible: true
  33. });
  34. };
  35. onClose = () => {
  36. this.setState({
  37. visible: false,
  38. content
  39. });
  40. };
  41. modifyContent = () => {
  42. this.setState({
  43. content: (
  44. <div>
  45. <h3>Your one-stop communication tool!</h3>
  46. <ul>
  47. <li>View messages from buyers & suppliers</li>
  48. <li>Negotiate the details of your order</li>
  49. </ul>
  50. </div>
  51. )
  52. });
  53. };
  54. }
  55. ReactDOM.render(<App />, mountNode);

设置宽度等样式

通过style设置dialog的宽度或者top等样式

Dialog 弹窗 - 图11

查看源码在线预览

  1. import { Dialog, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. visible: false,
  7. align: "cc cc",
  8. style: {
  9. width: "80%"
  10. }
  11. };
  12. }
  13. render() {
  14. return (
  15. <span>
  16. <Button onClick={this.open.bind(this)} type="primary">
  17. Open dialog
  18. </Button>
  19. <Dialog
  20. visible={this.state.visible}
  21. onOk={this.onClose.bind(this)}
  22. onCancel={this.onClose.bind(this)}
  23. onClose={this.onClose.bind(this)}
  24. title="Welcome to Alibaba.com"
  25. style={this.state.style}
  26. align={this.state.align}
  27. >
  28. <h3>Your one-stop communication tool!</h3>
  29. <ul>
  30. <li>View messages from buyers & suppliers</li>
  31. <li>Negotiate the details of your order</li>
  32. </ul>
  33. <Button onClick={this.setPosition.bind(this)}>Set position</Button>
  34. </Dialog>
  35. </span>
  36. );
  37. }
  38. onClose() {
  39. this.setState({
  40. visible: false
  41. });
  42. }
  43. open() {
  44. this.setState({
  45. visible: true
  46. });
  47. }
  48. setPosition() {
  49. this.setState({
  50. align: false,
  51. style: {
  52. top: "10px"
  53. }
  54. });
  55. }
  56. }
  57. ReactDOM.render(<App />, mountNode);

相关区块

Dialog 弹窗 - 图12

暂无相关区块