mirror of
https://github.com/ecomfe/vue-echarts.git
synced 2025-10-27 19:13:59 +08:00
feat: support native listener in Vue 3
This commit is contained in:
24
README.md
24
README.md
@ -464,6 +464,30 @@ Vue-ECharts support the following events:
|
|||||||
|
|
||||||
See supported events [here →](https://echarts.apache.org/en/api.html#events)
|
See supported events [here →](https://echarts.apache.org/en/api.html#events)
|
||||||
|
|
||||||
|
#### Native DOM Events
|
||||||
|
|
||||||
|
<details open>
|
||||||
|
<summary>Vue 3</summary>
|
||||||
|
|
||||||
|
```vue
|
||||||
|
<template>
|
||||||
|
<v-chart @native:click="handleClick" />
|
||||||
|
</template>
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details open>
|
||||||
|
<summary>Vue 2</summary>
|
||||||
|
|
||||||
|
```vue
|
||||||
|
<template>
|
||||||
|
<v-chart @click.native="handleClick" />
|
||||||
|
</template>
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## CSP: `style-src` or `style-src-elem`
|
## CSP: `style-src` or `style-src-elem`
|
||||||
|
|
||||||
If you are applying a CSP to prevent inline `<style>` injection, you need to use files from `dist/csp` directory and include `dist/csp/style.css` into your app manually.
|
If you are applying a CSP to prevent inline `<style>` injection, you need to use files from `dist/csp` directory and include `dist/csp/style.css` into your app manually.
|
||||||
|
|||||||
@ -37,7 +37,7 @@ import {
|
|||||||
useLoading,
|
useLoading,
|
||||||
loadingProps
|
loadingProps
|
||||||
} from "./composables";
|
} from "./composables";
|
||||||
import { omitOn, unwrapInjected } from "./utils";
|
import { isOn, omitOn, unwrapInjected } from "./utils";
|
||||||
import { register, TAG_NAME, type EChartsElement } from "./wc";
|
import { register, TAG_NAME, type EChartsElement } from "./wc";
|
||||||
import "./style.css";
|
import "./style.css";
|
||||||
|
|
||||||
@ -95,6 +95,7 @@ export default defineComponent({
|
|||||||
() => props.updateOptions || unwrapInjected(defaultUpdateOptions, {})
|
() => props.updateOptions || unwrapInjected(defaultUpdateOptions, {})
|
||||||
);
|
);
|
||||||
const nonEventAttrs = computed(() => omitOn(attrs));
|
const nonEventAttrs = computed(() => omitOn(attrs));
|
||||||
|
const nativeEventAttrs: Record<string, unknown> = {};
|
||||||
|
|
||||||
// @ts-expect-error listeners for Vue 2 compatibility
|
// @ts-expect-error listeners for Vue 2 compatibility
|
||||||
const listeners = getCurrentInstance().proxy.$listeners;
|
const listeners = getCurrentInstance().proxy.$listeners;
|
||||||
@ -119,12 +120,22 @@ export default defineComponent({
|
|||||||
realListeners = {};
|
realListeners = {};
|
||||||
|
|
||||||
Object.keys(attrs)
|
Object.keys(attrs)
|
||||||
.filter(key => key.indexOf("on") === 0 && key.length > 2)
|
.filter(key => isOn(key))
|
||||||
.forEach(key => {
|
.forEach(key => {
|
||||||
// onClick -> c + lick
|
// onClick -> c + lick
|
||||||
// onZr:click -> z + r:click
|
// onZr:click -> z + r:click
|
||||||
let event = key.charAt(2).toLowerCase() + key.slice(3);
|
let event = key.charAt(2).toLowerCase() + key.slice(3);
|
||||||
|
|
||||||
|
// Collect native events
|
||||||
|
if (event.startsWith("native:")) {
|
||||||
|
// native:click -> onClick
|
||||||
|
const nativeKey =
|
||||||
|
"on" + event.charAt(7).toUpperCase() + event.slice(8);
|
||||||
|
|
||||||
|
nativeEventAttrs[nativeKey] = attrs[key];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// clickOnce -> ~click
|
// clickOnce -> ~click
|
||||||
// zr:clickOnce -> ~zr:click
|
// zr:clickOnce -> ~zr:click
|
||||||
if (event.substring(event.length - 4) === "Once") {
|
if (event.substring(event.length - 4) === "Once") {
|
||||||
@ -296,6 +307,7 @@ export default defineComponent({
|
|||||||
inner,
|
inner,
|
||||||
setOption,
|
setOption,
|
||||||
nonEventAttrs,
|
nonEventAttrs,
|
||||||
|
nativeEventAttrs,
|
||||||
...publicApi
|
...publicApi
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@ -303,7 +315,9 @@ export default defineComponent({
|
|||||||
// Vue 3 and Vue 2 have different vnode props format:
|
// Vue 3 and Vue 2 have different vnode props format:
|
||||||
// See https://v3-migration.vuejs.org/breaking-changes/render-function-api.html#vnode-props-format
|
// See https://v3-migration.vuejs.org/breaking-changes/render-function-api.html#vnode-props-format
|
||||||
const attrs = (
|
const attrs = (
|
||||||
Vue2 ? { attrs: this.nonEventAttrs } : { ...this.nonEventAttrs }
|
Vue2
|
||||||
|
? { attrs: this.nonEventAttrs }
|
||||||
|
: { ...this.nonEventAttrs, ...this.nativeEventAttrs }
|
||||||
) as any;
|
) as any;
|
||||||
attrs.ref = "root";
|
attrs.ref = "root";
|
||||||
attrs.class = attrs.class ? ["echarts"].concat(attrs.class) : "echarts";
|
attrs.class = attrs.class ? ["echarts"].concat(attrs.class) : "echarts";
|
||||||
|
|||||||
Reference in New Issue
Block a user