|
|
1
|
+<script lang="ts">
|
|
|
2
|
+ export type OptionsItem = { label: string; value: string; disabled?: boolean };
|
|
|
3
|
+ export interface OnChangeHookParams {
|
|
|
4
|
+ options: Ref<OptionsItem[]>;
|
|
|
5
|
+ }
|
|
|
6
|
+</script>
|
|
|
7
|
+
|
|
|
8
|
+<script lang="ts" setup>
|
|
|
9
|
+ import { ref, watchEffect, computed, unref, watch, Ref } from 'vue';
|
|
|
10
|
+ import { Select } from 'ant-design-vue';
|
|
|
11
|
+ import { isFunction } from '/@/utils/is';
|
|
|
12
|
+ import { useRuleFormItem } from '/@/hooks/component/useFormItem';
|
|
|
13
|
+ import { get, omit } from 'lodash-es';
|
|
|
14
|
+ import { LoadingOutlined } from '@ant-design/icons-vue';
|
|
|
15
|
+ import { useI18n } from '/@/hooks/web/useI18n';
|
|
|
16
|
+ import { useDebounceFn } from '@vueuse/shared';
|
|
|
17
|
+
|
|
|
18
|
+ const emit = defineEmits(['options-change', 'change']);
|
|
|
19
|
+ const props = withDefaults(
|
|
|
20
|
+ defineProps<{
|
|
|
21
|
+ value?: Recordable | number | string;
|
|
|
22
|
+ numberToString?: boolean;
|
|
|
23
|
+ api?: (arg?: Recordable) => Promise<Recordable>;
|
|
|
24
|
+ queryApi?: (value?: any) => Promise<Recordable>;
|
|
|
25
|
+ params?: Recordable | ((searchText?: string) => Recordable);
|
|
|
26
|
+ resultField?: string;
|
|
|
27
|
+ labelField?: string;
|
|
|
28
|
+ valueField?: string;
|
|
|
29
|
+ immediate?: boolean;
|
|
|
30
|
+ queryEmptyDataAgin?: boolean;
|
|
|
31
|
+ }>(),
|
|
|
32
|
+ {
|
|
|
33
|
+ resultField: '',
|
|
|
34
|
+ labelField: 'label',
|
|
|
35
|
+ valueField: 'value',
|
|
|
36
|
+ searchField: 'text',
|
|
|
37
|
+ immediate: true,
|
|
|
38
|
+ queryEmptyDataAgin: true,
|
|
|
39
|
+ }
|
|
|
40
|
+ );
|
|
|
41
|
+
|
|
|
42
|
+ const selectOption = ref<OptionsItem>();
|
|
|
43
|
+ const options = ref<OptionsItem[]>([]);
|
|
|
44
|
+ const loading = ref(false);
|
|
|
45
|
+ const isFirstLoad = ref(true);
|
|
|
46
|
+ const emitData = ref<any[]>([]);
|
|
|
47
|
+ const { t } = useI18n();
|
|
|
48
|
+
|
|
|
49
|
+ // Embedded in the form, just use the hook binding to perform form verification
|
|
|
50
|
+ const [state] = useRuleFormItem(props, 'value', 'change', emitData);
|
|
|
51
|
+
|
|
|
52
|
+ const getOptions = computed(() => {
|
|
|
53
|
+ const { labelField, valueField = 'value', numberToString } = props;
|
|
|
54
|
+ const _options = unref(options);
|
|
|
55
|
+
|
|
|
56
|
+ if (
|
|
|
57
|
+ unref(selectOption) &&
|
|
|
58
|
+ !_options.find((item) => get(item, valueField) === get(unref(selectOption), valueField))
|
|
|
59
|
+ ) {
|
|
|
60
|
+ _options.push(unref(selectOption)!);
|
|
|
61
|
+ }
|
|
|
62
|
+ return _options.reduce((prev, next: Recordable) => {
|
|
|
63
|
+ if (next) {
|
|
|
64
|
+ const value = get(next, valueField);
|
|
|
65
|
+ const label = get(next, labelField);
|
|
|
66
|
+ prev.push({
|
|
|
67
|
+ ...omit(next, [labelField, valueField]),
|
|
|
68
|
+ label,
|
|
|
69
|
+ value: numberToString ? `${value}` : value,
|
|
|
70
|
+ });
|
|
|
71
|
+ }
|
|
|
72
|
+ return prev;
|
|
|
73
|
+ }, [] as OptionsItem[]);
|
|
|
74
|
+ });
|
|
|
75
|
+
|
|
|
76
|
+ watchEffect(() => {
|
|
|
77
|
+ props.immediate && fetch();
|
|
|
78
|
+ });
|
|
|
79
|
+
|
|
|
80
|
+ watch(
|
|
|
81
|
+ () => props.params,
|
|
|
82
|
+ () => {
|
|
|
83
|
+ !unref(isFirstLoad) && fetch();
|
|
|
84
|
+ },
|
|
|
85
|
+ { deep: true }
|
|
|
86
|
+ );
|
|
|
87
|
+
|
|
|
88
|
+ watch(
|
|
|
89
|
+ () => props.value,
|
|
|
90
|
+ async (target) => {
|
|
|
91
|
+ if (target && props.queryApi && isFunction(props.queryApi)) {
|
|
|
92
|
+ if (unref(getOptions).find((item) => item.value === target)) return;
|
|
|
93
|
+ const detail = await props.queryApi(target);
|
|
|
94
|
+ if (
|
|
|
95
|
+ unref(options).find(
|
|
|
96
|
+ (item) => get(item, props.valueField) === get(detail, props.valueField)
|
|
|
97
|
+ )
|
|
|
98
|
+ )
|
|
|
99
|
+ return;
|
|
|
100
|
+
|
|
|
101
|
+ selectOption.value = detail as OptionsItem;
|
|
|
102
|
+ }
|
|
|
103
|
+ },
|
|
|
104
|
+ {
|
|
|
105
|
+ immediate: true,
|
|
|
106
|
+ }
|
|
|
107
|
+ );
|
|
|
108
|
+
|
|
|
109
|
+ async function fetch(searchText?: string) {
|
|
|
110
|
+ const api = props.api;
|
|
|
111
|
+ if (!api || !isFunction(api)) return;
|
|
|
112
|
+ options.value = [];
|
|
|
113
|
+ try {
|
|
|
114
|
+ loading.value = true;
|
|
|
115
|
+ const params =
|
|
|
116
|
+ props.params && isFunction(props.params) ? props.params(searchText) : props.params;
|
|
|
117
|
+
|
|
|
118
|
+ const res = await api(params);
|
|
|
119
|
+
|
|
|
120
|
+ if (Array.isArray(res)) {
|
|
|
121
|
+ options.value = res;
|
|
|
122
|
+ emitChange();
|
|
|
123
|
+ return;
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ if (props.resultField) {
|
|
|
127
|
+ options.value = get(res, props.resultField) || [];
|
|
|
128
|
+ }
|
|
|
129
|
+ emitChange();
|
|
|
130
|
+ } catch (error) {
|
|
|
131
|
+ console.warn(error);
|
|
|
132
|
+ } finally {
|
|
|
133
|
+ loading.value = false;
|
|
|
134
|
+ }
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ async function handleFetch() {
|
|
|
138
|
+ const { immediate } = props;
|
|
|
139
|
+ if (!immediate && unref(isFirstLoad)) {
|
|
|
140
|
+ await fetch();
|
|
|
141
|
+ isFirstLoad.value = false;
|
|
|
142
|
+ }
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ function emitChange() {
|
|
|
146
|
+ emit('options-change', unref(getOptions));
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ function handleChange(value: string, ...args) {
|
|
|
150
|
+ emitData.value = args;
|
|
|
151
|
+ if (!value && props.queryEmptyDataAgin) fetch();
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ const debounceSearchFunction = useDebounceFn(fetch, 300);
|
|
|
155
|
+</script>
|
|
|
156
|
+
|
|
|
157
|
+<template>
|
|
|
158
|
+ <Select
|
|
|
159
|
+ v-bind="$attrs"
|
|
|
160
|
+ show-search
|
|
|
161
|
+ @dropdownVisibleChange="handleFetch"
|
|
|
162
|
+ @change="handleChange"
|
|
|
163
|
+ :options="getOptions"
|
|
|
164
|
+ :filter-option="false"
|
|
|
165
|
+ @search="debounceSearchFunction"
|
|
|
166
|
+ v-model:value="state"
|
|
|
167
|
+ >
|
|
|
168
|
+ <template #[item]="data" v-for="item in Object.keys($slots)">
|
|
|
169
|
+ <slot :name="item" v-bind="data || {}"></slot>
|
|
|
170
|
+ </template>
|
|
|
171
|
+ <template #suffixIcon v-if="loading">
|
|
|
172
|
+ <LoadingOutlined spin />
|
|
|
173
|
+ </template>
|
|
|
174
|
+ <template #notFoundContent v-if="loading">
|
|
|
175
|
+ <span>
|
|
|
176
|
+ <LoadingOutlined spin class="mr-1" />
|
|
|
177
|
+ {{ t('component.form.apiSelectNotFound') }}
|
|
|
178
|
+ </span>
|
|
|
179
|
+ </template>
|
|
|
180
|
+ </Select>
|
|
|
181
|
+</template> |
...
|
...
|
|