-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revert(defineModel): reuse
useVModel
composable for generics where …
…the type is `T | T[]` (#998)
- Loading branch information
Showing
9 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ref, watch, nextTick, getCurrentInstance, type Ref } from "vue"; | ||
|
||
/** | ||
* Adaption of {@link https://vueuse.org/core/useVModel/} options. | ||
*/ | ||
export interface PropBindingOptions { | ||
/** | ||
* Attempting to check for changes of properties in a deeply nested object or array. | ||
* Apply only when `passive` option is set to `true` | ||
* | ||
* @default false | ||
*/ | ||
deep?: boolean; | ||
} | ||
|
||
/** | ||
* @deprecated can be replaced by `defineModel` with vue 3.5 | ||
* Use two-way model binding in script setup syntax. | ||
* Adaption of {@link https://vueuse.org/core/useVModel/}. | ||
* @param name Property name | ||
* @param options Extended usage options | ||
* @returns Ref<T> | ||
*/ | ||
export function useVModel<T>( | ||
name: string = "modelValue", | ||
options?: PropBindingOptions, | ||
): Ref<T> { | ||
// getting a hold of the internal instance in setup() | ||
const vm = getCurrentInstance(); | ||
if (!vm) | ||
throw new Error( | ||
"useVModel must be called within a component setup function.", | ||
); | ||
|
||
/** reactive two-way binding model */ | ||
const proxy = ref<T>(vm.proxy.$props[name!]) as Ref<T>; | ||
|
||
let isUpdating = false; | ||
|
||
watch( | ||
() => vm.proxy.$props[name!], | ||
(value) => { | ||
if (!isUpdating) { | ||
isUpdating = true; | ||
proxy.value = value; | ||
nextTick(() => (isUpdating = false)); | ||
} | ||
}, | ||
); | ||
|
||
watch( | ||
proxy, | ||
(value) => { | ||
if ( | ||
(!isUpdating && value !== vm.proxy.$props[name!]) || | ||
options?.deep | ||
) | ||
vm.emit(`update:${name}`, value); | ||
}, | ||
{ deep: options?.deep }, | ||
); | ||
|
||
return proxy; | ||
} |