27 lines
616 B
TypeScript
27 lines
616 B
TypeScript
|
import { computed, getCurrentInstance } from "vue"
|
||
|
|
||
|
export function useModel<Type>(
|
||
|
propName = "modelValue",
|
||
|
options: { type?: "object" | "array" } = {}
|
||
|
) {
|
||
|
const vm = getCurrentInstance().proxy
|
||
|
|
||
|
let valueToSet
|
||
|
if (options.type === "object") {
|
||
|
valueToSet = (value: object) => {
|
||
|
return { ...value }
|
||
|
}
|
||
|
} else if (options.type === "array") {
|
||
|
valueToSet = (value: any[]) => [...value]
|
||
|
} else valueToSet = (value) => value
|
||
|
|
||
|
return computed<Type>({
|
||
|
get() {
|
||
|
return vm.$props[propName]
|
||
|
},
|
||
|
set(value) {
|
||
|
vm.$emit(`update:${propName}`, valueToSet(value))
|
||
|
},
|
||
|
})
|
||
|
}
|