uni-segmented-control.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="segmented-control" :class="styleType" :style="wrapStyle">
  3. <view v-for="(item, index) in values" class="segmented-control-item" :class="styleType" :key="index" :style="index === currentIndex ? activeStyle : itemStyle" @click="onClick(index)">
  4. {{item}}
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'uni-segmented-control',
  11. props: {
  12. current: {
  13. type: Number,
  14. default: 0
  15. },
  16. values: {
  17. type: Array,
  18. default () {
  19. return [];
  20. }
  21. },
  22. activeColor: {
  23. type: String,
  24. default: '#007aff'
  25. },
  26. styleType: {
  27. type: String,
  28. default: 'button'
  29. }
  30. },
  31. data() {
  32. return {
  33. currentIndex: this.current
  34. }
  35. },
  36. watch: {
  37. current(val) {
  38. if (val !== this.currentIndex) {
  39. this.currentIndex = val;
  40. }
  41. }
  42. },
  43. computed: {
  44. wrapStyle() {
  45. let styleString = '';
  46. switch (this.styleType) {
  47. case 'text':
  48. styleString = `border:0;`;
  49. break;
  50. default:
  51. styleString = `border-color: ${this.activeColor}`;
  52. break;
  53. }
  54. return styleString;
  55. },
  56. itemStyle() {
  57. let styleString = '';
  58. switch (this.styleType) {
  59. case 'text':
  60. styleString = `color:#000;border-left:0;`;
  61. break;
  62. default:
  63. styleString = `color:${this.activeColor};border-color:${this.activeColor};`;
  64. break;
  65. }
  66. return styleString;
  67. },
  68. activeStyle() {
  69. let styleString = '';
  70. switch (this.styleType) {
  71. case 'text':
  72. styleString = `color:${this.activeColor};border-left:0;border-bottom-style:solid;`;
  73. break;
  74. default:
  75. styleString = `color:#fff;border-color:${this.activeColor};background-color:${this.activeColor}`;
  76. break;
  77. }
  78. return styleString;
  79. }
  80. },
  81. methods: {
  82. onClick(index) {
  83. if (this.currentIndex !== index) {
  84. this.currentIndex = index;
  85. this.$emit('clickItem', index);
  86. }
  87. }
  88. },
  89. }
  90. </script>
  91. <style>
  92. .segmented-control {
  93. display: flex;
  94. flex-direction: row;
  95. justify-content: center;
  96. width: 75%;
  97. font-size: 28upx;
  98. border-radius: 10upx;
  99. box-sizing: border-box;
  100. margin: 0 auto;
  101. overflow: hidden;
  102. }
  103. .segmented-control.button {
  104. border: 2upx solid;
  105. }
  106. .segmented-control.text {
  107. border: 0;
  108. border-radius: 0upx;
  109. }
  110. .segmented-control-item {
  111. flex: 1;
  112. text-align: center;
  113. line-height: 60upx;
  114. box-sizing: border-box;
  115. }
  116. .segmented-control-item.button {
  117. border-left: 1upx solid;
  118. }
  119. .segmented-control-item.text {
  120. border-left: 0;
  121. }
  122. .segmented-control-item:first-child {
  123. border-left-width: 0;
  124. }
  125. </style>