mirror of
https://github.com/ecomfe/vue-echarts.git
synced 2025-10-28 03:25:02 +08:00
feat: make it possible to dynamically change provide values for Vue 2
This commit is contained in:
@ -2,7 +2,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import {
|
||||
defineComponent,
|
||||
unref,
|
||||
shallowRef,
|
||||
toRefs,
|
||||
watch,
|
||||
@ -37,7 +36,7 @@ import {
|
||||
useLoading,
|
||||
loadingProps
|
||||
} from "./composables";
|
||||
import { omitOn } from "./utils";
|
||||
import { omitOn, unwrapInjected } from "./utils";
|
||||
import "./style.css";
|
||||
|
||||
const TAG_NAME = "x-vue-echarts";
|
||||
@ -81,12 +80,14 @@ export default defineComponent({
|
||||
const realOption = computed(
|
||||
() => manualOption.value || props.option || null
|
||||
);
|
||||
const realTheme = computed(() => props.theme || unref(defaultTheme) || {});
|
||||
const realTheme = computed(
|
||||
() => props.theme || unwrapInjected(defaultTheme, {})
|
||||
);
|
||||
const realInitOptions = computed(
|
||||
() => props.initOptions || unref(defaultInitOptions) || {}
|
||||
() => props.initOptions || unwrapInjected(defaultInitOptions, {})
|
||||
);
|
||||
const realUpdateOptions = computed(
|
||||
() => props.updateOptions || unref(defaultUpdateOptions) || {}
|
||||
() => props.updateOptions || unwrapInjected(defaultUpdateOptions, {})
|
||||
);
|
||||
const nonEventAttrs = computed(() => omitOn(attrs));
|
||||
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
import { unwrapInjected } from "src/utils";
|
||||
import {
|
||||
inject,
|
||||
unref,
|
||||
computed,
|
||||
Ref,
|
||||
watchEffect,
|
||||
InjectionKey
|
||||
type Ref,
|
||||
type InjectionKey
|
||||
} from "vue-demi";
|
||||
import { EChartsType } from "../types";
|
||||
|
||||
@ -22,7 +23,7 @@ export function useLoading(
|
||||
): void {
|
||||
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
|
||||
const realLoadingOptions = computed(() => ({
|
||||
...unref(defaultLoadingOptions),
|
||||
...unwrapInjected(defaultLoadingOptions, {}),
|
||||
...loadingOptions?.value
|
||||
}));
|
||||
|
||||
|
||||
4
src/index.vue2.d.ts
vendored
4
src/index.vue2.d.ts
vendored
@ -1,6 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
import { Ref, DefineComponent } from "vue-demi";
|
||||
import { Option, InitOptions, UpdateOptions, EChartsType } from "./types";
|
||||
import type { Ref, DefineComponent } from "vue-demi";
|
||||
import type { Option, InitOptions, UpdateOptions, EChartsType } from "./types";
|
||||
|
||||
declare const LOADING_OPTIONS_KEY = "ecLoadingOptions";
|
||||
declare const THEME_KEY = "ecTheme";
|
||||
|
||||
20
src/types.ts
20
src/types.ts
@ -1,21 +1,21 @@
|
||||
import { init, SetOptionOpts } from "echarts/core";
|
||||
import { Ref } from "vue";
|
||||
import { init, type SetOptionOpts } from "echarts/core";
|
||||
import type { Ref } from "vue";
|
||||
|
||||
export type Injection<T> = T | null | Ref<T | null> | { value: T | null };
|
||||
|
||||
type InitType = typeof init;
|
||||
export type InitParameters = Parameters<InitType>;
|
||||
export type Theme = NonNullable<InitParameters[1]>;
|
||||
export type ThemeInjection = Theme | null | Ref<Theme | null>;
|
||||
export type ThemeInjection = Injection<Theme>;
|
||||
export type InitOptions = NonNullable<InitParameters[2]>;
|
||||
export type InitOptionsInjection = InitOptions | null | Ref<InitOptions | null>;
|
||||
|
||||
export type InitOptionsInjection = Injection<InitOptions>;
|
||||
|
||||
export type UpdateOptions = SetOptionOpts;
|
||||
export type UpdateOptionsInjection = Injection<UpdateOptions>;
|
||||
|
||||
export type EChartsType = ReturnType<InitType>;
|
||||
type ZRenderType = ReturnType<EChartsType["getZr"]>;
|
||||
export type EventTarget = EChartsType | ZRenderType;
|
||||
type SetOptionType = EChartsType["setOption"];
|
||||
export type Option = Parameters<SetOptionType>[0];
|
||||
|
||||
export type UpdateOptions = SetOptionOpts;
|
||||
export type UpdateOptionsInjection =
|
||||
| UpdateOptions
|
||||
| null
|
||||
| Ref<UpdateOptions | null>;
|
||||
|
||||
16
src/utils.ts
16
src/utils.ts
@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user