nova-admin/src/hooks/useDefault.ts
2024-06-05 09:18:21 +08:00

16 lines
288 B
TypeScript

/**
* Apply default value to a ref.
*/
export function useDefault<T>(defaultValue: T): Ref<T> {
const source: Ref<T | undefined | null> = ref()
return computed({
get() {
return source.value ?? defaultValue
},
set(value) {
source.value = value
},
})
}