index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. callback: () => {},
  72. },
  73. methods: {
  74. onConfirm() {
  75. this.handleAction('confirm');
  76. },
  77. onCancel() {
  78. this.handleAction('cancel');
  79. },
  80. onClickOverlay() {
  81. this.close('overlay');
  82. },
  83. close(action) {
  84. this.setData({ show: false });
  85. wx.nextTick(() => {
  86. this.$emit('close', action);
  87. const { callback } = this.data;
  88. if (callback) {
  89. callback(action, this);
  90. }
  91. });
  92. },
  93. stopLoading() {
  94. this.setData({
  95. loading: {
  96. confirm: false,
  97. cancel: false,
  98. },
  99. });
  100. },
  101. handleAction(action) {
  102. this.$emit(action, { dialog: this });
  103. const { asyncClose, beforeClose } = this.data;
  104. if (!asyncClose && !beforeClose) {
  105. this.close(action);
  106. return;
  107. }
  108. this.setData({
  109. [`loading.${action}`]: true,
  110. });
  111. if (beforeClose) {
  112. toPromise(beforeClose(action)).then((value) => {
  113. if (value) {
  114. this.close(action);
  115. } else {
  116. this.stopLoading();
  117. }
  118. });
  119. }
  120. },
  121. },
  122. });