index.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view class="tab-radio-container" :style="{ gridTemplateColumns: `repeat(${colNum}, 1fr)` }">
  3. <view
  4. class="tab-item"
  5. :class="{
  6. 'tab-item--active': isMultiple ? activeValues.includes(item.value) : activeValue === item.value,
  7. 'tab-item--multiple': isMultiple
  8. }"
  9. v-for="(item, index) in tabList"
  10. :key="index"
  11. @click="handleTabClick(item)"
  12. >
  13. <text class="tab-item__text">{{ item.name }}</text>
  14. <!-- <view v-if="isMultiple" class="tab-item__checkbox" :class="{ 'tab-item__checkbox--checked': activeValues.includes(item.value) }">
  15. <view v-if="activeValues.includes(item.value)" class="tab-item__checkbox-inner"></view>
  16. </view> -->
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: "TabRadio",
  23. props: {
  24. tabList: {
  25. type: Array,
  26. required: true,
  27. default: () => []
  28. },
  29. defaultActive: {
  30. type: [String, Number, Array],
  31. default: ""
  32. },
  33. colNum: {
  34. type: Number,
  35. default: 4
  36. },
  37. mode: {
  38. type: String,
  39. default: "single",
  40. validator: (value) => ["single", "multiple"].includes(value)
  41. }
  42. },
  43. computed: {
  44. isMultiple() {
  45. return this.mode === "multiple";
  46. }
  47. },
  48. data() {
  49. return {
  50. activeValue: this.defaultActive,
  51. activeValues: Array.isArray(this.defaultActive) ? this.defaultActive : []
  52. };
  53. },
  54. watch: {
  55. defaultActive: {
  56. handler(val) {
  57. if (this.isMultiple) {
  58. this.activeValues = Array.isArray(val) ? val : [];
  59. } else {
  60. this.activeValue = val;
  61. }
  62. },
  63. immediate: true
  64. }
  65. },
  66. methods: {
  67. handleTabClick(item) {
  68. if (this.isMultiple) {
  69. this.handleMultipleSelect(item);
  70. } else {
  71. this.handleSingleSelect(item);
  72. }
  73. },
  74. handleSingleSelect(item) {
  75. this.activeValue = item.value;
  76. this.$emit("tabChange", item.value);
  77. },
  78. handleMultipleSelect(item) {
  79. const index = this.activeValues.indexOf(item.value);
  80. if (index > -1) {
  81. this.activeValues.splice(index, 1);
  82. } else {
  83. this.activeValues.push(item.value);
  84. }
  85. this.$emit("tabChange", [...this.activeValues]);
  86. }
  87. }
  88. };
  89. </script>
  90. <style lang="scss" scoped>
  91. @import "./index.scss";
  92. </style>