my-tab.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="tab uni-row" :style="'font-size: ' + fontSize + 'px;'">
  3. <template v-for="(item,index) in tabItems" :key="index">
  4. <text v-if="typeof item == 'string'" :class="getClassList(index)" :style="getTextStyle(index)" @click="onTabClick(index)">{{item}}</text>
  5. <e-select v-if="typeof item == 'object'" :style="getSelectStyle()" v-model="item.selectValue" maxHeight="300px"
  6. :options="item.data" :search="false" :inputClick="false" :clearable="false" :class="getClassList(index)"
  7. @click="onTabClick(index)" @getText="getESelectText" @change="eSelectChange"></e-select>
  8. </template>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: "my-tab",
  14. props: {
  15. initActIndex: {
  16. type: Number,
  17. default: 0
  18. },
  19. tabItems: {
  20. type: Array,
  21. default: null
  22. },
  23. tabItemsMark: {
  24. type: Array,
  25. default: null
  26. },
  27. type: {
  28. type: Number,
  29. default: 0
  30. },
  31. fontSize: {
  32. type: Number,
  33. default: 16
  34. },
  35. },
  36. emits: ['onTabClick', 'onSelectChange'],
  37. data() {
  38. return {
  39. tabCurrent: 0,
  40. selectedID: 0
  41. };
  42. },
  43. mounted() {
  44. // console.log("initActIndex:" , this.initActIndex);
  45. // console.log("tabItems:" , this.tabItems);
  46. this.tabCurrent = this.initActIndex;
  47. },
  48. watch: {
  49. initActIndex(newVal, oldVal) {
  50. // console.log('initActIndex oldVal:', oldVal);
  51. // console.log('initActIndex newVal:', newVal);
  52. this.tabCurrent = newVal;
  53. }
  54. },
  55. methods: {
  56. getClassList(index) {
  57. // console.log("index:", index);
  58. let classList = "";
  59. if (this.tabCurrent == index) {
  60. classList = "tab-active";
  61. if (this.type == 1 && this.tabItems.length > 1) {
  62. if (index == 0) {
  63. classList += " boder-radius-left";
  64. } else if (index == (this.tabItems.length - 1)) {
  65. classList += " boder-radius-right";
  66. }
  67. } else {
  68. classList += " boder-radius-all";
  69. }
  70. } else {
  71. classList = "tab-unactive";
  72. if (this.type == 1) {
  73. if (index < (this.tabItems.length - 1)) {
  74. classList += " boder-solid-right";
  75. }
  76. }
  77. }
  78. // console.log("classList:", classList);
  79. return classList;
  80. },
  81. getTextStyle(index) {
  82. if (this.tabItemsMark == null || index > this.tabItemsMark.length - 1) {
  83. return;
  84. }
  85. let styleStr = "";
  86. const mark = this.tabItemsMark[index];
  87. if (mark.textColor.length > 0) {
  88. if (this.tabCurrent != index) {
  89. styleStr += "color: " + mark.textColor + ";";
  90. styleStr += "background: url('" + mark.icon + "') no-repeat;";
  91. } else {
  92. styleStr += "background: url('" + mark.icon + "') #2e85ec no-repeat;";
  93. }
  94. styleStr += "background-position-x: 90%;";
  95. styleStr += "background-position-y: 2%;";
  96. styleStr += "background-size: 14px;";
  97. }
  98. // console.log("getTextStyle: ", styleStr);
  99. return styleStr;
  100. },
  101. getSelectStyle() {
  102. let styleStr = "";
  103. // const len = this.tabItems.length;
  104. // if (len > 0) {
  105. // styleStr = "width: " + 100 / len + "%;";
  106. // }
  107. // console.log("getSelectStyle", styleStr);
  108. return styleStr;
  109. },
  110. onTabClick(index) {
  111. // console.log("onTabClick:", index);
  112. this.tabCurrent = index;
  113. this.$emit('onTabClick', index);
  114. },
  115. // 获取输入框中值
  116. getESelectText(data) {
  117. // console.log("getESelectText:", data);
  118. },
  119. // 获取选择选项值
  120. eSelectChange(data) {
  121. // console.log("eSelectChange:", data);
  122. this.$emit('onSelectChange', data);
  123. },
  124. }
  125. }
  126. </script>
  127. <style scoped>
  128. .tab {
  129. width: 90%;
  130. height: 60rpx;
  131. margin-top: 30rpx;
  132. background: #e7ecef;
  133. border-radius: 18px;
  134. justify-content: space-around;
  135. font-size: 16px;
  136. font-weight: 500;
  137. line-height: 60rpx;
  138. text-align: center;
  139. }
  140. .tab-active {
  141. width: 100%;
  142. height: 60rpx;
  143. background-color: #2e85ec;
  144. color: #ffffff;
  145. }
  146. .tab-unactive {
  147. width: 100%;
  148. height: 60rpx;
  149. color: #818181;
  150. }
  151. .tab-corner-mark {
  152. background-image: url('/static/common/award.png');
  153. background-repeat: no-repeat;
  154. background-position: center;
  155. background-size: cover;
  156. }
  157. .boder-radius-all {
  158. border-radius: 18px;
  159. }
  160. .boder-radius-left {
  161. border-radius: 18px 0 0 18px;
  162. }
  163. .boder-radius-right {
  164. border-radius: 0 18px 18px 0;
  165. }
  166. .boder-solid-left {
  167. border-left: #d7d7d7 1px solid;
  168. }
  169. .boder-solid-right {
  170. border-right: #d7d7d7 1px solid;
  171. }
  172. /deep/ .e-select {
  173. border: none !important;
  174. }
  175. /deep/ .e-select-input-text {
  176. width: 90% !important;
  177. color: inherit !important;
  178. font-size: 14px !important;
  179. }
  180. /deep/ .e-select-selector-item {
  181. color: #818181;
  182. }
  183. /deep/ .e-select-selector .e-select-selector-scroll .highlight {
  184. color: #409eff !important;
  185. }
  186. /deep/ .uni-icons {
  187. color: inherit !important;
  188. }
  189. /deep/ .e-select-input-placeholder {
  190. color: inherit !important;
  191. font-size: 14px !important;
  192. line-height: 60rpx !important;
  193. }
  194. /deep/ .e-select-icon {
  195. /* width: 26px !important; */
  196. width: 10% !important;
  197. }
  198. </style>