You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
881 B
TypeScript
35 lines
881 B
TypeScript
1 year ago
|
import type { Ref } from 'vue'
|
||
|
|
||
|
function getDefaultValue<T>(propValue: Ref<T>, defaultValue?: T | (() => T)) {
|
||
|
if (propValue.value !== undefined) {
|
||
|
return propValue.value
|
||
|
}
|
||
|
if (defaultValue !== undefined) {
|
||
|
return typeof defaultValue === 'function' ? (defaultValue as any)() : defaultValue
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default function index<T, R = Ref<T>>(
|
||
|
propValue: Ref<T>,
|
||
|
option?: {
|
||
|
defaultValue?: T | (() => T)
|
||
|
onChange?: (value: T) => void
|
||
|
}
|
||
|
): [R, (value: T, ignoreDestroy?: boolean) => void] {
|
||
|
const innerValue = ref(getDefaultValue(propValue, option?.defaultValue))
|
||
|
|
||
|
watch(
|
||
|
() => propValue.value,
|
||
|
() => (innerValue.value = propValue.value)
|
||
|
)
|
||
|
|
||
|
const triggerChange = (newValue: T) => {
|
||
|
innerValue.value = newValue
|
||
|
if (option?.onChange) {
|
||
|
option.onChange(newValue)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return [innerValue as unknown as R, triggerChange]
|
||
|
}
|