cineclub-site/front/composables/modelWrapper.ts

27 lines
616 B
TypeScript
Raw Normal View History

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") {
valueToSet = (value: any[]) => [...value]
} else valueToSet = (value) => value
return computed<Type>({
get() {
return vm.$props[propName]
},
set(value) {
vm.$emit(`update:${propName}`, valueToSet(value))
},
})
}