Commit fa64fc8a622832b87fdf672965d55d543b5929a2
1 parent
a2a75a09
fix(api-select): ensure that the onchange function parameters are correct
Showing
2 changed files
with
24 additions
and
6 deletions
src/components/Form/src/components/ApiSelect.vue
@@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
2 | <Select | 2 | <Select |
3 | @dropdownVisibleChange="handleFetch" | 3 | @dropdownVisibleChange="handleFetch" |
4 | v-bind="attrs" | 4 | v-bind="attrs" |
5 | + @change="handleChange" | ||
5 | :options="getOptions" | 6 | :options="getOptions" |
6 | v-model:value="state" | 7 | v-model:value="state" |
7 | > | 8 | > |
@@ -67,11 +68,12 @@ | @@ -67,11 +68,12 @@ | ||
67 | const options = ref<OptionsItem[]>([]); | 68 | const options = ref<OptionsItem[]>([]); |
68 | const loading = ref(false); | 69 | const loading = ref(false); |
69 | const isFirstLoad = ref(true); | 70 | const isFirstLoad = ref(true); |
71 | + const emitData = ref<any[]>([]); | ||
70 | const attrs = useAttrs(); | 72 | const attrs = useAttrs(); |
71 | const { t } = useI18n(); | 73 | const { t } = useI18n(); |
72 | 74 | ||
73 | // Embedded in the form, just use the hook binding to perform form verification | 75 | // Embedded in the form, just use the hook binding to perform form verification |
74 | - const [state] = useRuleFormItem(props); | 76 | + const [state] = useRuleFormItem(props, 'value', 'change', emitData); |
75 | 77 | ||
76 | const getOptions = computed(() => { | 78 | const getOptions = computed(() => { |
77 | const { labelField, valueField, numberToString } = props; | 79 | const { labelField, valueField, numberToString } = props; |
@@ -135,7 +137,11 @@ | @@ -135,7 +137,11 @@ | ||
135 | emit('options-change', unref(options)); | 137 | emit('options-change', unref(options)); |
136 | } | 138 | } |
137 | 139 | ||
138 | - return { state, attrs, getOptions, loading, t, handleFetch }; | 140 | + function handleChange(_, ...args) { |
141 | + emitData.value = args; | ||
142 | + } | ||
143 | + | ||
144 | + return { state, attrs, getOptions, loading, t, handleFetch, handleChange }; | ||
139 | }, | 145 | }, |
140 | }); | 146 | }); |
141 | </script> | 147 | </script> |
src/hooks/component/useFormItem.ts
1 | -import type { UnwrapRef } from 'vue'; | ||
2 | -import { reactive, readonly, computed, getCurrentInstance, watchEffect } from 'vue'; | 1 | +import type { UnwrapRef, Ref } from 'vue'; |
2 | +import { | ||
3 | + reactive, | ||
4 | + readonly, | ||
5 | + computed, | ||
6 | + getCurrentInstance, | ||
7 | + watchEffect, | ||
8 | + unref, | ||
9 | + nextTick, | ||
10 | + toRaw, | ||
11 | +} from 'vue'; | ||
3 | 12 | ||
4 | import { isEqual } from 'lodash-es'; | 13 | import { isEqual } from 'lodash-es'; |
5 | 14 | ||
6 | export function useRuleFormItem<T extends Recordable>( | 15 | export function useRuleFormItem<T extends Recordable>( |
7 | props: T, | 16 | props: T, |
8 | key: keyof T = 'value', | 17 | key: keyof T = 'value', |
9 | - changeEvent = 'change' | 18 | + changeEvent = 'change', |
19 | + emitData?: Ref<any[]> | ||
10 | ) { | 20 | ) { |
11 | const instance = getCurrentInstance(); | 21 | const instance = getCurrentInstance(); |
12 | const emit = instance?.emit; | 22 | const emit = instance?.emit; |
@@ -33,7 +43,9 @@ export function useRuleFormItem<T extends Recordable>( | @@ -33,7 +43,9 @@ export function useRuleFormItem<T extends Recordable>( | ||
33 | if (isEqual(value, defaultState.value)) return; | 43 | if (isEqual(value, defaultState.value)) return; |
34 | 44 | ||
35 | innerState.value = value as T[keyof T]; | 45 | innerState.value = value as T[keyof T]; |
36 | - emit?.(changeEvent, value); | 46 | + nextTick(() => { |
47 | + emit?.(changeEvent, value, ...(toRaw(unref(emitData)) || [])); | ||
48 | + }); | ||
37 | }, | 49 | }, |
38 | }); | 50 | }); |
39 | 51 |