u-tabbar-item.vue 4.1 KB

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