toast.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import transition from '../mixins/transition';
  11. const { prefix } = config;
  12. const name = `${prefix}-toast`;
  13. let Toast = class Toast extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`];
  17. this.options = {
  18. multipleSlots: true,
  19. };
  20. this.behaviors = [transition()];
  21. this.hideTimer = null;
  22. this.data = {
  23. prefix,
  24. classPrefix: name,
  25. typeMapIcon: '',
  26. };
  27. this.properties = props;
  28. this.methods = {
  29. show(options) {
  30. if (this.hideTimer)
  31. clearTimeout(this.hideTimer);
  32. const iconMap = {
  33. loading: 'loading',
  34. success: 'check-circle',
  35. fail: 'error-circle',
  36. };
  37. const typeMapIcon = iconMap[options === null || options === void 0 ? void 0 : options.theme] || '';
  38. const defaultOptions = {
  39. direction: props.direction.value,
  40. duration: props.duration.value,
  41. icon: props.icon.value,
  42. message: props.message.value,
  43. placement: props.placement.value,
  44. preventScrollThrough: props.preventScrollThrough.value,
  45. theme: props.theme.value,
  46. };
  47. const data = Object.assign(Object.assign(Object.assign({}, defaultOptions), options), { visible: true, typeMapIcon });
  48. const { duration } = data;
  49. this.setData(data);
  50. if (duration > 0) {
  51. this.hideTimer = setTimeout(() => {
  52. this.hide();
  53. }, duration);
  54. }
  55. },
  56. hide() {
  57. var _a, _b;
  58. this.setData({ visible: false });
  59. (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.close) === null || _b === void 0 ? void 0 : _b.call(_a);
  60. this.triggerEvent('close');
  61. },
  62. destroyed() {
  63. if (this.hideTimer) {
  64. clearTimeout(this.hideTimer);
  65. this.hideTimer = null;
  66. }
  67. this.triggerEvent('destory');
  68. },
  69. };
  70. }
  71. detached() {
  72. this.destroyed();
  73. }
  74. };
  75. Toast = __decorate([
  76. wxComponent()
  77. ], Toast);
  78. export default Toast;