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