index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. props: {
  8. show: {
  9. type: Boolean,
  10. observer(show) {
  11. !show && this.stopLoading();
  12. },
  13. },
  14. title: String,
  15. message: String,
  16. theme: {
  17. type: String,
  18. value: 'default',
  19. },
  20. useSlot: Boolean,
  21. className: String,
  22. customStyle: String,
  23. asyncClose: Boolean,
  24. messageAlign: String,
  25. beforeClose: null,
  26. overlayStyle: String,
  27. useTitleSlot: Boolean,
  28. showCancelButton: Boolean,
  29. closeOnClickOverlay: Boolean,
  30. confirmButtonOpenType: String,
  31. width: null,
  32. zIndex: {
  33. type: Number,
  34. value: 2000,
  35. },
  36. confirmButtonText: {
  37. type: String,
  38. value: '确认',
  39. },
  40. cancelButtonText: {
  41. type: String,
  42. value: '取消',
  43. },
  44. confirmButtonColor: {
  45. type: String,
  46. value: RED,
  47. },
  48. cancelButtonColor: {
  49. type: String,
  50. value: GRAY,
  51. },
  52. showConfirmButton: {
  53. type: Boolean,
  54. value: true,
  55. },
  56. overlay: {
  57. type: Boolean,
  58. value: true,
  59. },
  60. transition: {
  61. type: String,
  62. value: 'scale',
  63. },
  64. },
  65. data: {
  66. loading: {
  67. confirm: false,
  68. cancel: false,
  69. },
  70. callback: (() => { }),
  71. },
  72. methods: {
  73. onConfirm() {
  74. this.handleAction('confirm');
  75. },
  76. onCancel() {
  77. this.handleAction('cancel');
  78. },
  79. onClickOverlay() {
  80. this.close('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. }
  115. else {
  116. this.stopLoading();
  117. }
  118. });
  119. }
  120. },
  121. },
  122. });