index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { ToastDefaultProps } from './props';
  2. import fmtEvent from '../_util/fmtEvent';
  3. import '../_util/assert-component2';
  4. Component({
  5. props: ToastDefaultProps,
  6. data: {
  7. show: false,
  8. timer: null,
  9. },
  10. didUpdate: function (prev) {
  11. if (!prev.visible && this.props.visible) {
  12. this.handleShowToast();
  13. }
  14. else if (!this.props.visible && this.data.show) {
  15. this.closeMask();
  16. }
  17. },
  18. didMount: function () {
  19. if (this.props.visible) {
  20. this.handleShowToast();
  21. }
  22. },
  23. methods: {
  24. closeMask: function () {
  25. var _a, _b;
  26. if (this.data.timer) {
  27. clearTimeout(this.data.timer);
  28. }
  29. this.setData({ show: false, timer: null });
  30. (_b = (_a = this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, fmtEvent(this.props, {}));
  31. },
  32. handleShowToast: function () {
  33. var _this = this;
  34. this.setData({ show: true });
  35. if (this.props.duration > 0) {
  36. var timer = setTimeout(function () {
  37. _this.closeMask();
  38. }, this.props.duration);
  39. this.setData({ timer: timer });
  40. }
  41. },
  42. handleClickMask: function () {
  43. if (this.props.showMask && this.props.maskCloseable) {
  44. this.closeMask();
  45. }
  46. },
  47. },
  48. });