CustomizeFormButtonSelect.vue 4.54 KB
<template>
    <div>
        <div class="selectList">
            <div v-for="(col, index) in columnsList" :key="index" class="selectBut"
                :class="{ 'pitchOn': pickerValue.includes(col.value) || pickerValue.includes(col.text), 'selectBut--readonly': readonly }" @click="handleBut(col)">
                {{ col.text }}
                <div v-if="pickerValue.includes(col.value) || pickerValue.includes(col.text)" class="pitchImg"></div>
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-facing-decorator";
import { TrackHelper } from "@/common/TrackHelper";

@Component({ components: {} })

export default class CustomizeFormButtonSelect extends Vue {
    @Prop({ default: () => [] })
    columnsList: any = [];
    @Prop({ default: () => [] })
    item: any = {};
    @Prop({ default: false })
    readonly = false;

    pickerValue = []

    // item.resultStatus 变化时,清空选择项
    @Watch("item.resultStatus")
    watchItem(newValue: boolean): void {
        if (newValue) {
            this.pickerValue = [];
            this.setupdate();
        }
    }

    mounted() {
        this.initData();
    }

    // 初始化数据
    initData() {
        if (this.item?.resultStatus) {
            this.pickerValue = [];
            return;
        }
        const valueList = this.item?.inspectionResultVO?.inspectionValueList || [];
        const normalized: string[] = [];
        for (const entry of valueList) {
            if (!entry || !entry.valueText) continue;
            if (String(entry.interactionType ?? "") !== "button_select") continue;
            const preferredKey = String(entry.valueKey ?? "");
            const preferredText = String(entry.valueText ?? "");
            const matched = (this.columnsList || []).find(
                (col) =>
                    (preferredKey && String(col.value) === preferredKey) ||
                    (preferredText && String(col.text) === preferredText)
            );
            const value = matched
                ? String(matched.value)
                : (preferredKey || preferredText);
            if (value && !normalized.includes(value)) {
                normalized.push(value);
            }
        }
        this.pickerValue = normalized;
        this.setupdate();
    }

    handleBut(item: { value: string }) {
        if (this.readonly) {
            return;
        }
        if (item.value) {
            this.item.resultStatus = false;
        }
        if (this.pickerValue.includes(item.value)) {
            this.pickerValue = this.pickerValue.filter((i) => i !== item.value);
        } else {
            this.pickerValue.push(item.value);
        }
        this.setupdate();
    }

    setupdate() {
        const valueList = (this.pickerValue || []).map((item) => {
            const object =
                (this.columnsList || []).find(
                    (finditem) => finditem.value == item,
                ) ||
                (this.columnsList || []).find(
                    (finditem) => finditem.text == item,
                ) || {};
            const pickedKey = object.value ?? item ?? "";
            const pickedText =
                object.text || (item && !object.value ? String(item) : "") || "";
            return {
                valueText: pickedText,
                valueKey: String(pickedKey),
            };
        });
        const nextValue =
            valueList.length > 0 ? JSON.stringify(valueList) : "";
        if (
            TrackHelper.normalize(this.item?.value) ===
            TrackHelper.normalize(nextValue)
        ) {
            return;
        }
        this.$emit("update", nextValue);
    }

}
</script>

<style scoped lang="less">

.selectList {
    width: 100%;
}

.selectBut {
    position: relative;
    float: left;
    min-width: 76px;
    padding: 0 24px;
    height: 36px;
    margin-right: 6px;
    margin-bottom: 12px;
    margin-left: 6px;
    font-size: 14px;
    font-weight: 400;
    line-height: 36px;
    color: #333;
    text-align: center;
    cursor: pointer;
    background: #f9f9f9;
    border: 1px solid #f9f9f9;
    border-radius: 6px;
}

.selectBut--readonly {
    cursor: default;
}

.pitchOn {
    color: #185EFE !important;
    background: #ecf1ff !important;
    border: 1px solid #185efe !important;
}

.pitchImg {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 99;
    width: 16px;
    height: 16px;
    background-image: url("https://img02.glsx.com.cn/aus/images/preference/pitchOn.png");
    background-repeat: no-repeat;
    background-size: 100%;
}
</style>