u-tabbar-item.vue 3.8 KB

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