uni-segmented-control.vue 3.6 KB

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