wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { defineComponent, h, ref, toValue, type PropType } from 'vue';
import { CrudProps } from '../../comm';
import { cloneDeep, isArray, isString } from 'lodash-es';
import { deepFind } from '/$/dict/utils';
 
export default defineComponent({
    name: 'cl-dict',
 
    props: {
        ...CrudProps,
        // 绑定值
        modelValue: [String, Array, Number],
        // 选项列表
        options: {
            type: Array as PropType<DictOptions[]>,
            default: () => []
        },
        // 格式化返回
        formatter: Function as PropType<(arr: DictOptions[]) => any>,
        // 颜色组
        color: {
            type: Array as PropType<string[]>,
            default: () => []
        },
        // 分割符号
        separator: {
            type: String,
            default: ','
        },
        // 展示所有层级
        allLevels: Boolean,
        // 超出几个隐藏
        hideOver: {
            type: Number,
            default: 5
        },
        // 纯文字显示
        text: Boolean
    },
 
    setup(props) {
        // 选项列表
        const list: DictOptions = cloneDeep(toValue(props.options || []));
 
        // 设置颜色
        if (props.color) {
            list.forEach((e, i) => {
                if (!e.color) {
                    e.color = props.color[i];
                }
            });
        }
 
        // 是否展开
        const isExpand = ref(false);
 
        return () => {
            const value = props.modelValue;
 
            // 绑定值
            let values: any[] = [];
 
            // 格式化值
            if (isArray(value)) {
                values = value;
            } else if (isString(value)) {
                if (props.separator) {
                    values = value.split(props.separator);
                } else {
                    values = [value];
                }
            } else {
                values = [value];
            }
 
            // 数据
            const data = values
                .filter(e => e !== undefined && e !== null && e !== '')
                .map(v => {
                    let d = deepFind(v, list, { allLevels: props.allLevels });
 
                    if (!d) {
                        d = {
                            label: v,
                            value: v
                        };
                    }
 
                    return {
                        ...d,
                        children: []
                    };
                });
 
            // 是否隐藏部分
            const isHide = data.length > props.hideOver && !isExpand.value;
 
            // 自定义返回
            if (props.formatter) {
                return props.formatter(data);
            }
 
            // 文字返回
            if (props.text) {
                return data.map(e => e.label).join(props.separator);
            }
 
            // el-tag 渲染
            return [
                data
                    .filter((_, i) => {
                        return !isHide || i < props.hideOver;
                    })
                    .map(e => {
                        return h(
                            <el-tag style={{ margin: '2px', border: e.color ? '0' : null }} />,
                            {
                                type: e.type,
                                closable: e.closable,
                                hit: e.hit,
                                color: e.color,
                                size: e.size,
                                effect: e.effect || 'dark',
                                round: e.round,
                                disableTransitions: true
                            },
                            {
                                default: () => e.label
                            }
                        );
                    }),
                isHide &&
                    h(
                        <el-link
                            style={{ marginLeft: '5px', userSelect: 'none', cursor: 'pointer' }}
                            onClick={() => {
                                isExpand.value = true;
                            }}
                        />,
                        {},
                        { default: () => '...' }
                    )
            ];
        };
    }
});