Modal模态框 - 图1

Modal 模态框

基本用法

  1. import { Modal, Cell, Button, Select } from 'zarm';
  2. class Demo extends React.Component {
  3. state = {
  4. modal1: false,
  5. modal2: false,
  6. modal3: false,
  7. modal4: false,
  8. modal5: false,
  9. specModal: false,
  10. animationType: 'fade',
  11. };
  12. myRef = React.createRef();
  13. open = (key) => {
  14. this.setState({
  15. [`${key}`]: true,
  16. });
  17. }
  18. close = (key) => {
  19. this.setState({
  20. [`${key}`]: false,
  21. });
  22. }
  23. render() {
  24. const { modal1, modal2, modal3, modal4, modal5, animationType, specModal } = this.state;
  25. return (
  26. <>
  27. <Cell
  28. description={
  29. <Button size="xs" onClick={() => this.open('modal1')}>开启</Button>
  30. }
  31. >
  32. 普通
  33. </Cell>
  34. <Cell
  35. description={
  36. <Button size="xs" onClick={() => this.open('modal2')}>开启</Button>
  37. }
  38. >
  39. 有底部按钮
  40. </Cell>
  41. <Cell
  42. description={
  43. <Button size="xs" onClick={() => this.open('modal3')}>开启</Button>
  44. }
  45. >
  46. 遮罩层可关闭
  47. </Cell>
  48. <Cell
  49. description={
  50. <Button size="xs" onClick={() => this.open('modal4')}>开启</Button>
  51. }
  52. >
  53. 无头部,无底部
  54. </Cell>
  55. <Cell
  56. title="动画效果"
  57. description={
  58. <Button size="xs" onClick={() => this.open('modal5')}>开启</Button>
  59. }
  60. >
  61. <Select
  62. value={animationType}
  63. dataSource={[
  64. { value: 'fade', label: '淡出淡入效果(fade)' },
  65. { value: 'zoom', label: '缩放效果(zoom)' },
  66. { value: 'rotate', label: '旋转效果(rotate)' },
  67. { value: 'door', label: '开关门效果(door)' },
  68. { value: 'flip', label: '翻转效果(flip)' },
  69. { value: 'moveUp', label: '移出移入效果(moveUp)' },
  70. { value: 'moveDown', label: '移出移入效果(moveDown)' },
  71. { value: 'moveLeft', label: '移出移入效果(moveLeft)' },
  72. { value: 'moveRight', label: '移出移入效果(moveRight)' },
  73. { value: 'slideUp', label: '滑出滑入效果(slideUp)' },
  74. { value: 'slideDown', label: '滑出滑入效果(slideDown)' },
  75. { value: 'slideLeft', label: '滑出滑入效果(slideLeft)' },
  76. { value: 'slideRight', label: '滑出滑入效果(slideRight)' },
  77. ]}
  78. onOk={(selected) => {
  79. this.setState({
  80. animationType: selected.map(item => item.value),
  81. });
  82. }}
  83. />
  84. </Cell>
  85. <Cell
  86. description={
  87. <Button size="xs" onClick={() => this.open('specModal')}>开启</Button>
  88. }
  89. >
  90. 挂载到指定dom节点
  91. </Cell>
  92. <Modal
  93. visible={modal1}
  94. title="标题"
  95. closable
  96. onCancel={() => this.close('modal1')}
  97. >
  98. 模态框内容
  99. </Modal>
  100. <Modal
  101. title="标题"
  102. visible={modal2}
  103. closable
  104. onCancel={() => this.close('modal2')}
  105. footer={
  106. <Button
  107. block
  108. theme="primary"
  109. onClick={() => this.close('modal2')}
  110. >确认</Button>
  111. }
  112. >
  113. 模态框内容
  114. </Modal>
  115. <Modal
  116. visible={modal3}
  117. title="标题"
  118. maskClosable
  119. onCancel={() => this.close('modal3')}
  120. >
  121. 点击遮罩层关闭
  122. </Modal>
  123. <Modal
  124. visible={modal4}
  125. maskClosable
  126. onCancel={() => this.close('modal4')}
  127. >
  128. 无头部,无底部
  129. </Modal>
  130. <Modal
  131. visible={modal5}
  132. animationType={animationType}
  133. maskClosable
  134. onCancel={() => this.close('modal5')}
  135. >
  136. <div style={{ height: 100 }}>
  137. 当前使用的动画类型animationType'{animationType}'
  138. </div>
  139. </Modal>
  140. <Modal
  141. visible={specModal}
  142. maskClosable
  143. onCancel={() => this.close('specModal')}
  144. getContainer={() => this.myRef.current}
  145. >
  146. 挂载到指定dom节点
  147. </Modal>
  148. <div
  149. id="test-div"
  150. style={{ position: 'relative', zIndex: 1 }}
  151. ref={this.myRef}
  152. />
  153. </>
  154. )
  155. }
  156. }
  157. ReactDOM.render(<Demo />, mountNode);

