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
import { useRefs } from '/@/cool';
 
/**
 * 设置聚焦,prop为空则默认第一个选项
 * @param prop 字段标识
 * @returns
 */
export function setFocus(prop?: string): ClForm.Plugin {
    const { refs, setRefs } = useRefs();
 
    return ({ exposed, onOpen }) => {
        if (prop === '') {
            return;
        }
 
        const name = prop || exposed.config?.items?.[0]?.prop;
        let _ref: any;
 
        if (name) {
            function deep(arr: ClForm.Item[]) {
                arr.forEach(e => {
                    if (e.prop == name && name) {
                        if (e.component) {
                            if (e.component.ref) {
                                _ref = e.component.ref();
                            } else {
                                e.component.ref = setRefs(name);
                            }
                        }
                    } else {
                        deep(e.children || []);
                    }
                });
            }
 
            deep(exposed.config?.items || []);
 
            onOpen(() => {
                if (_ref) {
                    refs[name] = _ref();
                }
 
                refs[name]?.focus?.();
            });
        }
    };
}