BtnGroup.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <view class="btn-group">
  3. <button class="btn" :class="currentVal === item.value ? 'active' : ''" v-for="item in options" :key="item.value" @click="handleClick(item.value)">{{ item.label }}</button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'BtnGroup',
  9. props: {
  10. value: {
  11. type: Number,
  12. default: 1,
  13. },
  14. defaultVal: {
  15. type: Number,
  16. default: 1,
  17. },
  18. options: {
  19. type: Array,
  20. default: () => [
  21. { label: '日', value: 1 },
  22. { label: '周', value: 2 },
  23. { label: '月', value: 3 },
  24. ]
  25. },
  26. },
  27. data() {
  28. return {
  29. currentVal: 1,
  30. }
  31. },
  32. watch: {
  33. defaultVal: {
  34. handler(newVal) {
  35. this.currentVal = newVal
  36. },
  37. immediate: true
  38. }
  39. },
  40. methods: {
  41. handleClick(val) {
  42. this.currentVal = val
  43. this.$emit('input', val)
  44. },
  45. },
  46. }
  47. </script>
  48. <style lang="scss">
  49. .btn-group {
  50. position: absolute;
  51. top: 0;
  52. right: 0;
  53. z-index: 999999;
  54. display: flex;
  55. }
  56. .btn {
  57. padding: 8rpx 16rpx;
  58. gap: 0rpx 16rpx;
  59. font-size: 24rpx;
  60. line-height: 32rpx;
  61. background-color: #F5F5F5;
  62. color: #333333;
  63. margin-left: 16rpx;
  64. }
  65. .btn.active {
  66. background-color: #00C18A;
  67. color: white;
  68. }
  69. </style>