index.js 3.0 KB

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