tm-groupButton.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view class="tm-groupButton " :class="[`mx-${margin[0]}`,`my-${margin[1]}`]">
  3. <view class="overflow fulled " :class="[`round-${round}`]">
  4. <slot></slot>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. /**
  10. * 按钮组
  11. * @param {Number} round = [] 默认4,圆角
  12. * @param {Number} margin = [] 默认 [0,24] 上下间距
  13. * @param {Number} active-color = [] 默认 primary 点按激活的颜色主题
  14. * @property {Function} change 点按按钮选中时的触发,返回当前索引值。
  15. */
  16. export default {
  17. name:"tm-groupButton",
  18. props: {
  19. round: {
  20. type: Number|String,
  21. default: 4
  22. },
  23. margin: {
  24. type: Array,
  25. default: ()=>[0,24]
  26. },
  27. activeColor: {
  28. type: String,
  29. default: 'primary'
  30. },
  31. },
  32. data() {
  33. return {
  34. activeIndex:null,
  35. };
  36. },
  37. mounted() {
  38. this.$nextTick(function(){
  39. this.inits();
  40. })
  41. },
  42. updated(){
  43. this.$nextTick(function () {
  44. this.inits();
  45. })
  46. },
  47. methods: {
  48. inits() {
  49. let t = this;
  50. let ch = [];
  51. // #ifndef H5
  52. ch = this.$children;
  53. // #endif
  54. // #ifdef H5
  55. ch = this.$children[0].$children[0].$children;
  56. // #endif
  57. ch.forEach((item, index) => {
  58. if (item.$options.name === 'tm-button') {
  59. let str = '';
  60. if(index==0){
  61. str = ' round-l-'+t.round+' round-r-0'
  62. }else if(index==ch.length-1){
  63. str = ' round-r-'+t.round+' round-l-0'
  64. }else{
  65. str = ' round-l-0 round-r-0'
  66. }
  67. item.customClass_puted=" ma-0 "+str;
  68. item.customDense_puted=true;
  69. item.onclick=function(){
  70. t.colorDefault();
  71. item.$emit('click',...arguments)
  72. t.$emit('change',index)
  73. t.activeIndex=index;
  74. item.color_tmeme = t.activeColor
  75. }
  76. if(index!==0){
  77. item.setConfigStyle('border-l-1')
  78. }
  79. }
  80. });
  81. },
  82. colorDefault(){
  83. let t = this;
  84. let ch = [];
  85. // #ifndef H5
  86. ch = this.$children;
  87. // #endif
  88. // #ifdef H5
  89. ch = this.$children[0].$children[0].$children;
  90. // #endif
  91. ch.forEach((item, index) => {
  92. if (item.$options.name === 'tm-button') {
  93. item.color_tmeme = item.theme
  94. }
  95. });
  96. }
  97. }
  98. }
  99. </script>
  100. <style lang="scss">
  101. </style>