Skip to content

Commit

Permalink
fix(api-select): ensure that the onchange function parameters are cor…
Browse files Browse the repository at this point in the history
…rect
  • Loading branch information
anncwb committed Jun 24, 2021
1 parent a2a75a0 commit fa64fc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/components/Form/src/components/ApiSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Select
@dropdownVisibleChange="handleFetch"
v-bind="attrs"
@change="handleChange"
:options="getOptions"
v-model:value="state"
>
Expand Down Expand Up @@ -67,11 +68,12 @@
const options = ref<OptionsItem[]>([]);
const loading = ref(false);
const isFirstLoad = ref(true);
const emitData = ref<any[]>([]);
const attrs = useAttrs();
const { t } = useI18n();
// Embedded in the form, just use the hook binding to perform form verification
const [state] = useRuleFormItem(props);
const [state] = useRuleFormItem(props, 'value', 'change', emitData);
const getOptions = computed(() => {
const { labelField, valueField, numberToString } = props;
Expand Down Expand Up @@ -135,7 +137,11 @@
emit('options-change', unref(options));
}
return { state, attrs, getOptions, loading, t, handleFetch };
function handleChange(_, ...args) {
emitData.value = args;
}
return { state, attrs, getOptions, loading, t, handleFetch, handleChange };
},
});
</script>
20 changes: 16 additions & 4 deletions src/hooks/component/useFormItem.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import type { UnwrapRef } from 'vue';
import { reactive, readonly, computed, getCurrentInstance, watchEffect } from 'vue';
import type { UnwrapRef, Ref } from 'vue';
import {
reactive,
readonly,
computed,
getCurrentInstance,
watchEffect,
unref,
nextTick,
toRaw,
} from 'vue';

import { isEqual } from 'lodash-es';

export function useRuleFormItem<T extends Recordable>(
props: T,
key: keyof T = 'value',
changeEvent = 'change'
changeEvent = 'change',
emitData?: Ref<any[]>
) {
const instance = getCurrentInstance();
const emit = instance?.emit;
Expand All @@ -33,7 +43,9 @@ export function useRuleFormItem<T extends Recordable>(
if (isEqual(value, defaultState.value)) return;

innerState.value = value as T[keyof T];
emit?.(changeEvent, value);
nextTick(() => {
emit?.(changeEvent, value, ...(toRaw(unref(emitData)) || []));
});
},
});

Expand Down

0 comments on commit fa64fc8

Please sign in to comment.