2022-04-09 02:49:16 +02:00
|
|
|
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") {
|
2022-07-13 00:39:33 +02:00
|
|
|
valueToSet = (value: unknown[]) => [...value]
|
2022-04-09 02:49:16 +02:00
|
|
|
} else valueToSet = (value) => value
|
|
|
|
|
|
|
|
return computed<Type>({
|
|
|
|
get() {
|
|
|
|
return vm.$props[propName]
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
vm.$emit(`update:${propName}`, valueToSet(value))
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|