index.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { VantComponent } from '../common/component';
  2. import { button } from '../mixins/button';
  3. import { openType } from '../mixins/open-type';
  4. VantComponent({
  5. mixins: [button, openType],
  6. props: {
  7. show: Boolean,
  8. title: String,
  9. message: String,
  10. useSlot: Boolean,
  11. className: String,
  12. customStyle: String,
  13. asyncClose: Boolean,
  14. messageAlign: String,
  15. showCancelButton: Boolean,
  16. closeOnClickOverlay: Boolean,
  17. confirmButtonOpenType: String,
  18. zIndex: {
  19. type: Number,
  20. value: 2000
  21. },
  22. confirmButtonText: {
  23. type: String,
  24. value: '确认'
  25. },
  26. cancelButtonText: {
  27. type: String,
  28. value: '取消'
  29. },
  30. showConfirmButton: {
  31. type: Boolean,
  32. value: true
  33. },
  34. overlay: {
  35. type: Boolean,
  36. value: true
  37. },
  38. transition: {
  39. type: String,
  40. value: 'scale'
  41. }
  42. },
  43. data: {
  44. loading: {
  45. confirm: false,
  46. cancel: false
  47. }
  48. },
  49. watch: {
  50. show(show) {
  51. !show && this.stopLoading();
  52. }
  53. },
  54. methods: {
  55. onConfirm() {
  56. this.handleAction('confirm');
  57. },
  58. onCancel() {
  59. this.handleAction('cancel');
  60. },
  61. onClickOverlay() {
  62. this.onClose('overlay');
  63. },
  64. handleAction(action) {
  65. if (this.data.asyncClose) {
  66. this.set({
  67. [`loading.${action}`]: true
  68. });
  69. }
  70. this.onClose(action);
  71. },
  72. close() {
  73. this.set({
  74. show: false
  75. });
  76. },
  77. stopLoading() {
  78. this.set({
  79. loading: {
  80. confirm: false,
  81. cancel: false
  82. }
  83. });
  84. },
  85. onClose(action) {
  86. if (!this.data.asyncClose) {
  87. this.close();
  88. }
  89. this.$emit('close', action);
  90. // 把 dialog 实例传递出去,可以通过 stopLoading() 在外部关闭按钮的 loading
  91. this.$emit(action, { dialog: this });
  92. const callback = this.data[action === 'confirm' ? 'onConfirm' : 'onCancel'];
  93. if (callback) {
  94. callback(this);
  95. }
  96. }
  97. }
  98. });