su-tabbar-item.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="u-tabbar-item" :style="[addStyle(customStyle)]">
  3. <view v-if="isCenter" class="tabbar-center-item">
  4. <image class="center-image" :src="centerImage" mode="aspectFill"></image>
  5. </view>
  6. <template v-else>
  7. <view class="u-tabbar-item__icon">
  8. <image
  9. v-if="icon"
  10. :name="icon"
  11. :color="isActive ? parentData.activeColor : parentData.inactiveColor"
  12. :size="20"
  13. ></image>
  14. <block v-else>
  15. <slot v-if="isActive" name="active-icon" />
  16. <slot v-else name="inactive-icon" />
  17. </block>
  18. <!-- <u-badge
  19. absolute
  20. :offset="[0, dot ? '34rpx' : badge > 9 ? '14rpx' : '20rpx']"
  21. :customStyle="badgeStyle"
  22. :isDot="dot"
  23. :value="badge || (dot ? 1 : null)"
  24. :show="dot || badge > 0"
  25. ></u-badge> -->
  26. </view>
  27. <slot name="text">
  28. <text
  29. class="u-tabbar-item__text"
  30. :style="{
  31. color: isActive ? parentData.activeColor : parentData.inactiveColor,
  32. }"
  33. >
  34. {{ text }}
  35. </text>
  36. </slot>
  37. </template>
  38. </view>
  39. </template>
  40. <script>
  41. /**
  42. * TabbarItem 底部导航栏子组件
  43. * @description 此组件提供了自定义tabbar的能力。
  44. * @property {String | Number} name item标签的名称,作为与u-tabbar的value参数匹配的标识符
  45. * @property {String} icon uView内置图标或者绝对路径的图片
  46. * @property {String | Number} badge 右上角的角标提示信息
  47. * @property {Boolean} dot 是否显示圆点,将会覆盖badge参数(默认 false )
  48. * @property {String} text 描述文本
  49. * @property {Object | String} badgeStyle 控制徽标的位置,对象或者字符串形式,可以设置top和right属性(默认 'top: 6px;right:2px;' )
  50. * @property {Object} customStyle 定义需要用到的外部样式
  51. *
  52. */
  53. import { deepMerge, addStyle, sleep, $parent } from '@/sheep/helper';
  54. export default {
  55. name: 'su-tabbar-item',
  56. props: {
  57. customStyle: {
  58. type: [Object, String],
  59. default: () => ({}),
  60. },
  61. customClass: {
  62. type: String,
  63. default: '',
  64. },
  65. // 跳转的页面路径
  66. url: {
  67. type: String,
  68. default: '',
  69. },
  70. // 页面跳转的类型
  71. linkType: {
  72. type: String,
  73. default: 'navigateTo',
  74. },
  75. // item标签的名称,作为与u-tabbar的value参数匹配的标识符
  76. name: {
  77. type: [String, Number, null],
  78. default: '',
  79. },
  80. // uView内置图标或者绝对路径的图片
  81. icon: {
  82. icon: String,
  83. default: '',
  84. },
  85. // 右上角的角标提示信息
  86. badge: {
  87. type: [String, Number, null],
  88. default: '',
  89. },
  90. // 是否显示圆点,将会覆盖badge参数
  91. dot: {
  92. type: Boolean,
  93. default: false,
  94. },
  95. // 描述文本
  96. text: {
  97. type: String,
  98. default: '',
  99. },
  100. // 控制徽标的位置,对象或者字符串形式,可以设置top和right属性
  101. badgeStyle: {
  102. type: [Object, String],
  103. default: '',
  104. },
  105. isCenter: {
  106. type: Boolean,
  107. default: false,
  108. },
  109. centerImage: {
  110. type: String,
  111. default: '',
  112. },
  113. },
  114. data() {
  115. return {
  116. isActive: false, // 是否处于激活状态
  117. addStyle,
  118. parentData: {
  119. value: null,
  120. activeColor: '',
  121. inactiveColor: '',
  122. },
  123. parent: {},
  124. };
  125. },
  126. created() {
  127. this.init();
  128. },
  129. methods: {
  130. getParentData(parentName = '') {
  131. // 避免在created中去定义parent变量
  132. if (!this.parent) this.parent = {};
  133. // 这里的本质原理是,通过获取父组件实例(也即类似u-radio的父组件u-radio-group的this)
  134. // 将父组件this中对应的参数,赋值给本组件(u-radio的this)的parentData对象中对应的属性
  135. // 之所以需要这么做,是因为所有端中,头条小程序不支持通过this.parent.xxx去监听父组件参数的变化
  136. // 此处并不会自动更新子组件的数据,而是依赖父组件u-radio-group去监听data的变化,手动调用更新子组件的方法去重新获取
  137. this.parent = $parent.call(this, parentName);
  138. if (this.parent.children) {
  139. // 如果父组件的children不存在本组件的实例,才将本实例添加到父组件的children中
  140. this.parent.children.indexOf(this) === -1 && this.parent.children.push(this);
  141. }
  142. if (this.parent && this.parentData) {
  143. // 历遍parentData中的属性,将parent中的同名属性赋值给parentData
  144. Object.keys(this.parentData).map((key) => {
  145. this.parentData[key] = this.parent[key];
  146. });
  147. }
  148. },
  149. init() {
  150. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  151. this.updateParentData();
  152. if (!this.parent) {
  153. console.log('u-tabbar-item必须搭配u-tabbar组件使用');
  154. }
  155. // 本子组件在u-tabbar的children数组中的索引
  156. const index = this.parent.children.indexOf(this);
  157. // 判断本组件的name(如果没有定义name,就用index索引)是否等于父组件的value参数
  158. this.isActive = (this.name.split('?')[0] || index) === this.parentData.value;
  159. },
  160. updateParentData() {
  161. // 此方法在mixin中
  162. this.getParentData('su-tabbar');
  163. },
  164. // 此方法将会被父组件u-tabbar调用
  165. updateFromParent() {
  166. // 重新初始化
  167. this.init();
  168. },
  169. clickHandler() {
  170. this.$nextTick(() => {
  171. const index = this.parent.children.indexOf(this);
  172. const name = this.name || index;
  173. // 点击的item为非激活的item才发出change事件
  174. if (name !== this.parent.value) {
  175. this.parent.$emit('change', name);
  176. }
  177. this.$emit('click', name);
  178. });
  179. },
  180. },
  181. };
  182. </script>
  183. <style lang="scss" scoped>
  184. .tabbar-center-item {
  185. height: 40px;
  186. width: 40px;
  187. display: flex;
  188. align-items: center;
  189. justify-content: center;
  190. border-radius: 50%;
  191. background-color: rebeccapurple;
  192. transform: scale(1.3) translateY(-6px);
  193. position: absolute;
  194. z-index: 2;
  195. .center-image {
  196. width: 25px;
  197. height: 25px;
  198. }
  199. }
  200. .u-tabbar-item {
  201. display: flex;
  202. flex-direction: column;
  203. align-items: center;
  204. justify-content: center;
  205. flex: 1;
  206. position: relative;
  207. z-index: 1;
  208. &__icon {
  209. display: flex;
  210. position: relative;
  211. width: 150rpx;
  212. justify-content: center;
  213. }
  214. &__text {
  215. margin-top: 2px;
  216. font-size: 12px;
  217. color: var(--textSize);
  218. }
  219. }
  220. /* #ifdef MP */
  221. // 由于小程序都使用shadow DOM形式实现,需要给影子宿主设置flex: 1才能让其撑开
  222. :host {
  223. flex: 1;
  224. }
  225. /* #endif */
  226. </style>