su-tab-item.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view>
  3. <view :id="'tab-' + props.index" class="ui-tab-item" :class="[{ cur: cur }, tpl]">
  4. <view class="ui-tab-icon" :class="props.data.icon" v-if="props.data.icon"></view>
  5. <view
  6. class="ui-tab-text"
  7. :class="[cur ? 'curColor cur' : 'default-color']"
  8. :style="[{ color: cur ? titleStyle.activeColor : titleStyle.color }]"
  9. >
  10. {{ props.data.title }}
  11. </view>
  12. <view class="ui-tag badge ml-2" v-if="props.data.tag != null">{{ props.data.tag }}</view>
  13. </view>
  14. <view
  15. v-if="tpl === 'subtitle'"
  16. class="item-subtitle ss-flex ss-col-bottom ss-row-center"
  17. :style="[{ color: cur ? subtitleStyle.activeColor : subtitleStyle.color }]"
  18. >
  19. {{ props.data.subtitle }}
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. name: 'UiTabItem',
  26. };
  27. </script>
  28. <script setup>
  29. /**
  30. * 基础组件 - uiTabItem
  31. */
  32. import { computed, onMounted, getCurrentInstance, inject } from 'vue';
  33. import sheep from '@/sheep';
  34. const vm = getCurrentInstance();
  35. const props = defineProps({
  36. data: {
  37. type: [Object, String, Number],
  38. default() {},
  39. },
  40. index: {
  41. type: Number,
  42. default: 0,
  43. },
  44. });
  45. const emits = defineEmits(['up']);
  46. onMounted(() => {
  47. computedQuery();
  48. uni.onWindowResize((res) => {
  49. computedQuery();
  50. });
  51. });
  52. function getParent(name) {
  53. let parent = vm?.parent;
  54. // 无父级返回null
  55. if (parent) {
  56. let parentName = parent?.type?.name;
  57. // 父组件name 为真返回父级,为假循环
  58. while (parentName !== name) {
  59. parent = parent?.parent;
  60. // 存在父级循环,不存在打断循环
  61. if (parent) {
  62. parentName = parent?.type?.name;
  63. } else {
  64. return null;
  65. }
  66. }
  67. return parent;
  68. }
  69. return null;
  70. }
  71. const UiTab = getParent('SuTab');
  72. // 获取抛出的数据和方法
  73. let uiTabProvide;
  74. if (UiTab) {
  75. uiTabProvide = inject('suTabProvide');
  76. }
  77. const cur = computed(() => uiTabProvide?.curValue.value === props.index);
  78. const tpl = computed(() => uiTabProvide?.props?.tpl);
  79. const subtitleStyle = computed(() => uiTabProvide?.props?.subtitleStyle);
  80. const titleStyle = computed(() => uiTabProvide?.props?.titleStyle);
  81. const computedQuery = () => {
  82. uni.createSelectorQuery()
  83. .in(vm)
  84. .select('#tab-' + props.index)
  85. .boundingClientRect((data) => {
  86. if (data != null) {
  87. // 传递到父组件进行计算
  88. emits('up', props.index, data);
  89. } else {
  90. console.log('tab-item data error');
  91. }
  92. })
  93. .exec();
  94. };
  95. </script>
  96. <style lang="scss" scoped>
  97. .default-color {
  98. color: $black;
  99. }
  100. .ui-tab-item {
  101. display: inline-flex;
  102. align-items: center;
  103. padding: 0 1em;
  104. min-height: 1.5em;
  105. line-height: 1.5em;
  106. position: relative;
  107. z-index: 1;
  108. opacity: 0.6;
  109. transition: opacity 0.3s;
  110. min-width: 60px;
  111. .ui-tab-text {
  112. width: 100%;
  113. text-align: center;
  114. }
  115. .ui-tab-icon {
  116. margin: 0 0.25em;
  117. font-size: 120%;
  118. }
  119. &.cur {
  120. opacity: 1;
  121. font-weight: bold;
  122. }
  123. &.btn {
  124. .ui-tab-text {
  125. transform: scale(0.9);
  126. transition: color 0.3s;
  127. font-weight: bold;
  128. }
  129. }
  130. &.subtitle {
  131. .ui-tab-text {
  132. transform: scale(0.9);
  133. transition: color 0.3s;
  134. font-weight: bold;
  135. height: calc(100% - 2.6em);
  136. line-height: calc(100% - 2.6em);
  137. margin-top: 1.2em;
  138. color: $white;
  139. }
  140. }
  141. }
  142. .item-subtitle {
  143. height: 2em;
  144. font-size: 22rpx;
  145. color: $dark-9;
  146. margin-bottom: 0.6em;
  147. }
  148. .cur-subtitle {
  149. color: var(--ui-BG-Main);
  150. }
  151. </style>