uni-segmented-control.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  23. * SegmentedControl 分段器
  24. * @description 用作不同视图的显示
  25. * @tutorial https://ext.dcloud.net.cn/plugin?id=54
  26. * @property {Number} current 当前选中的tab索引值,从0计数
  27. * @property {String} styleType = [button|text] 分段器样式类型
  28. * @value button 按钮类型
  29. * @value text 文字类型
  30. * @property {String} activeColor 选中的标签背景色与边框颜色
  31. * @property {Array} values 选项数组
  32. * @event {Function} clickItem 组件触发点击事件时触发,e={currentIndex}
  33. */
  34. export default {
  35. name: 'UniSegmentedControl',
  36. props: {
  37. current: {
  38. type: Number,
  39. default: 0
  40. },
  41. values: {
  42. type: Array,
  43. default () {
  44. return []
  45. }
  46. },
  47. activeColor: {
  48. type: String,
  49. default: '#007aff'
  50. },
  51. styleType: {
  52. type: String,
  53. default: 'button'
  54. }
  55. },
  56. data() {
  57. return {
  58. currentIndex: 0
  59. }
  60. },
  61. watch: {
  62. current(val) {
  63. if (val !== this.currentIndex) {
  64. this.currentIndex = val
  65. }
  66. }
  67. },
  68. created() {
  69. this.currentIndex = this.current
  70. },
  71. methods: {
  72. _onClick(index) {
  73. if (this.currentIndex !== index) {
  74. this.currentIndex = index
  75. this.$emit('clickItem', {
  76. currentIndex: index
  77. })
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .segmented-control {
  85. /* #ifndef APP-NVUE */
  86. display: flex;
  87. box-sizing: border-box;
  88. /* #endif */
  89. flex-direction: row;
  90. height: 36px;
  91. overflow: hidden;
  92. /* #ifdef H5 */
  93. cursor: pointer;
  94. /* #endif */
  95. }
  96. .segmented-control__item {
  97. /* #ifndef APP-NVUE */
  98. display: inline-flex;
  99. box-sizing: border-box;
  100. /* #endif */
  101. position: relative;
  102. flex: 1;
  103. justify-content: center;
  104. align-items: center;
  105. }
  106. .segmented-control__item--button {
  107. border-style: solid;
  108. border-top-width: 1px;
  109. border-bottom-width: 1px;
  110. border-right-width: 1px;
  111. border-left-width: 0;
  112. }
  113. .segmented-control__item--button--first {
  114. border-left-width: 1px;
  115. border-top-left-radius: 5px;
  116. border-bottom-left-radius: 5px;
  117. }
  118. .segmented-control__item--button--last {
  119. border-top-right-radius: 5px;
  120. border-bottom-right-radius: 5px;
  121. }
  122. .segmented-control__item--text {
  123. border-bottom-style: solid;
  124. border-bottom-width: 3px;
  125. }
  126. .segmented-control__text {
  127. font-size: 16px;
  128. line-height: 20px;
  129. text-align: center;
  130. }
  131. </style>