uni-segmented-control.vue 2.9 KB

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