index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { VantComponent } from '../common/component';
  2. import { button } from '../mixins/button';
  3. import { openType } from '../mixins/open-type';
  4. import { GRAY, RED } from '../common/color';
  5. import { toPromise } from '../common/utils';
  6. VantComponent({
  7. mixins: [button, openType],
  8. props: {
  9. show: {
  10. type: Boolean,
  11. observer(show) {
  12. !show && this.stopLoading();
  13. },
  14. },
  15. title: String,
  16. message: String,
  17. theme: {
  18. type: String,
  19. value: 'default',
  20. },
  21. useSlot: Boolean,
  22. className: String,
  23. customStyle: String,
  24. asyncClose: Boolean,
  25. messageAlign: String,
  26. beforeClose: null,
  27. overlayStyle: String,
  28. useTitleSlot: Boolean,
  29. showCancelButton: Boolean,
  30. closeOnClickOverlay: Boolean,
  31. confirmButtonOpenType: String,
  32. width: null,
  33. zIndex: {
  34. type: Number,
  35. value: 2000,
  36. },
  37. confirmButtonText: {
  38. type: String,
  39. value: '确认',
  40. },
  41. cancelButtonText: {
  42. type: String,
  43. value: '取消',
  44. },
  45. confirmButtonColor: {
  46. type: String,
  47. value: RED,
  48. },
  49. cancelButtonColor: {
  50. type: String,
  51. value: GRAY,
  52. },
  53. showConfirmButton: {
  54. type: Boolean,
  55. value: true,
  56. },
  57. overlay: {
  58. type: Boolean,
  59. value: true,
  60. },
  61. transition: {
  62. type: String,
  63. value: 'scale',
  64. },
  65. },
  66. data: {
  67. loading: {
  68. confirm: false,
  69. cancel: false,
  70. },
  71. },
  72. methods: {
  73. onConfirm() {
  74. this.handleAction('confirm');
  75. },
  76. onCancel() {
  77. this.handleAction('cancel');
  78. },
  79. onClickOverlay() {
  80. this.onClose('overlay');
  81. },
  82. close(action) {
  83. this.setData({ show: false });
  84. wx.nextTick(() => {
  85. this.$emit('close', action);
  86. const { callback } = this.data;
  87. if (callback) {
  88. callback(action, this);
  89. }
  90. });
  91. },
  92. stopLoading() {
  93. this.setData({
  94. loading: {
  95. confirm: false,
  96. cancel: false,
  97. },
  98. });
  99. },
  100. handleAction(action) {
  101. this.$emit(action, { dialog: this });
  102. const { asyncClose, beforeClose } = this.data;
  103. if (!asyncClose && !beforeClose) {
  104. this.close(action);
  105. return;
  106. }
  107. this.setData({
  108. [`loading.${action}`]: true,
  109. });
  110. if (beforeClose) {
  111. toPromise(beforeClose(action)).then((value) => {
  112. if (value) {
  113. this.close(action);
  114. } else {
  115. this.stopLoading();
  116. }
  117. });
  118. }
  119. },
  120. },
  121. });