uni-segmented-control.vue 3.4 KB

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