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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { defineComponent, h, inject, mergeProps, nextTick, type PropType, reactive, ref } from "vue";
import { Close } from "@element-plus/icons-vue";
import { useBrowser, useConfig, useCore } from "../../hooks";
import { renderNode } from "../../utils/vnode";
import { useApi } from "../form/helper";
import { isArray } from "lodash-es";
 
export default defineComponent({
    name: "cl-adv-search",
 
    components: {
        Close
    },
 
    props: {
        // 表单项
        items: {
            type: Array as PropType<ClForm.Item[]>,
            default: () => []
        },
        // 标题
        title: String,
        // 窗体大小
        size: {
            type: [Number, String],
            default: "30%"
        },
        // 操作按钮
        op: {
            type: Array,
            default: () => ["clear", "reset", "close", "search"]
        },
        // 搜索钩子
        onSearch: Function
    },
 
    emits: ["reset", "clear"],
 
    setup(props, { emit, slots, expose }) {
        const { crud, mitt } = useCore();
        const { style } = useConfig();
        const browser = useBrowser();
 
        // 配置
        const config = reactive<ClAdvSearch.Config>(
            mergeProps(props, inject("useAdvSearch__options") || {})
        );
 
        // cl-form
        const Form = ref<ClForm.Ref>();
 
        // el-drawer
        const Drawer = ref();
 
        // 是否可见
        const visible = ref(false);
 
        // 打开
        function open() {
            visible.value = true;
 
            nextTick(() => {
                Form.value?.open({
                    items: config.items || [],
                    op: {
                        hidden: true
                    },
                    isReset: false
                });
            });
        }
 
        // 关闭
        function close() {
            Drawer.value.handleClose();
        }
 
        // 重置数据
        function reset() {
            const d: any = {};
 
            config.items?.map((e) => {
                if (typeof e.hook != 'string' && e.hook?.reset) {
                    const props = e.hook.reset(e.prop!)
 
                    if (isArray(props)) {
                        props.forEach((prop) => {
                            d[prop] = undefined;
                        })
                    }
                }
 
                d[e.prop!] = undefined;
            });
 
            // 重置表单
            Form.value?.reset();
 
            // 列表刷新
            search();
 
            // 重置事件
            emit("reset", d);
        }
 
        // 清空数据
        function clear() {
            Form.value?.clear();
            emit("clear");
        }
 
        // 搜素请求
        function search(params?: any) {
            const form = Form.value?.getForm();
 
            function next(data: any) {
                Form.value?.done();
                close();
 
                return crud.refresh({
                    ...data,
                    ...params,
                    page: 1
                });
            }
 
            if (config.onSearch) {
                config.onSearch(form, { next, close });
            } else {
                next(form);
            }
        }
 
        // 消息事件
        mitt.on("crud.openAdvSearch", open);
 
        // 渲染表单
        function renderForm() {
            return h(<cl-form ref={Form} inner enable-plugin={false} />, {}, slots);
        }
 
        // 渲染底部
        function renderFooter() {
            const fns = { search, reset, clear, close };
 
            return config.op?.map((e: string) => {
                switch (e) {
                    case "search":
                    case "reset":
                    case "clear":
                    case "close":
                        return h(
                            <el-button />,
                            {
                                type: e == "search" ? "primary" : null,
                                size: style.size,
                                onClick: () => {
                                    fns[e]()
                                }
                            },
                            { default: () => crud.dict.label[e] }
                        );
 
                    default:
                        return renderNode(e, {
                            scope: Form.value?.getForm(),
                            slots
                        });
                }
            });
        }
 
        expose({
            open,
            close,
            clear,
            ...useApi({ Form }),
            reset,
            Form
        });
 
        return () => {
            return (
                <el-drawer
                    ref={Drawer}
                    modal-class="cl-adv-search"
                    v-model={visible.value}
                    direction="rtl"
                    with-header={false}
                    size={browser.isMini ? "100%" : config.size}>
                    <div class="cl-adv-search__header">
                        <span class="text">{config.title || crud.dict.label.advSearch}</span>
                        <el-icon size={20} onClick={close}>
                            <Close />
                        </el-icon>
                    </div>
                    <div class="cl-adv-search__container">{renderForm()}</div>
                    <div class="cl-adv-search__footer">{renderFooter()}</div>
                </el-drawer>
            );
        };
    }
});