react-use-echarts 0.0.4 → 0.0.6

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/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  [![NPM version](https://img.shields.io/npm/v/react-use-echarts.svg)](https://www.npmjs.com/package/react-use-echarts)
4
4
  [![NPM downloads](https://img.shields.io/npm/dm/react-use-echarts.svg)](https://www.npmjs.com/package/react-use-echarts)
5
+ [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/chensid/react-use-echarts/npm-publish.yml)](https://github.com/chensid/react-use-echarts/actions/workflows/npm-publish.yml)
6
+ [![GitHub issues](https://img.shields.io/github/issues/chensid/react-use-echarts)](https://github.com/chensid/react-use-echarts/issues)
7
+ [![GitHub pull requests](https://img.shields.io/github/issues-pr/chensid/react-use-echarts)](https://github.com/chensid/react-use-echarts/pulls)
5
8
  [![GitHub license](https://img.shields.io/github/license/chensid/react-use-echarts.svg)](https://github.com/chensid/react-use-echarts/blob/main/LICENSE.txt)
6
9
 
7
10
  A powerful React hooks library for Apache ECharts, making it easy to use ECharts in your React applications with minimal boilerplate.
@@ -31,10 +34,10 @@ pnpm add react-use-echarts echarts
31
34
  ## 🔨 Usage
32
35
 
33
36
  ```tsx
34
- import { useEcharts } from 'react-use-echarts';
37
+ import { useEcharts, UseEchartsOptions } from 'react-use-echarts';
35
38
 
36
39
  function MyChart() {
37
- const options = {
40
+ const options: UseEchartsOptions['option'] = {
38
41
  xAxis: {
39
42
  type: 'category',
40
43
  data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
@@ -64,7 +67,7 @@ The main hook for using ECharts in React components.
64
67
 
65
68
  ```tsx
66
69
  const { chartRef, setOption, getInstance } = useEcharts({
67
- option: EChartsOption; // ECharts options configuration (required)
70
+ option: UseEchartsOptions['option']; // ECharts options configuration (required)
68
71
  theme?: string | object; // ECharts theme name or configuration
69
72
  notMerge?: boolean; // Whether to not merge with previous options
70
73
  lazyUpdate?: boolean; // Whether to update chart lazily
package/dist/index.d.ts CHANGED
@@ -55,36 +55,16 @@ export declare interface EChartsEvents {
55
55
  export declare type Theme = string | object | null;
56
56
 
57
57
  /**
58
- * A React hook for using Apache ECharts
59
- * React Hook 用于使用 Apache ECharts
60
- *
61
- * @param options - Configuration options for the chart
62
- * @param options.option - Chart configuration options
63
- * @param options.theme - Chart theme
64
- * @param options.notMerge - Whether to merge options or not
65
- * @param options.lazyUpdate - Whether to update chart lazily or not
66
- * @param options.showLoading - Whether to show loading animation or not
67
- * @param options.loadingOption - Loading animation configuration options
68
- * @param options.onEvents - Event handlers for the chart
69
- * @returns Object containing chart reference and control functions
70
- *
71
- * @example
72
- * ```typescript
73
- * const { chartRef, setOption, getInstance } = useEcharts({
74
- * option: {
75
- * xAxis: { type: 'category', data: ['A', 'B', 'C'] },
76
- * yAxis: { type: 'value' },
77
- * series: [{ data: [120, 200, 150], type: 'line' }]
78
- * },
79
- * onEvents: {
80
- * 'click': {
81
- * handler: (params) => console.log('clicked', params)
82
- * }
83
- * }
84
- * });
85
- *
86
- * return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
87
- * ```
58
+ * React hook for Apache ECharts integration
59
+ * @param options Configuration object
60
+ * @param options.option Chart configuration
61
+ * @param options.theme Chart theme name
62
+ * @param options.notMerge Skip merging with previous options
63
+ * @param options.lazyUpdate Enable lazy update mode
64
+ * @param options.showLoading Display loading animation
65
+ * @param options.loadingOption Loading animation config
66
+ * @param options.onEvents Event handlers map
67
+ * @returns Chart control methods and ref
88
68
  */
89
69
  export declare const useEcharts: ({ option, theme, notMerge, lazyUpdate, showLoading, loadingOption, onEvents, }: UseEchartsOptions) => UseEchartsReturn;
90
70
 
package/dist/index.es.js CHANGED
@@ -1,84 +1,72 @@
1
- import { useRef as b, useCallback as m, useEffect as i } from "react";
2
- import * as R from "echarts";
3
- const x = ({
4
- /**
5
- * Chart configuration options
6
- * 图表配置选项
7
- */
1
+ import { useRef as p, useCallback as z, useEffect as w } from "react";
2
+ import * as C from "echarts";
3
+ const R = ({
4
+ /** Chart configuration */
8
5
  option: f,
9
- /**
10
- * Chart theme
11
- * 图表主题
12
- */
13
- theme: o,
14
- /**
15
- * Whether to merge options or not
16
- * 是否合并选项
17
- * @default false
18
- */
19
- notMerge: a = !1,
20
- /**
21
- * Whether to update chart lazily or not
22
- * 是否懒更新图表
23
- * @default false
24
- */
6
+ /** Theme name */
7
+ theme: a,
8
+ /** Skip merging with previous options */
9
+ notMerge: m = !1,
10
+ /** Enable lazy update mode */
25
11
  lazyUpdate: l = !1,
26
- /**
27
- * Whether to show loading animation or not
28
- * 是否显示加载动画
29
- * @default false
30
- */
31
- showLoading: d = !1,
32
- /**
33
- * Loading animation configuration options
34
- * 加载动画配置选项
35
- */
36
- loadingOption: h,
37
- /**
38
- * Event handlers for the chart
39
- * 图表事件处理函数
40
- */
41
- onEvents: c
12
+ /** Display loading animation */
13
+ showLoading: h = !1,
14
+ /** Loading animation config */
15
+ loadingOption: d,
16
+ /** Event handlers map */
17
+ onEvents: s
42
18
  }) => {
43
- const u = b(null), r = b(), z = m(() => r.current, []), L = m(
44
- (e, t) => {
45
- var n;
46
- (n = r.current) == null || n.setOption(e, t);
19
+ const c = p(null), t = p(), O = () => t.current, u = z(() => {
20
+ if (c.current && !t.current) {
21
+ const e = C.init(c.current, a);
22
+ return t.current = e, s && Object.entries(s).forEach(
23
+ ([r, { handler: n, query: i, context: o }]) => {
24
+ i ? e.on(r, i, n, o) : e.on(r, n, o);
25
+ }
26
+ ), h ? e.showLoading(d) : e.hideLoading(), e.setOption(f, { notMerge: m, lazyUpdate: l }), e;
27
+ }
28
+ return t.current;
29
+ }, [
30
+ f,
31
+ a,
32
+ m,
33
+ l,
34
+ h,
35
+ d,
36
+ s
37
+ ]), b = z(
38
+ (e, r) => {
39
+ requestAnimationFrame(() => {
40
+ const n = t.current || u();
41
+ n && n.setOption(e, r);
42
+ });
47
43
  },
48
- []
44
+ [u]
49
45
  );
50
- return i(() => {
51
- if (u.current)
52
- return r.current || (r.current = R.init(u.current, o)), c && r.current && Object.entries(c).forEach(([e, t]) => {
53
- var w, O;
54
- const { handler: n, query: s, context: p } = t;
55
- s ? (w = r.current) == null || w.on(e, s, n, p) : (O = r.current) == null || O.on(e, n, p);
56
- }), () => {
57
- r.current && (c && Object.entries(c).forEach(([e, t]) => {
58
- var s;
59
- const { handler: n } = t;
60
- (s = r.current) == null || s.off(e, n);
61
- }), r.current.dispose(), r.current = void 0);
62
- };
63
- }, [o, c]), i(() => {
64
- r.current && (d ? r.current.showLoading(h) : r.current.hideLoading(), r.current.setOption(f, {
65
- notMerge: a,
66
- lazyUpdate: l
67
- }));
68
- }, [f, a, l, d, h]), i(() => {
69
- const e = () => {
70
- var t;
71
- (t = r.current) == null || t.resize();
46
+ return w(() => {
47
+ const e = t.current;
48
+ if (!e) return;
49
+ const r = () => {
50
+ const i = setTimeout(() => {
51
+ e == null || e.resize();
52
+ }, 250);
53
+ return () => clearTimeout(i);
54
+ }, n = r();
55
+ return window.addEventListener("resize", r), () => {
56
+ s && Object.entries(s).forEach(([i, { handler: o }]) => {
57
+ e.off(i, o);
58
+ }), window.removeEventListener("resize", r), e.dispose(), t.current = void 0, n();
72
59
  };
73
- return window.addEventListener("resize", e), () => {
74
- window.removeEventListener("resize", e);
75
- };
76
- }, []), {
77
- chartRef: u,
78
- setOption: L,
79
- getInstance: z
60
+ }, [s]), w(() => {
61
+ c.current && requestAnimationFrame(() => {
62
+ u();
63
+ });
64
+ }, [c, u]), {
65
+ chartRef: c,
66
+ setOption: b,
67
+ getInstance: O
80
68
  };
81
69
  };
82
70
  export {
83
- x as useEcharts
71
+ R as useEcharts
84
72
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(n,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("react"),require("echarts")):typeof define=="function"&&define.amd?define(["exports","react","echarts"],t):(n=typeof globalThis<"u"?globalThis:n||self,t(n["react-use-echarts"]={},n.React,n.echarts))})(this,function(n,t,y){"use strict";function m(c){const f=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(c){for(const u in c)if(u!=="default"){const o=Object.getOwnPropertyDescriptor(c,u);Object.defineProperty(f,u,o.get?o:{enumerable:!0,get:()=>c[u]})}}return f.default=c,Object.freeze(f)}const g=m(y),w=({option:c,theme:f,notMerge:u=!1,lazyUpdate:o=!1,showLoading:h=!1,loadingOption:p,onEvents:a})=>{const l=t.useRef(null),e=t.useRef(),E=t.useCallback(()=>e.current,[]),R=t.useCallback((r,s)=>{var i;(i=e.current)==null||i.setOption(r,s)},[]);return t.useEffect(()=>{if(l.current)return e.current||(e.current=g.init(l.current,f)),a&&e.current&&Object.entries(a).forEach(([r,s])=>{var O,j;const{handler:i,query:d,context:b}=s;d?(O=e.current)==null||O.on(r,d,i,b):(j=e.current)==null||j.on(r,i,b)}),()=>{e.current&&(a&&Object.entries(a).forEach(([r,s])=>{var d;const{handler:i}=s;(d=e.current)==null||d.off(r,i)}),e.current.dispose(),e.current=void 0)}},[f,a]),t.useEffect(()=>{e.current&&(h?e.current.showLoading(p):e.current.hideLoading(),e.current.setOption(c,{notMerge:u,lazyUpdate:o}))},[c,u,o,h,p]),t.useEffect(()=>{const r=()=>{var s;(s=e.current)==null||s.resize()};return window.addEventListener("resize",r),()=>{window.removeEventListener("resize",r)}},[]),{chartRef:l,setOption:R,getInstance:E}};n.useEcharts=w,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
1
+ (function(r,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("react"),require("echarts")):typeof define=="function"&&define.amd?define(["exports","react","echarts"],t):(r=typeof globalThis<"u"?globalThis:r||self,t(r["react-use-echarts"]={},r.React,r.echarts))})(this,function(r,t,O){"use strict";function j(n){const f=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const i in n)if(i!=="default"){const d=Object.getOwnPropertyDescriptor(n,i);Object.defineProperty(f,i,d.get?d:{enumerable:!0,get:()=>n[i]})}}return f.default=n,Object.freeze(f)}const T=j(O),z=({option:n,theme:f,notMerge:i=!1,lazyUpdate:d=!1,showLoading:m=!1,loadingOption:b,onEvents:o})=>{const l=t.useRef(null),c=t.useRef(),g=()=>c.current,h=t.useCallback(()=>{if(l.current&&!c.current){const e=T.init(l.current,f);return c.current=e,o&&Object.entries(o).forEach(([s,{handler:u,query:a,context:p}])=>{a?e.on(s,a,u,p):e.on(s,u,p)}),m?e.showLoading(b):e.hideLoading(),e.setOption(n,{notMerge:i,lazyUpdate:d}),e}return c.current},[n,f,i,d,m,b,o]),w=t.useCallback((e,s)=>{requestAnimationFrame(()=>{const u=c.current||h();u&&u.setOption(e,s)})},[h]);return t.useEffect(()=>{const e=c.current;if(!e)return;const s=()=>{const a=setTimeout(()=>{e==null||e.resize()},250);return()=>clearTimeout(a)},u=s();return window.addEventListener("resize",s),()=>{o&&Object.entries(o).forEach(([a,{handler:p}])=>{e.off(a,p)}),window.removeEventListener("resize",s),e.dispose(),c.current=void 0,u()}},[o]),t.useEffect(()=>{l.current&&requestAnimationFrame(()=>{h()})},[l,h]),{chartRef:l,setOption:w,getInstance:g}};r.useEcharts=z,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-use-echarts",
3
3
  "private": false,
4
- "version": "0.0.4",
4
+ "version": "0.0.6",
5
5
  "description": "A powerful React hooks library for Apache ECharts",
6
6
  "keywords": [
7
7
  "react",
@@ -41,22 +41,26 @@
41
41
  "dist"
42
42
  ],
43
43
  "devDependencies": {
44
- "@eslint/js": "^9.15.0",
45
- "@types/node": "^22.9.0",
44
+ "@eslint/js": "^9.16.0",
45
+ "@testing-library/react": "^16.0.1",
46
+ "@types/node": "^22.10.1",
46
47
  "@types/react": "^18.3.12",
47
48
  "@types/react-dom": "^18.3.1",
48
- "@vitejs/plugin-react-swc": "^3.7.1",
49
+ "@vitejs/plugin-react-swc": "^3.7.2",
50
+ "@vitest/coverage-v8": "2.1.8",
49
51
  "echarts": "^5.5.1",
50
- "eslint": "^9.15.0",
52
+ "eslint": "^9.16.0",
51
53
  "eslint-plugin-react-hooks": "^5.0.0",
52
- "eslint-plugin-react-refresh": "^0.4.14",
53
- "globals": "^15.12.0",
54
+ "eslint-plugin-react-refresh": "^0.4.16",
55
+ "globals": "^15.13.0",
56
+ "jsdom": "^25.0.1",
54
57
  "react": "^18.3.1",
55
58
  "react-dom": "^18.3.1",
56
- "typescript": "~5.6.3",
57
- "typescript-eslint": "^8.15.0",
58
- "vite": "^5.4.11",
59
- "vite-plugin-dts": "^4.3.0"
59
+ "typescript": "~5.7.2",
60
+ "typescript-eslint": "^8.17.0",
61
+ "vite": "^6.0.2",
62
+ "vite-plugin-dts": "^4.3.0",
63
+ "vitest": "^2.1.8"
60
64
  },
61
65
  "peerDependencies": {
62
66
  "echarts": "^5.5.1",
@@ -66,6 +70,8 @@
66
70
  "scripts": {
67
71
  "dev": "vite",
68
72
  "build": "tsc -b && vite build",
73
+ "test": "vitest",
74
+ "coverage": "vitest run --coverage",
69
75
  "lint": "eslint .",
70
76
  "typecheck": "tsc -b"
71
77
  }