警告框 Alert

  1. import { Cell, Button, Alert, Confirm, Modal } from 'zarm';
  2. class Demo extends React.Component {
  3. render() {
  4. return (
  5. <>
  6. <Cell
  7. description={
  8. <Button size="xs" onClick={
  9. () => {
  10. const modal = Modal.alert({
  11. title: '静态调用的title',
  12. content: '静态调用的body',
  13. onCancel: () => {
  14. modal.hide();
  15. }
  16. });
  17. }}>开启</Button>
  18. }
  19. >
  20. 静态调用(静态关闭)
  21. </Cell>
  22. <Cell
  23. description={
  24. <Button size="xs" onClick={() => {
  25. const modal = Modal.alert({
  26. title: '静态调用的title',
  27. content: '静态调用的body,使用promise关闭',
  28. onCancel: () => {
  29. return new Promise((resolve, reject) => {
  30. resolve();
  31. // setTimeout(Math.random() > 0.5 ? resolve : reject, 500);
  32. }).catch(() => {
  33. window.alert('出错啦,弹窗无法关闭,继续点击试试');
  34. return false; // 返回false,可使弹窗无法关闭
  35. })
  36. }
  37. });
  38. }}>开启</Button>
  39. }
  40. >
  41. 静态调用(使用promise关闭)
  42. </Cell>
  43. <div
  44. id="test-div"
  45. style={{ position: 'relative', zIndex: 1 }}
  46. ref={this.myRef}
  47. />
  48. </>
  49. )
  50. }
  51. }
  52. ReactDOM.render(<Demo />, mountNode);

确认框 Confirm

  1. import { Cell, Button, Confirm, Modal } from 'zarm';
  2. class Demo extends React.Component {
  3. render() {
  4. return (
  5. <>
  6. <Cell
  7. description={
  8. <Button size="xs" onClick={() => {
  9. const modal = Modal.confirm({
  10. title: '确认信息',
  11. content: '静态调用的body',
  12. onCancel: () => {
  13. console.log('点击cancel');
  14. },
  15. onOk: () => {
  16. console.log('点击ok');
  17. }
  18. });
  19. }}>开启</Button>
  20. }
  21. >
  22. 静态调用(静态关闭)
  23. </Cell>
  24. <Cell
  25. description={
  26. <Button size="xs" onClick={() => {
  27. const modal = Modal.confirm({
  28. title: '静态调用的title',
  29. content: '静态调用的body,使用promise关闭',
  30. onCancel: () => {
  31. return new Promise((resolve, reject) => {
  32. resolve();
  33. // setTimeout(Math.random() > 0.5 ? resolve : reject, 500);
  34. }).catch(() => {
  35. window.alert('出错啦,弹窗无法关闭,继续点击试试');
  36. return false; // 返回false,可使弹窗无法关闭
  37. })
  38. }
  39. });
  40. }}>开启</Button>
  41. }
  42. >
  43. 静态调用(使用promise关闭)
  44. </Cell>
  45. </>
  46. )
  47. }
  48. }
  49. ReactDOM.render(<Demo />, mountNode);

API

属性类型默认值说明
shapestring'radius'形状,可选值 rectradius
visiblebooleanfalse是否显示
animationTypestring'fade'动画效果,可选值 fade, door, flip, rotate, zoom,moveUp, moveDown, moveLeft, moveRight,slideUp, slideDown, slideLeft, slideRight
animationDurationnumber200动画执行时间(单位:毫秒)
widthstring | number'70%'宽度
maskbooleantrue是否展示遮罩层
maskTypestring'normal'遮罩层的类型,可选值 transparent, normal
maskClosablebooleanfalse是否点击遮罩层时关闭,需要和onCancel一起使用
closablebooleanfalse右上角是否显示关闭按钮,需要和onCancel一起使用
onCancel() => void-如果maskClosable或closable为true,那么点击遮罩或者右上角关闭按钮会调用此函数
titleReactNode-标题
footerReactNode-弹窗底部内容
destroybooleantrue弹层关闭后是否移除节点
afterOpen() => void-模态框打开后的回调
afterClose() => void-模态框关闭后的回调
getContainerHTMLElement | () => HTMLElementdocument.body指定 Modal 挂载的 HTML 节点

静态方法

  1. // 显示警告框,不传onCancel也可关闭,如需做更多操作,参考下方confirm的例子
  2. const alert = Modal.alert({
  3. title: '静态调用的title',
  4. content: '静态调用的body',
  5. });
  6. // 显示确认框,若关闭时需要promise,onOk、onCancel均支持promise
  7. const confirm = Modal.confirm({
  8. title: '静态调用的title',
  9. content: '静态调用的body,使用promise关闭',
  10. onOk: () => {
  11. return fetch.get('xxx.api').then((res) => {
  12. if(res.code === 0) {
  13. return true; // 关闭弹窗
  14. } else {
  15. return false; // 阻止弹窗关闭
  16. }
  17. }).catch(...);
  18. }
  19. });
属性类型默认值说明
titleReactNode-弹出框的标题
contentReactNode-弹出框的内容
cancelTextReactNode'关闭'(Alert)/'取消'(Confirm)取消按钮的内容
okTextReactNode'确认'确认按钮的内容
onOk() => void-点击“确认”后的回调函数(Confirm)
onCancel() => void-点击“关闭/取消”后的回调函数