Skip to content

Commit ec124f4

Browse files
committed
feat: make it possible to dynamically change provide values for Vue 2
1 parent 9491a90 commit ec124f4

File tree

10 files changed

+106
-42
lines changed

10 files changed

+106
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.3.0
2+
3+
* Injected values can now be wrapped in an object so that they can be reactive in Vue 2.
4+
15
## 6.2.4
26

37
* Fixed that attributes were not outputted onto the chart root element for Vue 2 (#670).

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,16 +399,16 @@ Vue-ECharts provides provide/inject API for `theme`, `init-options`, `update-opt
399399
<summary>Vue 3</summary>
400400

401401
```js
402-
import { INIT_OPTIONS_KEY } from 'vue-echarts'
402+
import { THEME_KEY } from 'vue-echarts'
403403
import { provide } from 'vue'
404404

405405
// composition API
406-
provide(INIT_OPTIONS_KEY, ...)
406+
provide(THEME_KEY, 'dark')
407407

408408
// options API
409409
{
410410
provide: {
411-
[INIT_OPTIONS_KEY]: { ... }
411+
[THEME_KEY]: 'dark'
412412
}
413413
}
414414
```
@@ -419,16 +419,35 @@ provide(INIT_OPTIONS_KEY, ...)
419419
<summary>Vue 2</summary>
420420

421421
```js
422-
import { INIT_OPTIONS_KEY } from 'vue-echarts'
422+
import { THEME_KEY } from 'vue-echarts'
423423

424424
// in component options
425425
{
426426
provide: {
427-
[INIT_OPTIONS_KEY]: { ... }
427+
[THEME_KEY]: 'dark'
428428
}
429429
}
430430
```
431431

432+
> **Note**
433+
>
434+
> You need to provide an object for Vue 2 if you want to change it dynamically.
435+
>
436+
> ```js
437+
> // in component options
438+
> {
439+
> data () {
440+
> return {
441+
> theme: { value: 'dark' }
442+
> }
443+
> },
444+
> provide () {
445+
> return {
446+
> [THEME_KEY]: this.theme
447+
> }
448+
> }
449+
> }
450+
> ```
432451
</details>
433452
434453
### Methods
@@ -464,6 +483,8 @@ You can bind events with Vue's `v-on` directive.
464483
</template>
465484
```
466485
486+
> **Note**
487+
>
467488
> Only the `.once` event modifier is supported as other modifiers are tightly coupled with the DOM event system.
468489
469490
Vue-ECharts support the following events:

README.zh-Hans.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ Vue.component("v-chart", VueECharts);
400400
</template>
401401
```
402402

403+
> **Note**
404+
>
403405
> 仅支持 `.once` 修饰符,因为其它修饰符都与 DOM 事件机制强耦合。
404406
405407
Vue-ECharts 支持如下事件:
@@ -458,16 +460,16 @@ Vue-ECharts 为 `theme`、`init-options`、`update-options` 和 `loading-options
458460
<summary>Vue 3</summary>
459461

460462
```js
461-
import { INIT_OPTIONS_KEY } from 'vue-echarts'
463+
import { THEME_KEY } from 'vue-echarts'
462464
import { provide } from 'vue'
463465

464-
// composition API
465-
provide(INIT_OPTIONS_KEY, ...)
466+
// 组合式 API
467+
provide(THEME_KEY, 'dark')
466468

467-
// options API
469+
// 选项式 API
468470
{
469471
provide: {
470-
[INIT_OPTIONS_KEY]: { ... }
472+
[THEME_KEY]: 'dark'
471473
}
472474
}
473475
```
@@ -478,16 +480,35 @@ provide(INIT_OPTIONS_KEY, ...)
478480
<summary>Vue 2</summary>
479481

480482
```js
481-
import { INIT_OPTIONS_KEY } from 'vue-echarts'
483+
import { THEME_KEY } from 'vue-echarts'
482484

483-
// in component options
485+
// 组件选项中
484486
{
485487
provide: {
486-
[INIT_OPTIONS_KEY]: { ... }
488+
[THEME_KEY]: 'dark'
487489
}
488490
}
489491
```
490492

493+
> **Note**
494+
>
495+
> 在 Vue 2 中,如果你想动态地改变这些选项,那么你需要提供一个对象。
496+
>
497+
> ```js
498+
> // 组件选项中
499+
> {
500+
> data () {
501+
> return {
502+
> theme: { value: 'dark' }
503+
> }
504+
> },
505+
> provide () {
506+
> return {
507+
> [THEME_KEY]: this.theme
508+
> }
509+
> }
510+
> }
511+
> ```
491512
</details>
492513
493514
### 方法

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-echarts",
3-
"version": "6.2.4",
3+
"version": "6.3.0",
44
"description": "Vue.js component for Apache ECharts.",
55
"author": "GU Yiling <[email protected]>",
66
"scripts": {
@@ -62,7 +62,7 @@
6262
"tslib": "^2.4.0",
6363
"typescript": "4.6.4",
6464
"vue": "^3.2.33",
65-
"vue2": "npm:vue@^2.7.4",
65+
"vue2": "npm:vue@^2.7.14",
6666
"webpack": "^5.72.1"
6767
},
6868
"peerDependencies": {

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ECharts.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
import {
44
defineComponent,
5-
unref,
65
shallowRef,
76
toRefs,
87
watch,
@@ -37,7 +36,7 @@ import {
3736
useLoading,
3837
loadingProps
3938
} from "./composables";
40-
import { omitOn } from "./utils";
39+
import { omitOn, unwrapInjected } from "./utils";
4140
import "./style.css";
4241

4342
const TAG_NAME = "x-vue-echarts";
@@ -81,12 +80,14 @@ export default defineComponent({
8180
const realOption = computed(
8281
() => manualOption.value || props.option || null
8382
);
84-
const realTheme = computed(() => props.theme || unref(defaultTheme) || {});
83+
const realTheme = computed(
84+
() => props.theme || unwrapInjected(defaultTheme, {})
85+
);
8586
const realInitOptions = computed(
86-
() => props.initOptions || unref(defaultInitOptions) || {}
87+
() => props.initOptions || unwrapInjected(defaultInitOptions, {})
8788
);
8889
const realUpdateOptions = computed(
89-
() => props.updateOptions || unref(defaultUpdateOptions) || {}
90+
() => props.updateOptions || unwrapInjected(defaultUpdateOptions, {})
9091
);
9192
const nonEventAttrs = computed(() => omitOn(attrs));
9293

src/composables/loading.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { unwrapInjected } from "src/utils";
12
import {
23
inject,
34
unref,
45
computed,
5-
Ref,
66
watchEffect,
7-
InjectionKey
7+
type Ref,
8+
type InjectionKey
89
} from "vue-demi";
910
import { EChartsType } from "../types";
1011

@@ -22,7 +23,7 @@ export function useLoading(
2223
): void {
2324
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
2425
const realLoadingOptions = computed(() => ({
25-
...unref(defaultLoadingOptions),
26+
...unwrapInjected(defaultLoadingOptions, {}),
2627
...loadingOptions?.value
2728
}));
2829

src/index.vue2.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/ban-types */
2-
import { Ref, DefineComponent } from "vue-demi";
3-
import { Option, InitOptions, UpdateOptions, EChartsType } from "./types";
2+
import type { Ref, DefineComponent } from "vue-demi";
3+
import type { Option, InitOptions, UpdateOptions, EChartsType } from "./types";
44

55
declare const LOADING_OPTIONS_KEY = "ecLoadingOptions";
66
declare const THEME_KEY = "ecTheme";

src/types.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { init, SetOptionOpts } from "echarts/core";
2-
import { Ref } from "vue";
1+
import { init, type SetOptionOpts } from "echarts/core";
2+
import type { Ref } from "vue";
3+
4+
export type Injection<T> = T | null | Ref<T | null> | { value: T | null };
35

46
type InitType = typeof init;
57
export type InitParameters = Parameters<InitType>;
68
export type Theme = NonNullable<InitParameters[1]>;
7-
export type ThemeInjection = Theme | null | Ref<Theme | null>;
9+
export type ThemeInjection = Injection<Theme>;
810
export type InitOptions = NonNullable<InitParameters[2]>;
9-
export type InitOptionsInjection = InitOptions | null | Ref<InitOptions | null>;
11+
12+
export type InitOptionsInjection = Injection<InitOptions>;
13+
14+
export type UpdateOptions = SetOptionOpts;
15+
export type UpdateOptionsInjection = Injection<UpdateOptions>;
1016

1117
export type EChartsType = ReturnType<InitType>;
1218
type ZRenderType = ReturnType<EChartsType["getZr"]>;
1319
export type EventTarget = EChartsType | ZRenderType;
1420
type SetOptionType = EChartsType["setOption"];
1521
export type Option = Parameters<SetOptionType>[0];
16-
17-
export type UpdateOptions = SetOptionOpts;
18-
export type UpdateOptionsInjection =
19-
| UpdateOptions
20-
| null
21-
| Ref<UpdateOptions | null>;

src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { unref } from "vue-demi";
2+
import type { Injection } from "./types";
3+
14
type Attrs = {
25
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36
[key: string]: any;
@@ -18,3 +21,16 @@ export function omitOn(attrs: Attrs): Attrs {
1821

1922
return result;
2023
}
24+
25+
export function unwrapInjected<T, V>(
26+
injection: Injection<T>,
27+
defaultValue: V
28+
): T | V {
29+
const value = unref(injection);
30+
31+
if (value && typeof value === "object" && "value" in value) {
32+
return value.value || defaultValue;
33+
}
34+
35+
return value || defaultValue;
36+
}

0 commit comments

Comments
 (0)