Modal 对话框

概述

模态对话框,在浮层中显示,引导用户进行相关操作。

使用指南

在 .json 中引入组件

  1. "usingComponents": {
  2. "i-modal": "../../dist/modal/index"
  3. }

示例

  1. <view style="margin-top: 100px;">
  2. <i-button bind:click="handleOpen1">普通对话框</i-button>
  3. <i-button bind:click="handleOpen2">无标题对话框</i-button>
  4. <i-button bind:click="handleOpen3">自定义按钮对话框</i-button>
  5. <i-button bind:click="handleOpen4">纵向按钮,自定义颜色及图标</i-button>
  6. <i-button bind:click="handleOpen5">异步操作</i-button>
  7. </view>
  8. <i-modal title="标题" visible="{{ visible1 }}" bind:ok="handleClose1" bind:cancel="handleClose1">
  9. <view>一些文本</view>
  10. <view>一些文本</view>
  11. <view>一些文本</view>
  12. <view>一些文本</view>
  13. <view>一些文本</view>
  14. <view>一些文本</view>
  15. <view>一些文本</view>
  16. <view>一些文本</view>
  17. <view>一些文本</view>
  18. </i-modal>
  19. <i-modal visible="{{ visible2 }}" bind:ok="handleClose2" bind:cancel="handleClose2">
  20. <view>这是一个无标题的对话框</view>
  21. </i-modal>
  22. <i-modal title="支付" visible="{{ visible3 }}" actions="{{ actions3 }}" bind:click="handleClick3">
  23. <view>请选择支付方式</view>
  24. </i-modal>
  25. <i-modal title="纵向排列的按钮" visible="{{ visible4 }}" actions="{{ actions4 }}" action-mode="{{ vertical }}" bind:click="handleClick4">
  26. </i-modal>
  27. <i-modal title="删除确认" visible="{{ visible5 }}" actions="{{ actions5 }}" bind:click="handleClick5">
  28. <view>删除后无法恢复哦</view>
  29. </i-modal>
  30. <i-message id="message" />
  1. // 关于本示例的 $Message ,可以查看 Message 组件的介绍
  2. const { $Message } = require('../../dist/base/index');
  3. Page({
  4. data: {
  5. visible1: false,
  6. visible2: false,
  7. visible3: false,
  8. visible4: false,
  9. visible5: false,
  10. actions3: [
  11. {
  12. name: '现金支付',
  13. color: '#2d8cf0',
  14. },
  15. {
  16. name: '微信支付',
  17. color: '#19be6b'
  18. },
  19. {
  20. name: '取消'
  21. }
  22. ],
  23. actions4: [
  24. {
  25. name: '按钮1'
  26. },
  27. {
  28. name: '按钮2',
  29. color: '#ff9900'
  30. },
  31. {
  32. name: '按钮3',
  33. icon: 'search'
  34. }
  35. ],
  36. actions5: [
  37. {
  38. name: '取消'
  39. },
  40. {
  41. name: '删除',
  42. color: '#ed3f14',
  43. loading: false
  44. }
  45. ]
  46. },
  47. handleOpen1 () {
  48. this.setData({
  49. visible1: true
  50. });
  51. },
  52. handleClose1 () {
  53. this.setData({
  54. visible1: false
  55. });
  56. },
  57. handleOpen2 () {
  58. this.setData({
  59. visible2: true
  60. });
  61. },
  62. handleClose2 () {
  63. this.setData({
  64. visible2: false
  65. });
  66. },
  67. handleOpen3 () {
  68. this.setData({
  69. visible3: true
  70. });
  71. },
  72. handleClick3 ({ detail }) {
  73. const index = detail.index;
  74. if (index === 0) {
  75. $Message({
  76. content: '点击了现金支付'
  77. });
  78. } else if (index === 1) {
  79. $Message({
  80. content: '点击了微信支付'
  81. });
  82. }
  83. this.setData({
  84. visible3: false
  85. });
  86. },
  87. handleOpen4 () {
  88. this.setData({
  89. visible4: true
  90. });
  91. },
  92. handleClick4 () {
  93. this.setData({
  94. visible4: false
  95. });
  96. },
  97. handleOpen5 () {
  98. this.setData({
  99. visible5: true
  100. });
  101. },
  102. handleClick5 ({ detail }) {
  103. if (detail.index === 0) {
  104. this.setData({
  105. visible5: false
  106. });
  107. } else {
  108. const action = [...this.data.actions5];
  109. action[1].loading = true;
  110. this.setData({
  111. actions5: action
  112. });
  113. setTimeout(() => {
  114. action[1].loading = false;
  115. this.setData({
  116. visible5: false,
  117. actions5: action
  118. });
  119. $Message({
  120. content: '删除成功!',
  121. type: 'success'
  122. });
  123. }, 2000);
  124. }
  125. }
  126. });

API

Modal properties

属性说明类型默认值
visible是否显示组件Booleanfalse
title标题String-
show-ok是否显示确定按钮Booleantrue
show-cancel是否显示取消按钮Booleantrue
ok-text确定按钮的文案String确定
cancel-text取消按钮的文案String取消
actions按钮组,具体项参照后面的表格,设置此值后,则默认的确定和取消按钮不予显示Array[]
action-mode按钮的排列方向,可选值为 horizontal 或 verticalStringhorizontal

Modal events

事件名说明返回值
bind:click点击某个按钮时触发,返回按钮所在 actions 中的索引{ index }
bind:ok点击确定按钮时触发-
bind:cancel点击取消按钮时触发-

Modal actions

属性说明类型默认值
name按钮文案String-
icon按钮图标String-
color按钮文字的颜色String-
loading按钮是否显示为加载中Booleanfalse