uni-segmented-control.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view
  3. :class="[styleType === 'text'?'segmented-control--text' : 'segmented-control--button' ]"
  4. :style="{ borderColor: styleType === 'text' ? '' : activeColor }"
  5. class="segmented-control">
  6. <view :class="[
  7. styleType === 'text'?'segmented-control__item--text': 'segmented-control__item--button',
  8. index === currentIndex&&styleType === 'button'?'segmented-control__item--button--active': '' ,
  9. index === 0&&styleType === 'button'?'segmented-control__item--button--first': '',
  10. index === values.length - 1&&styleType === 'button'?'segmented-control__item--button--last': ''
  11. ]"
  12. v-for="(item, index) in values"
  13. :key="index"
  14. :style="{
  15. backgroundColor: index === currentIndex && styleType === 'button' ? activeColor : '',
  16. borderColor: index === currentIndex&&styleType === 'text'||styleType === 'button'?activeColor:'transparent'
  17. }"
  18. class="segmented-control__item" @click="_onClick(index)">
  19. <text :style="{color: index === currentIndex ? styleType === 'text'
  20. ? activeColor : '#fff' : styleType === 'text' ? '#000' : activeColor}"
  21. class="segmented-control__text">{{ item }}</text>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'UniSegmentedControl',
  28. props: {
  29. current: {
  30. type: Number,
  31. default: 0
  32. },
  33. values: {
  34. type: Array,
  35. default () {
  36. return []
  37. }
  38. },
  39. activeColor: {
  40. type: String,
  41. default: '#007aff'
  42. },
  43. styleType: {
  44. type: String,
  45. default: 'button'
  46. }
  47. },
  48. data() {
  49. return {
  50. currentIndex: 0
  51. }
  52. },
  53. watch: {
  54. current(val) {
  55. if (val !== this.currentIndex) {
  56. this.currentIndex = val
  57. }
  58. }
  59. },
  60. created() {
  61. this.currentIndex = this.current
  62. },
  63. methods: {
  64. _onClick(index) {
  65. if (this.currentIndex !== index) {
  66. this.currentIndex = index
  67. this.$emit('clickItem', {currentIndex:index})
  68. }
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. @import '@/uni.scss';
  75. .segmented-control {
  76. /* #ifndef APP-NVUE */
  77. display: flex;
  78. box-sizing: border-box;
  79. /* #endif */
  80. flex-direction: row;
  81. height: 52.5rpx;
  82. overflow: hidden;
  83. background-color: #fff;
  84. }
  85. .segmented-control__item {
  86. /* #ifndef APP-NVUE */
  87. display: inline-flex;
  88. box-sizing: border-box;
  89. /* #endif */
  90. position: relative;
  91. flex: 1;
  92. justify-content: center;
  93. align-items: center;
  94. }
  95. .segmented-control__item--button {
  96. border-style: solid;
  97. border-top-width: 1px;
  98. border-bottom-width: 1px;
  99. border-right-width: 1px;
  100. border-left-width: 0;
  101. }
  102. .segmented-control__item--button--first {
  103. border-left-width: 1px;
  104. border-top-left-radius: 5px;
  105. border-bottom-left-radius: 5px;
  106. }
  107. .segmented-control__item--button--last {
  108. border-top-right-radius: 5px;
  109. border-bottom-right-radius: 5px;
  110. }
  111. .segmented-control__item--text {
  112. border-bottom-style: solid;
  113. border-bottom-width: 3.75rpx;
  114. }
  115. .segmented-control__text {
  116. font-size: 22.5rpx;
  117. font-weight: 500;
  118. line-height: 52.5rpx;
  119. text-align: center;
  120. }
  121. </style>