| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <template>
- <view class="tab-radio-container" :style="{ gridTemplateColumns: `repeat(${colNum}, 1fr)` }">
- <view
- class="tab-item"
- :class="{
- 'tab-item--active': isMultiple ? activeValues.includes(item[valueKey]) : activeValue === item[valueKey],
- 'tab-item--multiple': isMultiple
- }"
- v-for="(item, index) in tabList"
- :key="index"
- @click="handleTabClick(item)"
- >
- <text class="tab-item__text">{{ item[labelKey] }}</text>
- <!-- <view v-if="isMultiple" class="tab-item__checkbox" :class="{ 'tab-item__checkbox--checked': activeValues.includes(item.value) }">
- <view v-if="activeValues.includes(item.value)" class="tab-item__checkbox-inner"></view>
- </view> -->
- </view>
- </view>
- </template>
- <script>
- export default {
- name: "TabRadio",
- props: {
- tabList: {
- type: Array,
- required: true,
- default: () => []
- },
- defaultActive: {
- type: [String, Number, Array],
- default: ""
- },
- colNum: {
- type: Number,
- default: 4
- },
- mode: {
- type: String,
- default: "single",
- validator: (value) => ["single", "multiple"].includes(value)
- },
- echoInfo: {
- type: [String, Number, Array],
- default: ""
- },
- isClear: {
- type: Boolean,
- default: false
- },
- valueKey: {
- type: String,
- default: "value"
- },
- labelKey: {
- type: String,
- default: "name"
- }
- },
- computed: {
- isMultiple() {
- return this.mode === "multiple";
- }
- },
- data() {
- // 优先使用 echoInfo,如果 echoInfo 不为空则使用它,否则使用 defaultActive
- const initialValue = this.echoInfo || this.defaultActive;
- return {
- activeValue: this.isMultiple ? '' : initialValue,
- activeValues: this.isMultiple ? (Array.isArray(initialValue) ? initialValue : []) : []
- };
- },
- watch: {
- defaultActive: {
- handler(val) {
- // 只有当 echoInfo 为空时,才使用 defaultActive
- if (!this.echoInfo) {
- if (this.isMultiple) {
- this.activeValues = Array.isArray(val) ? val : [];
- } else {
- this.activeValue = val;
- }
- }
- },
- immediate: true
- },
- echoInfo: {
- handler(val) {
- if (val !== undefined && val !== null) {
- if (this.isMultiple) {
- this.activeValues = Array.isArray(val) ? val : [];
- } else {
- this.activeValue = val;
- }
- }
- },
- immediate: true
- },
- mode: {
- handler(newMode) {
- // 当 mode 变化时,重新初始化选中值
- const initialValue = this.echoInfo || this.defaultActive;
- if (newMode === 'multiple') {
- this.activeValues = Array.isArray(initialValue) ? initialValue : [];
- this.activeValue = '';
- } else {
- this.activeValue = initialValue;
- this.activeValues = [];
- }
- },
- immediate: true
- },
- isClear: {
- handler(val) {
- if (val) {
- this.activeValue = '';
- this.activeValues = [];
- }
- },
- immediate: true
- }
- },
- methods: {
- handleTabClick(item) {
- if (this.isMultiple) {
- this.handleMultipleSelect(item);
- } else {
- this.handleSingleSelect(item);
- }
- },
- handleSingleSelect(item) {
- this.activeValue = item[this.valueKey];
- this.$emit("tabChange", item[this.valueKey]);
- },
- handleMultipleSelect(item) {
- const index = this.activeValues.indexOf(item[this.valueKey]);
- if (index > -1) {
- this.activeValues.splice(index, 1);
- } else {
- this.activeValues.push(item[this.valueKey]);
- }
- this.$emit("tabChange", [...this.activeValues]);
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "./index.scss";
- </style>
|