toptips.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Component({
  2. options: {
  3. addGlobalClass: true
  4. },
  5. properties: {
  6. type: {
  7. type: String,
  8. value: 'error',
  9. observer: '_typeChange'
  10. },
  11. show: {
  12. type: Boolean,
  13. value: false,
  14. observer: '_showChange'
  15. },
  16. msg: {
  17. type: String,
  18. value: ''
  19. },
  20. delay: {
  21. type: Number,
  22. value: 2000
  23. },
  24. extClass: {
  25. type: String,
  26. value: ''
  27. }
  28. },
  29. data: {
  30. typeClassMap: {
  31. warn: 'weui-toptips_warn',
  32. info: 'weui-toptips_info',
  33. success: 'weui-toptips_success',
  34. error: 'weui-toptips_error'
  35. }
  36. },
  37. lifetimes: {
  38. attached() {
  39. const data = this.data
  40. this.setData({
  41. className: data.typeClassMap[data.type] || ''
  42. })
  43. },
  44. },
  45. methods: {
  46. _typeChange(newVal) {
  47. this.setData({
  48. className: this.data.typeClassMap[newVal] || ''
  49. })
  50. return newVal
  51. },
  52. _showChange(newVal) {
  53. this._showToptips(newVal)
  54. },
  55. _showToptips(newVal) {
  56. if (newVal && this.data.delay) {
  57. setTimeout(() => {
  58. this.setData({
  59. show: false
  60. }, () => {
  61. // tooltips 隐藏了,触发 hide 事件
  62. this.triggerEvent('hide', {}, {})
  63. })
  64. }, this.data.delay)
  65. }
  66. this.setData({
  67. show: newVal
  68. })
  69. }
  70. }
  71. })