u-tabbar-item.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view
  3. class="u-tabbar-item"
  4. :style="[addStyle(customStyle)]"
  5. :class="[isMidButton ? 'u-tabbar-item--mid-button' : '']"
  6. @tap="clickHandler"
  7. >
  8. <view
  9. class="u-tabbar-item__icon"
  10. :class="[isMidButton ? 'u-tabbar-item__icon--mid-button' : '']"
  11. >
  12. <view class="u-tabbar-item--mid-button-cover" v-if="isMidButton">
  13. </view>
  14. <up-icon
  15. v-if="icon"
  16. :name="icon"
  17. :color="isActive? parentData.activeColor : parentData.inactiveColor"
  18. :size="isMidButton ? 26 : 20"
  19. ></up-icon>
  20. <template v-else>
  21. <slot
  22. v-if="isActive"
  23. name="active-icon"
  24. />
  25. <slot
  26. v-else
  27. name="inactive-icon"
  28. />
  29. </template>
  30. <u-badge
  31. absolute
  32. :offset="[0, dot ? '34rpx' : badge > 9 ? '14rpx' : '20rpx']"
  33. :customStyle="badgeStyle"
  34. :isDot="dot"
  35. :value="badge || (dot ? 1 : null)"
  36. :show="dot || badge > 0"
  37. ></u-badge>
  38. </view>
  39. <slot name="text">
  40. <text
  41. class="u-tabbar-item__text"
  42. :style="{
  43. color: isActive? parentData.activeColor : parentData.inactiveColor
  44. }"
  45. >{{ text }}</text>
  46. </slot>
  47. </view>
  48. </template>
  49. <script>
  50. import { props } from './props';
  51. import { mpMixin } from '../../libs/mixin/mpMixin';
  52. import { mixin } from '../../libs/mixin/mixin';
  53. import { addStyle, error } from '../../libs/function/index';
  54. /**
  55. * TabbarItem 底部导航栏子组件
  56. * @description 此组件提供了自定义tabbar的能力。
  57. * @tutorial https://ijry.github.io/uview-plus/components/tabbar.html
  58. * @property {String | Number} name item标签的名称,作为与u-tabbar的value参数匹配的标识符
  59. * @property {String} icon uView内置图标或者绝对路径的图片
  60. * @property {String | Number} badge 右上角的角标提示信息
  61. * @property {Boolean} dot 是否显示圆点,将会覆盖badge参数(默认 false )
  62. * @property {String} text 描述文本
  63. * @property {Object | String} badgeStyle 控制徽标的位置,对象或者字符串形式,可以设置top和right属性(默认 'top: 6px;right:2px;' )
  64. * @property {Object} customStyle 定义需要用到的外部样式
  65. *
  66. * @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>
  67. */
  68. export default {
  69. name: 'u-tabbar-item',
  70. mixins: [mpMixin, mixin, props],
  71. data() {
  72. return {
  73. isActive: false, // 是否处于激活状态
  74. parentData: {
  75. value: null,
  76. activeColor: '',
  77. inactiveColor: ''
  78. }
  79. }
  80. },
  81. // 微信小程序中 options 选项
  82. options: {
  83. virtualHost: true //将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  84. },
  85. computed: {
  86. // 计算是否为中间按钮
  87. isMidButton() {
  88. return this.mode === 'midButton';
  89. }
  90. },
  91. created() {
  92. this.init()
  93. },
  94. emits: ["click", "change"],
  95. methods: {
  96. addStyle,
  97. init() {
  98. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  99. this.updateParentData()
  100. if (!this.parent) {
  101. error('up-tabbar-item必须搭配up-tabbar组件使用')
  102. }
  103. // 本子组件在u-tabbar的children数组中的索引
  104. const index = this.parent.children.indexOf(this)
  105. // 判断本组件的name(如果没有定义name,就用index索引)是否等于父组件的value参数
  106. this.isActive = (this.name || index) === this.parentData.value
  107. },
  108. updateParentData() {
  109. // 此方法在mixin中
  110. this.getParentData('u-tabbar')
  111. },
  112. // 此方法将会被父组件u-tabbar调用
  113. updateFromParent() {
  114. // 重新初始化
  115. this.init()
  116. },
  117. clickHandler() {
  118. this.$nextTick(() => {
  119. const index = this.parent.children.indexOf(this)
  120. const name = this.name || index
  121. // 点击的item为非激活的item才发出change事件
  122. if (name !== this.parent.value) {
  123. this.parent.$emit('change', name)
  124. }
  125. this.$emit('click', name)
  126. })
  127. }
  128. },
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. .u-tabbar-item {
  133. @include flex(column);
  134. align-items: center;
  135. justify-content: center;
  136. flex: 1;
  137. /* #ifndef APP-NVUE */
  138. width: 100%;
  139. height: 100%;
  140. /* #endif */
  141. /* #ifdef H5 */
  142. cursor: pointer;
  143. /* #endif */
  144. &__icon {
  145. @include flex;
  146. position: relative;
  147. width: 150rpx;
  148. justify-content: center;
  149. }
  150. &__text {
  151. margin-top: 2px;
  152. font-size: 12px;
  153. color: $u-content-color;
  154. }
  155. }
  156. // 中间按钮样式
  157. .u-tabbar-item--mid-button {
  158. /* #ifndef APP-NVUE */
  159. transform: translateY(-10px);
  160. /* #endif */
  161. }
  162. .u-tabbar-item--mid-button-cover {
  163. background-color: #fff;
  164. position: absolute;
  165. top: 22px;
  166. left: -10px;
  167. // right: -10px;
  168. width: 90px;
  169. bottom: 0;
  170. }
  171. .u-tabbar-item__icon--mid-button {
  172. width: 70px;
  173. height: 70px;
  174. border-radius: 100px;
  175. background-color: #ffffff;
  176. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  177. display: flex;
  178. align-items: center;
  179. justify-content: center;
  180. }
  181. /* #ifdef MP */
  182. // 由于小程序都使用shadow DOM形式实现,需要给影子宿主设置flex: 1才能让其撑开
  183. :host {
  184. flex: 1;
  185. width: 100%;
  186. }
  187. /* #endif */
  188. </style>