uv-tabbar-item.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view
  3. class="uv-tabbar-item"
  4. :style="[$uv.addStyle(customStyle)]"
  5. @tap="clickHandler"
  6. >
  7. <view class="uv-tabbar-item__icon">
  8. <uv-icon
  9. v-if="icon"
  10. :name="icon"
  11. :color="isActive? parentData.activeColor : parentData.inactiveColor"
  12. :size="iconSize? iconSize: parentData.iconSize"
  13. ></uv-icon>
  14. <template v-else>
  15. <slot
  16. v-if="isActive"
  17. name="active-icon"
  18. />
  19. <slot
  20. v-else
  21. name="inactive-icon"
  22. />
  23. </template>
  24. <uv-badge
  25. absolute
  26. :offset="[0, dot ? '34rpx' : badge > 9 ? '14rpx' : '20rpx']"
  27. :customStyle="badgeStyle"
  28. :isDot="dot"
  29. :value="badge || (dot ? 1 : null)"
  30. :show="dot || badge > 0"
  31. ></uv-badge>
  32. </view>
  33. <slot name="text">
  34. <text
  35. class="uv-tabbar-item__text"
  36. :style="{
  37. color: isActive? parentData.activeColor : parentData.inactiveColor
  38. }"
  39. >{{ text }}</text>
  40. </slot>
  41. </view>
  42. </template>
  43. <script>
  44. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  45. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  46. import props from './props.js';
  47. /**
  48. * TabbarItem 底部导航栏子组件
  49. * @description 此组件提供了自定义tabbar的能力。
  50. * @tutorial https://www.uvui.cn/components/tabbar.html
  51. * @property {String | Number} name item标签的名称,作为与uv-tabbar的value参数匹配的标识符
  52. * @property {String} icon uvui内置图标或者绝对路径的图片
  53. * @property {String | Number} badge 右上角的角标提示信息
  54. * @property {Boolean} dot 是否显示圆点,将会覆盖badge参数(默认 false )
  55. * @property {String} text 描述文本
  56. * @property {Object | String} badgeStyle 控制徽标的位置,对象或者字符串形式,可以设置top和right属性(默认 'top: 6px;right:2px;' )
  57. * @property {Object} customStyle 定义需要用到的外部样式
  58. *
  59. * @example <uv-tabbar :value="value2" :placeholder="false" @change="name => value2 = name" :fixed="false" :safeAreaInsetBottom="false"><uv-tabbar-item text="首页" icon="home" dot ></uv-tabbar-item></uv-tabbar>
  60. */
  61. export default {
  62. name: 'uv-tabbar-item',
  63. mixins: [mpMixin, mixin, props],
  64. emits: ['click','change'],
  65. data() {
  66. return {
  67. isActive: false, // 是否处于激活状态
  68. parentData: {
  69. value: null,
  70. activeColor: '',
  71. inactiveColor: '',
  72. iconSize: 20
  73. }
  74. }
  75. },
  76. created() {
  77. this.init()
  78. },
  79. methods: {
  80. init() {
  81. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  82. this.updateParentData()
  83. if (!this.parent) {
  84. this.$uv.error('uv-tabbar-item必须搭配uv-tabbar组件使用')
  85. }
  86. // 本子组件在uv-tabbar的children数组中的索引
  87. const index = this.parent.children.indexOf(this)
  88. // 判断本组件的name(如果没有定义name,就用index索引)是否等于父组件的value参数
  89. this.isActive = (this.name || index) === this.parentData.value
  90. },
  91. updateParentData() {
  92. // 此方法在mixin中
  93. this.getParentData('uv-tabbar')
  94. },
  95. // 此方法将会被父组件uv-tabbar调用
  96. updateFromParent() {
  97. // 重新初始化
  98. this.init()
  99. },
  100. clickHandler() {
  101. this.$nextTick(() => {
  102. const index = this.parent.children.indexOf(this)
  103. const name = this.name || index
  104. // 点击的item为非激活的item才发出change事件
  105. if (name !== this.parent.value) {
  106. this.parent.$emit('change', name)
  107. }
  108. this.$emit('click', name)
  109. })
  110. }
  111. },
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  116. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  117. .uv-tabbar-item {
  118. @include flex(column);
  119. align-items: center;
  120. justify-content: center;
  121. flex: 1;
  122. &__icon {
  123. @include flex;
  124. position: relative;
  125. width: 150rpx;
  126. justify-content: center;
  127. }
  128. &__text {
  129. margin-top: 2px;
  130. font-size: 12px;
  131. color: $uv-content-color;
  132. }
  133. }
  134. /* #ifdef MP */
  135. // 由于小程序都使用shadow DOM形式实现,需要给影子宿主设置flex: 1才能让其撑开
  136. :host {
  137. flex: 1
  138. }
  139. /* #endif */
  140. </style>