Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"no-unused-vars": 0, // @typescript-eslint/no-unused-vars
"no-inner-declarations": 0,
"prettier/prettier": 2,
"@typescript-eslint/no-unused-vars": 1,
"@typescript-eslint/no-unused-vars": [1, { "ignoreRestSiblings": true }],
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-use-before-define": [2, { "functions": false }],
Expand Down
25 changes: 25 additions & 0 deletions __tests__/charts/simple-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,29 @@ describe('chart', () => {
});
});
});

describe('props', () => {
it('default', () => {
let instance;
const div = createDiv();
div.style.display = 'block';
div.style.width = '1200px';
div.style.height = '720px';

const opts = {
width: null,
height: null,
};

const Comp = <ReactECharts ref={(e) => (instance = e)} option={options} opts={opts} data-testid="props-test" />;
render(Comp, div);

expect(div.querySelector('[data-testid="props-test"]')).toBe(instance.ele);

destroy(div);
expect(div.querySelector('*')).toBe(null);

removeDom(div);
});
});
});
9 changes: 5 additions & 4 deletions __tests__/helper/pick-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { pick } from '../../src/helper/pick';

describe('pick', () => {
it('pick', () => {
expect(pick({ a: 1 }, [])).toEqual({});
expect(pick({ a: 1 }, ['b'])).toEqual({});
expect(pick({ a: 1 }, ['a'])).toEqual({ a: 1 });
expect(pick({ a: 1 }, ['a', 'b'])).toEqual({ a: 1 });
const obj: { a: number; b?: number } = { a: 1 };
expect(pick(obj, [])).toEqual({});
expect(pick(obj, ['b'])).toEqual({});
expect(pick(obj, ['a'])).toEqual({ a: 1 });
expect(pick(obj, ['a', 'b'])).toEqual({ a: 1 });
});
});
92 changes: 92 additions & 0 deletions docs/examples/html-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: HTML Properties
order: 10
---

## HTML Properties

Unknown (non-ECharts) props are passed through to the div element.

```tsx
import React from 'react';
import ReactECharts from 'echarts-for-react';

const Page: React.FC = () => {
const option = {
title: {
text: '堆叠区域图'
},
tooltip : {
trigger: 'axis'
},
legend: {
data:['邮件营销','联盟广告','视频广告']
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis : [
{
type : 'category',
boundaryGap : false,
data : ['周一','周二','周三','周四','周五','周六','周日']
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'邮件营销',
type:'line',
stack: '总量',
areaStyle: {normal: {}},
data:[120, 132, 101, 134, 90, 230, 210]
},
{
name:'联盟广告',
type:'line',
stack: '总量',
areaStyle: {normal: {}},
data:[220, 182, 191, 234, 290, 330, 310]
},
{
name:'视频广告',
type:'line',
stack: '总量',
areaStyle: {normal: {}},
data:[150, 232, 201, 154, 190, 330, 410]
}
]
};

const handleDemoButton = () => {
console.log(document.querySelector(['[data-testid="html-props-demo"]']));
window.alert('Open console, see the log detail.')
};

return (
<>
<ReactECharts
option={option}
style={{ height: 400 }}
role="figure"
data-testid="html-props-demo"
/>
<button type="button" onClick={handleDemoButton}>Demo</button>
</>
);
};

export default Page;
```
29 changes: 22 additions & 7 deletions src/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
// 以下属性修改的时候,需要 dispose 之后再新建
// 1. 切换 theme 的时候
// 2. 修改 opts 的时候
if (
!isEqual(prevProps.theme, this.props.theme) ||
!isEqual(prevProps.opts, this.props.opts)
) {
if (!isEqual(prevProps.theme, this.props.theme) || !isEqual(prevProps.opts, this.props.opts)) {
this.dispose();

this.renderNewEcharts(); // 重建
Expand All @@ -70,8 +67,8 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
}

// when these props are not isEqual, update echarts
const pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
if (!isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
const pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'] as const;
if (!isEqual(pick(this.props, pickKeys), pick(prevProps as any, pickKeys))) {
this.updateEChartsOption();
}

Expand Down Expand Up @@ -243,7 +240,24 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
}

render(): JSX.Element {
const { style, className = '' } = this.props;
const {
style,
className = '',
echarts,
option,
theme,
notMerge,
replaceMerge,
lazyUpdate,
showLoading,
loadingOption,
opts,
onChartReady,
onEvents,
shouldSetOption,
autoResize,
...props
} = this.props;
// default height = 300
const newStyle = { height: 300, ...style };

Expand All @@ -254,6 +268,7 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
}}
style={newStyle}
className={`echarts-for-react ${className}`}
{...props}
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/helper/pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @param obj
* @param keys
*/
export function pick(obj: Record<string, unknown>, keys: string[]): Record<string, unknown> {
const r = {};
export function pick<T extends object>(obj: T, keys: readonly (keyof T)[]): Partial<T> {
const r = {} as Partial<T>;
keys.forEach((key) => {
r[key] = obj[key];
});
Expand Down
13 changes: 2 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { CSSProperties } from 'react';
import type { EChartsType } from 'echarts';

/**
Expand All @@ -16,19 +15,11 @@ export type Opts = {
readonly locale?: string;
};

export type EChartsReactProps = {
export interface EChartsReactProps extends React.HTMLAttributes<HTMLDivElement> {
/**
* echarts library entry, use it for import necessary.
*/
readonly echarts?: any;
/**
* `className` for container
*/
readonly className?: string;
/**
* `style` for container
*/
readonly style?: CSSProperties;
/**
* echarts option
*/
Expand Down Expand Up @@ -80,4 +71,4 @@ export type EChartsReactProps = {
* should trigger resize when window resize
*/
readonly autoResize?: boolean;
};
}