uni-segmented-control.vue 3.7 KB

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