Files
uniapp-im-shop/uni_modules/lime-shared/useVModel/index.ts
2026-01-16 00:12:33 +08:00

38 lines
871 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @ts-nocheck
import {ref, watch, type Ref, ComputedRef } from '../vue'
type UseVModelOptions<T> = {
value: ComputedRef<T> | (() => T)
// 默认值
defaultValue: T
// 变化回调
onChange?: (value: T) => void
// 格式化函数用于get时格式化返回值
onFormat?: (value: T) => T
// 是否深度监听
deep?: boolean
// 是否立即触发监听
immediate?: boolean
// 事件名称
eventName?: string
// 属性名称
propName?: string
}
export function useVModel<T>(options: UseVModelOptions<T>):Ref<T> {
const { value, defaultValue, onChange, onFormat } = options
const innerValue = ref<T>(value.value || defaultValue);
watch(value, (newVal:T) => {
innerValue.value = onFormat?.(newVal) ?? newVal
},
{ immediate: true }
)
watch(
innerValue,
(newVal: T) => {
onChange?.(newVal)
}
)
return innerValue
}