@varlet/use 2.9.3-alpha.1679397366672 → 2.9.3-alpha.1679420700979

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './useClickOutside.js';
3
3
  export * from './useMounted.js';
4
4
  export * from './useParent.js';
5
5
  export * from './useChildren.js';
6
+ export * from './useVModel.js';
package/lib/index.js CHANGED
@@ -3,3 +3,4 @@ export * from './useClickOutside.js';
3
3
  export * from './useMounted.js';
4
4
  export * from './useParent.js';
5
5
  export * from './useChildren.js';
6
+ export * from './useVModel.js';
@@ -0,0 +1,7 @@
1
+ export interface UseVModelOptions<P, K extends keyof P> {
2
+ passive?: boolean;
3
+ eventName?: string;
4
+ defaultValue?: P[K];
5
+ emit?: (event: string, value: P[K]) => void;
6
+ }
7
+ export declare function useVModel<P extends Record<string, any>, K extends keyof P>(props: P, key: K, options?: UseVModelOptions<P, K>): import("vue").WritableComputedRef<P[K]> | ([P[K]] extends [import("vue").Ref<any>] ? P[K] : import("vue").Ref<import("vue").UnwrapRef<P[K]>>);
@@ -0,0 +1,25 @@
1
+ import { computed, getCurrentInstance, ref, watch } from 'vue';
2
+ export function useVModel(props, key, options = {}) {
3
+ const vm = getCurrentInstance();
4
+ const { passive = true, eventName, defaultValue, emit = vm === null || vm === void 0 ? void 0 : vm.emit } = options;
5
+ const event = eventName !== null && eventName !== void 0 ? eventName : `update:${key.toString()}`;
6
+ const getValue = () => (props[key] != null ? props[key] : defaultValue);
7
+ if (!passive) {
8
+ return computed({
9
+ get() {
10
+ return getValue();
11
+ },
12
+ set(value) {
13
+ emit(event, value);
14
+ },
15
+ });
16
+ }
17
+ const proxy = ref(getValue());
18
+ watch(() => props[key], () => {
19
+ proxy.value = getValue();
20
+ });
21
+ watch(() => proxy.value, (newValue) => {
22
+ emit(event, newValue);
23
+ });
24
+ return proxy;
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/use",
3
- "version": "2.9.3-alpha.1679397366672",
3
+ "version": "2.9.3-alpha.1679420700979",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",
@@ -24,7 +24,7 @@
24
24
  "url": "https://github.com/varletjs/varlet/issues"
25
25
  },
26
26
  "dependencies": {
27
- "@varlet/shared": "2.9.3-alpha.1679397366672"
27
+ "@varlet/shared": "2.9.3-alpha.1679420700979"
28
28
  },
29
29
  "devDependencies": {
30
30
  "typescript": "^4.4.4",