gui-segmented-control.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <view
  3. class="gui-flex gui-row gui-nowrap gui-justify-content-center gui-segmented-control"
  4. :class="customClass"
  5. :style="customStyle">
  6. <text
  7. v-for="(item, index) in items"
  8. :key="index"
  9. :class="initClass(index)"
  10. class="gui-block"
  11. :style="{borderRadius:borderRadius}"
  12. @tap.stop="changeSC"
  13. :data-index="index">{{item}}</text>
  14. </view>
  15. </template>
  16. <script>
  17. export default{
  18. name : "gui-segmented-control",
  19. props : {
  20. // 自定义行内样式
  21. customStyle : {
  22. type : String,
  23. default : ''
  24. },
  25. // 自定义样式
  26. customClass : {
  27. type : Array,
  28. default : function(){
  29. return ['gui-bg-white', 'gui-dark-bg-level-2', 'gui-padding-small'];
  30. }
  31. },
  32. items : {
  33. type : Array,
  34. default : function () { return new Array();}
  35. },
  36. itemClass : {
  37. type : Array,
  38. default : function () { return ['gui-text'];}
  39. },
  40. activeClass : {
  41. type : Array,
  42. default : function () { return ['gui-bg-primary', 'gui-color-white'];}
  43. },
  44. current : {
  45. type : Number,
  46. default : 0
  47. },
  48. borderRadius : {
  49. type : String,
  50. default : '6rpx'
  51. }
  52. },
  53. data(){
  54. return {
  55. currentIn: 0,
  56. classIn : [],
  57. }
  58. },
  59. created : function(){
  60. this.currentIn = this.current;
  61. this.initClass();
  62. },
  63. watch : {
  64. current : function (val) {
  65. this.currentIn = val;
  66. this.initClass()
  67. }
  68. },
  69. methods : {
  70. changeSC : function (e) {
  71. var index = Number(e.currentTarget.dataset.index);
  72. this.currentIn = index;
  73. this.$emit('change', index);
  74. },
  75. initClass : function (idx) {
  76. var classReturn = ['gui-segmented-control-item', 'gui-border-box'];
  77. classReturn = classReturn.concat(this.itemClass);
  78. if(this.currentIn == idx){
  79. classReturn = classReturn.concat(this.activeClass);
  80. classReturn.push('gui-fade-in');
  81. }
  82. return classReturn;
  83. }
  84. },
  85. emits : ['change']
  86. }
  87. </script>
  88. <style scoped>
  89. </style>