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 | 2 | <Select |
3 | 3 | @dropdownVisibleChange="handleFetch" |
4 | 4 | v-bind="attrs" |
5 | + @change="handleChange" | |
5 | 6 | :options="getOptions" |
6 | 7 | v-model:value="state" |
7 | 8 | > |
... | ... | @@ -67,11 +68,12 @@ |
67 | 68 | const options = ref<OptionsItem[]>([]); |
68 | 69 | const loading = ref(false); |
69 | 70 | const isFirstLoad = ref(true); |
71 | + const emitData = ref<any[]>([]); | |
70 | 72 | const attrs = useAttrs(); |
71 | 73 | const { t } = useI18n(); |
72 | 74 | |
73 | 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 | 78 | const getOptions = computed(() => { |
77 | 79 | const { labelField, valueField, numberToString } = props; |
... | ... | @@ -135,7 +137,11 @@ |
135 | 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 | 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 | 13 | import { isEqual } from 'lodash-es'; |
5 | 14 | |
6 | 15 | export function useRuleFormItem<T extends Recordable>( |
7 | 16 | props: T, |
8 | 17 | key: keyof T = 'value', |
9 | - changeEvent = 'change' | |
18 | + changeEvent = 'change', | |
19 | + emitData?: Ref<any[]> | |
10 | 20 | ) { |
11 | 21 | const instance = getCurrentInstance(); |
12 | 22 | const emit = instance?.emit; |
... | ... | @@ -33,7 +43,9 @@ export function useRuleFormItem<T extends Recordable>( |
33 | 43 | if (isEqual(value, defaultState.value)) return; |
34 | 44 | |
35 | 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 | ... | ... |