1
0

mySegmentedControl.vue 3.7 KB

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