index.js 3.3 KB

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