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
147
148
149
150
151
import { computed, ref } from "vue";
 
export function useTabs({ config, Form }: { config: ClForm.Config; Form: Vue.Ref<any> }) {
    // 选中
    const active = ref<string | undefined>();
 
    // 列表
    const list = computed(() => {
        return get()?.props?.labels || [];
    });
 
    // 获取选项
    function getItem(value: any) {
        return list.value.find((e) => e.value == value);
    }
 
    // 是否已加载
    function isLoaded(value: any) {
        const d = getItem(value);
        return d?.lazy ? d.loaded : true;
    }
 
    // 加载后
    function onLoad(value: any) {
        const d = getItem(value);
        d!.loaded = true;
    }
 
    // 查找分组
    function toGroup(opts: { config: ClForm.Config; prop: string; refs: any }) {
        if (active.value) {
            let name;
 
            // 查找标签上绑定的数据
            const el = opts.refs.form.querySelector(`[data-prop="${opts.prop}"]`);
 
            // 各自判断
            if (el) {
                name = el?.getAttribute("data-group");
            } else {
                function deep(d: ClForm.Item) {
                    if (d.prop == opts.prop) {
                        name = d.group;
                    } else {
                        if (d.children) {
                            d.children.forEach(deep);
                        }
                    }
                }
 
                config.items.forEach(deep);
            }
 
            if (name) {
                set(name);
            }
        }
    }
 
    // 获取参数
    function get() {
        return config.items.find((e) => e.type === "tabs");
    }
 
    // 设置参数
    function set(data: any) {
        active.value = data;
    }
 
    // 清空
    function clear() {
        // 清空选中
        active.value = undefined;
 
        // 清空加载状态
        list.value.forEach((e) => {
            if (e.lazy && e.loaded) {
                e.loaded = undefined;
            }
        });
    }
 
    // 切换
    function change(value: any, isValid = true) {
        return new Promise((resolve: Function, reject: Function) => {
            function next() {
                active.value = value;
                resolve();
            }
 
            if (isValid) {
                let isError = false;
 
                const arr = config.items
                    .filter((e) => e.group == active.value && !e._hidden && e.prop)
                    .map((e) => {
                        return new Promise((r: Function) => {
                            // 验证表单
                            Form.value.validateField(e.prop, (valid: string) => {
                                if (valid) {
                                    isError = true;
                                }
 
                                r(valid);
                            });
                        });
                    });
 
                Promise.all(arr).then((msg) => {
                    if (isError) {
                        reject(msg.filter(Boolean));
                    } else {
                        next();
                    }
                });
            } else {
                next();
            }
        });
    }
 
    // 合并
    function mergeProp(item: ClForm.Item) {
        const d = get();
 
        if (d && d.props) {
            const { mergeProp, labels = [] } = d.props;
 
            if (mergeProp) {
                const t = labels.find((e) => e.value == item.group);
 
                if (t && t.name) {
                    item.prop = `${t.name}-${item.prop}`;
                }
            }
        }
    }
 
    return {
        active,
        list,
        isLoaded,
        onLoad,
        get,
        set,
        change,
        clear,
        mergeProp,
        toGroup
    };
}