wangrong
7 天以前 5ebdf612b8291106370bba5e79996b28a7452e67
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
import { defineComponent, ref, computed, type PropType, useModel } from "vue";
import { useConfig, useCore } from "../../hooks";
import { parsePx } from "../../utils";
import { debounce } from "lodash-es";
 
export default defineComponent({
    name: "cl-search-key",
 
    props: {
        // 绑定值
        modelValue: String,
        // 选中字段
        field: {
            type: String,
            default: "keyWord"
        },
        // 字段列表
        fieldList: {
            type: Array as PropType<Array<{ label: string; value: string }>>,
            default: () => []
        },
        // 搜索时的钩子
        onSearch: Function,
        // 输入框占位内容
        placeholder: String,
        // 宽度
        width: {
            type: [String, Number],
            default: 280
        },
        // 是否实时刷新
        refreshOnInput: Boolean
    },
 
    emits: ["update:modelValue", "change", "field-change"],
 
    setup(props, { emit, expose }) {
        const { crud } = useCore();
        const { style } = useConfig();
 
        // 选中字段
        const selectField = ref(props.field);
 
        // 加载状态
        const loading = ref(false);
 
        // 文字提示
        const placeholder = computed(() => {
            if (props.placeholder) {
                return props.placeholder;
            } else {
                const item = props.fieldList.find((e) => e.value == selectField.value);
 
                if (item) {
                    return crud.dict.label.placeholder + item.label;
                } else {
                    return crud.dict.label.searchKey;
                }
            }
        });
 
        // 搜索内容
        const value = useModel(props, "modelValue");
 
        // 锁
        let lock = false;
 
        // 搜索
        function search() {
            if (!lock) {
                const params: obj = {};
 
                props.fieldList.forEach((e) => {
                    params[e.value] = undefined;
                });
 
                async function next(newParams?: obj) {
                    loading.value = true;
 
                    await crud.refresh({
                        page: 1,
                        ...params,
                        [selectField.value]: value.value || undefined,
                        ...newParams
                    })
                        .catch(err => {
                            console.error(err);
                        })
 
                    loading.value = false;
                }
 
                if (props.onSearch) {
                    props.onSearch(params, { next });
                } else {
                    next();
                }
            }
        }
 
        // 回车搜索
        function onKeydown({ key }: KeyboardEvent) {
            if (key === "Enter") {
                search();
            }
        }
 
        // 监听变化
        function onChange(val: string) {
            if (!props.refreshOnInput) {
                search();
                lock = true;
 
                setTimeout(() => {
                    lock = false;
                }, 300);
 
                emit("change", val);
            }
        }
 
        // 监听输入
        const onInput = debounce((val: string) => {
            emit("change", val);
 
            if (props.refreshOnInput) {
                search();
            }
        }, 300);
 
        // 监听字段选择
        function onFieldChange() {
            emit("field-change", selectField.value);
            value.value = undefined;
        }
 
        expose({
            search
        });
 
        return () => {
            return (
                <div class="cl-search-key">
                    <el-select
                        class="cl-search-key__select"
                        size={style.size}
                        v-model={selectField.value}
                        v-show={props.fieldList.length > 0}
                        onChange={onFieldChange}>
                        {props.fieldList.map((e, i) => (
                            <el-option key={i} label={e.label} value={e.value} />
                        ))}
                    </el-select>
 
                    <div class="cl-search-key__wrap" style={{ width: parsePx(props.width) }}>
                        <el-input
                            v-model={value.value}
                            size={style.size}
                            placeholder={placeholder.value}
                            onKeydown={onKeydown}
                            onChange={onChange}
                            onInput={onInput}
                            clearable
                        />
 
                        <el-button
                            size={style.size}
                            type="primary"
                            loading={loading.value}
                            onClick={search}>
                            {crud.dict.label.search}
                        </el-button>
                    </div>
                </div>
            );
        };
    }
});