index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import styleToCssString from '../helpers/libs/styleToCssString'
  4. import { safeAreaProps } from '../helpers/mixins/safeAreaBehavior'
  5. baseComponent({
  6. relations: {
  7. '../tabbar-item/index': {
  8. type: 'child',
  9. observer() {
  10. this.callDebounceFn(this.updated)
  11. },
  12. },
  13. },
  14. properties: {
  15. prefixCls: {
  16. type: String,
  17. value: 'wux-tabbar',
  18. },
  19. defaultCurrent: {
  20. type: String,
  21. value: '',
  22. },
  23. current: {
  24. type: String,
  25. value: '',
  26. },
  27. controlled: {
  28. type: Boolean,
  29. value: false,
  30. },
  31. theme: {
  32. type: String,
  33. value: 'balanced',
  34. },
  35. backgroundColor: {
  36. type: String,
  37. value: '#fff',
  38. },
  39. position: {
  40. type: String,
  41. value: '',
  42. },
  43. ...safeAreaProps,
  44. },
  45. data: {
  46. tabbarStyle: '',
  47. activeKey: '',
  48. keys: [],
  49. },
  50. computed: {
  51. classes: ['prefixCls, position', function(prefixCls, position) {
  52. const wrap = classNames(prefixCls, {
  53. [`${prefixCls}--${position}`]: position,
  54. })
  55. const tabbar = `${prefixCls}-wrap`
  56. return {
  57. wrap,
  58. tabbar,
  59. }
  60. }],
  61. },
  62. observers: {
  63. current(newVal) {
  64. if (this.data.controlled) {
  65. this.updated(newVal)
  66. }
  67. },
  68. backgroundColor(newVal) {
  69. this.updateStyle(newVal)
  70. },
  71. },
  72. methods: {
  73. updated(activeKey = this.data.activeKey) {
  74. if (this.data.activeKey !== activeKey) {
  75. this.setData({ activeKey })
  76. }
  77. this.changeCurrent(activeKey)
  78. },
  79. changeCurrent(activeKey) {
  80. const elements = this.getRelationsByName('../tabbar-item/index')
  81. if (elements.length > 0) {
  82. elements.forEach((element, index) => {
  83. const key = element.data.key || String(index)
  84. const current = key === activeKey
  85. element.changeCurrent(current, key, this.data.theme, elements.length)
  86. })
  87. }
  88. if (this.data.keys.length !== elements.length) {
  89. this.setData({
  90. keys: elements.map((element) => element.data),
  91. })
  92. }
  93. },
  94. emitEvent(key) {
  95. this.triggerEvent('change', {
  96. key,
  97. keys: this.data.keys,
  98. })
  99. },
  100. setActiveKey(activeKey) {
  101. if (!this.data.controlled) {
  102. this.updated(activeKey)
  103. }
  104. this.emitEvent(activeKey)
  105. },
  106. updateStyle(backgroundColor) {
  107. const tabbarStyle = styleToCssString({
  108. backgroundColor,
  109. })
  110. if (tabbarStyle !== this.data.tabbarStyle) {
  111. this.setData({
  112. tabbarStyle,
  113. })
  114. }
  115. },
  116. },
  117. ready() {
  118. const { defaultCurrent, current, controlled, backgroundColor } = this.data
  119. const activeKey = controlled ? current : defaultCurrent
  120. this.updated(activeKey)
  121. this.updateStyle(backgroundColor)
  122. },
  123. })