feat [front]: v-model utility

This commit is contained in:
Alice 2022-04-09 02:49:16 +02:00
parent 4767becf7f
commit 21d4a4a25b

View file

@ -0,0 +1,26 @@
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))
},
})
}