feat [front]: v-model utility
This commit is contained in:
parent
4767becf7f
commit
21d4a4a25b
1 changed files with 26 additions and 0 deletions
26
front/composables/modelWrapper.ts
Normal file
26
front/composables/modelWrapper.ts
Normal 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))
|
||||
},
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue