You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

6 months ago
import type { Ref } from 'vue';
import { ref, computed, getCurrentInstance, watch } from 'vue';
import { isEqual, isUndefined } from 'lodash-es';
interface UseRuleEvent {
emitData?: Ref<any[]>;
key: string;
changeEvent: string;
}
export function useRuleEvent({ key = 'value', changeEvent = 'change', emitData }: UseRuleEvent) {
const instance = getCurrentInstance();
const props = instance?.props;
const emit = instance?.emit;
const changeValue = ref();
const modelValue = computed({
get() {
return changeValue.value;
},
set(value) {
if (!isEqual(value, changeValue.value)) {
changeValue.value = value;
emit?.('update:value', value);
// nextTick(() => {
// emit?.(changeEvent, value, ...(emitData?.value || []));
// });
}
},
});
watch(
() => props[key],
() => {
if (!isUndefined(props[key])) {
modelValue.value = props[key];
}
},
{
immediate: true,
}
);
return { modelValue };
}