You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.4 KiB

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