cl-action-sheet.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view class="cl-action-sheet">
  3. <cl-popup
  4. direction="bottom"
  5. size="auto"
  6. :visible.sync="visible"
  7. :with-header="false"
  8. :close-on-click-modal="closeOnClickModal"
  9. >
  10. <button
  11. class="cl-action-sheet__item"
  12. v-for="(item, index) in list"
  13. :key="index"
  14. :class="[item.disabled ? 'is-disabled' : '']"
  15. :style="{
  16. color: item.color,
  17. fontSize: item.size,
  18. }"
  19. :open-type="item.openType"
  20. @getphonenumber="onEvent(item, 'getphonenumber')"
  21. @getuserinfo="onEvent(item, 'getuserinfo')"
  22. @error="onEvent(item, 'error')"
  23. @opensetting="onEvent(item, 'opensetting')"
  24. @tap="select(index, item)"
  25. >
  26. <text class="cl-action-sheet__icon" :class="[item.icon]"></text>
  27. <text class="cl-action-sheet__label">{{ item.label }}</text>
  28. </button>
  29. <button
  30. class="cl-action-sheet__item cl-action-sheet__item--cancel"
  31. @tap="close('cancel')"
  32. v-if="showCancel"
  33. >
  34. {{ cancelText }}
  35. </button>
  36. </cl-popup>
  37. </view>
  38. </template>
  39. <script>
  40. /**
  41. * actionSheet 操作菜单
  42. * @description 从底部弹出的一个操作菜单,类似于 uni.showActionSheetAPI,同时添加新的支持。
  43. * @tutorial https://docs.cool-js.com/uni/components/operate/action-sheet.html
  44. * @example <cl-action-sheet ref="action-sheet"></cl-action-sheet>
  45. */
  46. export default {
  47. name: "cl-action-sheet",
  48. data() {
  49. return {
  50. visible: false,
  51. list: [],
  52. closeOnClickModal: true,
  53. callback: null,
  54. beforeClose: null,
  55. showCancel: true,
  56. cancelText: null,
  57. };
  58. },
  59. methods: {
  60. open({
  61. list = [],
  62. closeOnClickModal = true,
  63. callback,
  64. beforeClose,
  65. cancelText = "取消",
  66. showCancel = true,
  67. }) {
  68. this.closeOnClickModal = closeOnClickModal;
  69. this.list = list;
  70. this.callback = callback;
  71. this.beforeClose = beforeClose;
  72. this.cancelText = cancelText;
  73. this.showCancel = showCancel;
  74. this.visible = true;
  75. },
  76. close(action) {
  77. const done = () => {
  78. this.visible = false;
  79. if (this.callback) {
  80. this.callback({
  81. action,
  82. });
  83. }
  84. };
  85. if (this.beforeClose) {
  86. this.beforeClose({
  87. action,
  88. done,
  89. });
  90. } else {
  91. done();
  92. }
  93. },
  94. select(index, item) {
  95. if (item.disabled) {
  96. return false;
  97. }
  98. if (item.click) {
  99. item.click(() => {
  100. this.visible = false;
  101. });
  102. } else {
  103. this.close(index);
  104. }
  105. },
  106. onEvent(item, name) {
  107. if (item[name]) {
  108. item[name]();
  109. }
  110. },
  111. },
  112. };
  113. </script>