Files
dysync/app/src/utils/useModelValue.ts
T
lijianyou b4e759cf69 init
2025-09-30 15:00:32 +08:00

22 lines
510 B
TypeScript

import { watch, computed, ref } from 'vue';
function useModelValue<T>(value: () => T | undefined, onChange: (val?: T) => void, defaultValue?: T) {
const _value = ref<T>();
_value.value = value() ?? defaultValue;
const sValue = computed({
get() {
return value() ?? _value.value;
},
set(val: T | undefined) {
_value.value = val;
onChange(val);
},
});
watch(value, () => {
_value.value = value();
});
return { value: sValue };
}
export default useModelValue;