feat: make it possible to dynamically change provide values for Vue 2

This commit is contained in:
Justineo
2022-12-17 01:23:29 +08:00
parent 9491a904a0
commit ec124f4bf7
10 changed files with 106 additions and 42 deletions

View File

@ -1,3 +1,6 @@
import { unref } from "vue-demi";
import type { Injection } from "./types";
type Attrs = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
@ -18,3 +21,16 @@ export function omitOn(attrs: Attrs): Attrs {
return result;
}
export function unwrapInjected<T, V>(
injection: Injection<T>,
defaultValue: V
): T | V {
const value = unref(injection);
if (value && typeof value === "object" && "value" in value) {
return value.value || defaultValue;
}
return value || defaultValue;
}