ut-tabs-dict.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <template v-if="cStyle === 'fill'">
  3. <up-tabs ref="upTabsRef" class="fill-tabs" :list="tabs" keyName="label" @change="change"
  4. lineColor="rgba(0,0,0,0)">
  5. <template #content="{ item, keyName, index }">
  6. <view class="tab-fill" :style="{ background: index === current ? lineColor : '#F7F7F7' }"
  7. :class="{ checked: index === current }">{{ item[keyName] }}</view>
  8. </template>
  9. </up-tabs>
  10. </template>
  11. <template v-else>
  12. <up-tabs ref="upTabsRef" :list="tabs" keyName="label" @change="change" :lineWidth="curLineWidth"
  13. :current="current" :lineHeight="lineHeight" :itemStyle="itemStyle" :lineColor="lineColor"
  14. :inactiveStyle="inactiveStyle" :activeStyle="activeStyle">
  15. </up-tabs>
  16. </template>
  17. </template>
  18. <script setup name="ut-tabs-dict">
  19. import { toRefs, ref, onMounted, watch } from 'vue';
  20. const props = defineProps({
  21. tabs: {
  22. type: Array,
  23. default: () => []
  24. },
  25. modelValue: {
  26. type: String,
  27. default: ''
  28. },
  29. inactiveStyle: {
  30. type: Object,
  31. default: () => ({
  32. color: '#999',
  33. fontSize: '30rpx'
  34. })
  35. },
  36. activeStyle: {
  37. type: Object,
  38. default: () => ({
  39. color: '#2A6D52',
  40. fontSize: '30rpx',
  41. fontWeight: 500
  42. })
  43. },
  44. lineHeight: {
  45. type: String,
  46. default: '2px'
  47. },
  48. lineWidth: {
  49. type: String,
  50. default: '60rpx'
  51. },
  52. fontSize: {
  53. type: String,
  54. default: '30rpx'
  55. },
  56. itemStyle: {
  57. type: Object,
  58. default: () => ({
  59. padding: '10rpx 20rpx'
  60. })
  61. },
  62. isAutoWidth: {
  63. type: Boolean,
  64. default: false
  65. },
  66. lineColor: {
  67. type: String,
  68. default: '#2A6D52'
  69. },
  70. cStyle: {
  71. type: String,
  72. default: ''
  73. }
  74. });
  75. const upTabsRef = ref(null);
  76. const emit = defineEmits(['change', 'update:modelValue']);
  77. const change = (item) => {
  78. emit('update:modelValue', item.value);
  79. emit('change', item.value);
  80. };
  81. const setCurLineWidthset = (index) => {
  82. const item = props.tabs[index];
  83. const width = item?.label?.length * 30;
  84. curLineWidth.value = width + 'rpx';
  85. };
  86. const curLineWidth = ref(props.lineWidth);
  87. const current = ref(0);
  88. watch(() => props.modelValue, (val) => {
  89. current.value = props.tabs.findIndex(item => item.value === val);
  90. if (props.isAutoWidth && current.value !== -1) {
  91. setCurLineWidthset(current.value);
  92. }
  93. }, { immediate: true });
  94. </script>
  95. <style lang="scss" scoped>
  96. .tab-fill {
  97. display: inline-block;
  98. white-space: nowrap;
  99. font-size: 26rpx;
  100. color: #999;
  101. height: 54rpx;
  102. line-height: 54rpx;
  103. padding: 0 16rpx;
  104. background: #F7F7F7;
  105. border-radius: 8rpx;
  106. &.checked {
  107. background: #2A6D52;
  108. color: #fff;
  109. }
  110. }
  111. </style>
  112. <style lang="scss"></style>