Popup弹出框 - 图1

Popup 弹出框

基本用法

  1. import { Popup, Cell, Button, Picker, Toast, Modal, Loading } from 'zarm';
  2. const SINGLE_DATA = [
  3. { value: '1', label: '选项一' },
  4. { value: '2', label: '选项二' },
  5. ];
  6. class Demo extends React.Component {
  7. state = {
  8. visible: {
  9. popBottom: false,
  10. popTop: false,
  11. popLeft: false,
  12. popRight: false,
  13. picker: false,
  14. popSpec: false,
  15. popCenterSpec: false,
  16. },
  17. value: '',
  18. };
  19. toggle = (key) => {
  20. const visible = this.state.visible;
  21. this.setState({
  22. visible:{
  23. ...visible,
  24. [key]: !visible[key]
  25. }
  26. });
  27. }
  28. render() {
  29. const { visible, value } = this.state;
  30. return (
  31. <div>
  32. <Cell
  33. description={
  34. <Button size="xs" onClick={() => {
  35. this.toggle('popTop');
  36. setTimeout(() => {
  37. this.toggle('popTop');
  38. }, 3000);
  39. }}>开启</Button>
  40. }
  41. >
  42. 从上方弹出
  43. </Cell>
  44. <Cell
  45. description={
  46. <Button size="xs" onClick={() => { this.toggle('popBottom'); }}>开启</Button>
  47. }
  48. >
  49. 从下方弹出
  50. </Cell>
  51. <Cell
  52. description={
  53. <Button size="xs" onClick={() => this.toggle('popLeft')}>开启</Button>
  54. }
  55. >
  56. 从左侧弹出
  57. </Cell>
  58. <Cell
  59. description={
  60. <Button size="xs" onClick={() => this.toggle('popRight')}>开启</Button>
  61. }
  62. >
  63. 从右侧弹出
  64. </Cell>
  65. <Cell
  66. description={
  67. <Button size="xs" onClick={() => this.toggle('popCenter')}>开启</Button>
  68. }
  69. >
  70. 从中间弹出
  71. </Cell>
  72. <Cell
  73. description={
  74. <Button size="xs" onClick={() => this.toggle('popSpec')}>开启</Button>
  75. }
  76. >
  77. 自定义挂载节点
  78. </Cell>
  79. <Popup
  80. visible={visible.popTop}
  81. direction="top"
  82. mask={false}
  83. afterClose={() => { console.log('关闭'); }}
  84. >
  85. <div className="popup-box-top">
  86. 更新成功
  87. </div>
  88. </Popup>
  89. <Popup
  90. visible={visible.popBottom}
  91. direction="bottom"
  92. onMaskClick={() => { this.toggle('popBottom')}}
  93. afterOpen={() => console.log('打开')}
  94. afterClose={() => console.log('关闭')}
  95. destroy={false}
  96. >
  97. <div className="popup-box">
  98. <Button size="xs" onClick={() => { this.toggle('picker'); }}>打开Picker</Button>
  99. </div>
  100. </Popup>
  101. <Picker
  102. visible={visible.picker}
  103. value={value}
  104. dataSource={SINGLE_DATA}
  105. onOk={(selected) => {
  106. console.log('Picker onOk: ', selected);
  107. this.setState({ value: selected.map(item => item.value) });
  108. Toast.show(JSON.stringify(selected));
  109. this.toggle('picker');
  110. }}
  111. onCancel={() => this.toggle('picker')}
  112. />
  113. <Popup
  114. visible={visible.popLeft}
  115. onMaskClick={() => this.toggle('popLeft')}
  116. direction="left"
  117. afterClose={() => console.log('关闭')}
  118. >
  119. <div className="popup-box-left">
  120. <Button size="xs" onClick={() => this.toggle('popLeft')}>关闭弹层</Button>
  121. </div>
  122. </Popup>
  123. <Popup
  124. visible={visible.popRight}
  125. onMaskClick={() => this.toggle('popRight')}
  126. direction="right"
  127. afterClose={() => console.log('关闭')}
  128. >
  129. <div className="popup-box">
  130. <Button size="xs" onClick={() => this.toggle('popRight')}>关闭弹层</Button>
  131. </div>
  132. </Popup>
  133. <Popup
  134. visible={visible.popCenter}
  135. direction="center"
  136. width="70%"
  137. afterClose={() => console.log('关闭')}
  138. >
  139. <div className="popup-box">
  140. <Button size="xs" onClick={() => this.toggle('popCenter')}>关闭弹层</Button>
  141. </div>
  142. </Popup>
  143. <Popup
  144. visible={visible.popCenterSpec}
  145. direction="center"
  146. width="70%"
  147. afterClose={() => console.log('关闭')}
  148. getContainer={() => {
  149. return this.popupRef.portalRef.popup
  150. }}
  151. >
  152. <div className="popup-box">
  153. <Button size="xs" onClick={() => this.toggle('popCenterSpec')}>关闭弹层</Button>
  154. </div>
  155. </Popup>
  156. <Popup
  157. visible={visible.popSpec}
  158. onMaskClick={() => this.toggle('popSpec')}
  159. afterClose={() => console.log('关闭')}
  160. ref={ref => this.popupRef = ref}
  161. destroy={false}
  162. >
  163. <div className="popup-box-bottom">
  164. <Button size="xs" onClick={() => this.toggle('popCenterSpec')}>打开弹层</Button>
  165. <p>打开的modal挂载此popup上</p>
  166. </div>
  167. </Popup>
  168. </div>
  169. )
  170. }
  171. }
  172. ReactDOM.render(<Demo />, mountNode);

API

属性类型默认值说明
visiblebooleanfalse是否显示
directionstring'bottom'弹出方向,可选值 top, bottom, left, right, center
animationTypestring'fade'当弹出方向为中间位置(direction="center")时的动画效果,可选值 fade, door, flip, rotate, zoom,moveUp, moveDown, moveLeft, moveRight,slideUp, slideDown, slideLeft, slideRight
animationDurationnumber200动画执行时间(单位:毫秒)
widthstring | number-弹层宽度
maskbooleantrue是否展示遮罩层
maskTypestring'normal'遮罩层的类型,可选值 transparent, normal
destroybooleantrue弹层关闭后是否移除节点
afterOpen() => void-弹层展示后的回调
afterClose() => void-弹层关闭后的回调
onMaskClick() => void-点击遮罩层时触发的回调函数
getContainerHTMLElement | () => HTMLElementdocument.body指定 Popup 挂载的 HTML 节点