uni-segmented-control.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. emits:['clickItem'],
  37. props: {
  38. current: {
  39. type: Number,
  40. default: 0
  41. },
  42. values: {
  43. type: Array,
  44. default () {
  45. return []
  46. }
  47. },
  48. activeColor: {
  49. type: String,
  50. default: '#007aff'
  51. },
  52. styleType: {
  53. type: String,
  54. default: 'button'
  55. }
  56. },
  57. data() {
  58. return {
  59. currentIndex: 0
  60. }
  61. },
  62. watch: {
  63. current(val) {
  64. if (val !== this.currentIndex) {
  65. this.currentIndex = val
  66. }
  67. }
  68. },
  69. created() {
  70. this.currentIndex = this.current
  71. },
  72. methods: {
  73. _onClick(index) {
  74. if (this.currentIndex !== index) {
  75. this.currentIndex = index
  76. this.$emit('clickItem', {
  77. currentIndex: index
  78. })
  79. }
  80. }
  81. }
  82. }
  83. </script>
  84. <style lang="scss" scoped>
  85. .segmented-control {
  86. /* #ifndef APP-NVUE */
  87. display: flex;
  88. box-sizing: border-box;
  89. /* #endif */
  90. flex-direction: row;
  91. height: 36px;
  92. overflow: hidden;
  93. /* #ifdef H5 */
  94. cursor: pointer;
  95. /* #endif */
  96. }
  97. .segmented-control__item {
  98. /* #ifndef APP-NVUE */
  99. display: inline-flex;
  100. box-sizing: border-box;
  101. /* #endif */
  102. position: relative;
  103. flex: 1;
  104. justify-content: center;
  105. align-items: center;
  106. }
  107. .segmented-control__item--button {
  108. border-style: solid;
  109. border-top-width: 1px;
  110. border-bottom-width: 1px;
  111. border-right-width: 1px;
  112. border-left-width: 0;
  113. }
  114. .segmented-control__item--button--first {
  115. border-left-width: 1px;
  116. border-top-left-radius: 5px;
  117. border-bottom-left-radius: 5px;
  118. }
  119. .segmented-control__item--button--last {
  120. border-top-right-radius: 5px;
  121. border-bottom-right-radius: 5px;
  122. }
  123. .segmented-control__item--text {
  124. border-bottom-style: solid;
  125. border-bottom-width: 3px;
  126. }
  127. .segmented-control__text {
  128. font-size: 16px;
  129. line-height: 20px;
  130. text-align: center;
  131. }
  132. </style>