uni-segmented-control.vue 2.1 KB

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