Skip to content

Commit fe6c00f

Browse files
authored
fix: minor types fixes (#759)
1 parent 028b33f commit fe6c00f

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
"start": "microbundle-crl watch --no-compress --format modern,cjs",
2929
"dev": "microbundle-crl --no-compress --no-sourcemap watch",
3030
"prepublishOnly": "yarn build",
31-
"test": "yarn test:unit && yarn test:lint && yarn test:build",
32-
"test:build": "yarn build",
3331
"test:lint": "eslint 'src/**/*.{ts,tsx}' 'test/**/*.{ts,tsx}'",
3432
"test:unit": "jest -c jest.config.json",
33+
"test:types": "tsc --skipLibCheck --noEmit",
34+
"test:build": "yarn build",
35+
"test": "yarn test:lint && yarn test:unit && yarn test:types && yarn test:build",
3536
"format": "prettier --write src",
3637
"predeploy": "cd example && yarn && yarn build",
3738
"deploy": "gh-pages -d example/build"

src/chart.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import React, {
66
useMemo,
77
forwardRef,
88
} from 'react';
9-
import { Props } from './types';
10-
119
import Chart from 'chart.js/auto';
1210
import type { ChartData } from 'chart.js';
13-
1411
import merge from 'lodash/merge';
1512
import assign from 'lodash/assign';
1613
import find from 'lodash/find';
1714

15+
import { Props } from './types';
16+
1817
const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
1918
const {
2019
id,
@@ -37,7 +36,11 @@ const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
3736

3837
const computedData = useMemo<ChartData>(() => {
3938
if (typeof data === 'function') {
40-
return canvas.current ? data(canvas.current) : {};
39+
return canvas.current
40+
? data(canvas.current)
41+
: {
42+
datasets: [],
43+
};
4144
} else return merge({}, data);
4245
}, [data, canvas.current]);
4346

src/index.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { forwardRef } from 'react';
2+
import Chart, { defaults } from 'chart.js/auto';
3+
24
import { Props } from './types';
35
import ChartComponent from './chart';
4-
import Chart from 'chart.js/auto';
5-
import * as chartjs from 'chart.js';
66

77
export const Line = forwardRef<Chart | undefined, Omit<Props, 'type'>>(
88
(props, ref) => (
@@ -92,8 +92,6 @@ export const Scatter = forwardRef<Chart | undefined, Omit<Props, 'type'>>(
9292
)
9393
);
9494

95-
export const defaults = chartjs.defaults;
96-
97-
export { Chart };
95+
export { Chart, defaults };
9896

9997
export default ChartComponent;

src/types.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
import Chart from './index';
1+
import type { CanvasHTMLAttributes, ReactNode, MouseEvent } from 'react';
2+
import type {
3+
ChartType,
4+
ChartData,
5+
ChartOptions,
6+
Plugin,
7+
InteractionItem,
8+
} from 'chart.js';
29

3-
export interface Props extends React.CanvasHTMLAttributes<HTMLCanvasElement> {
10+
export interface Props extends CanvasHTMLAttributes<HTMLCanvasElement> {
411
id?: string;
512
className?: string;
613
height?: number;
714
width?: number;
815
redraw?: boolean;
9-
type: Chart.ChartType;
10-
data: Chart.ChartData | ((canvas: HTMLCanvasElement) => Chart.ChartData);
11-
options?: Chart.ChartOptions;
12-
fallbackContent?: React.ReactNode;
13-
plugins?: Chart.PluginServiceRegistrationOptions[];
16+
type: ChartType;
17+
data: ChartData | ((canvas: HTMLCanvasElement) => ChartData);
18+
options?: ChartOptions;
19+
fallbackContent?: ReactNode;
20+
plugins?: Plugin[];
1421
getDatasetAtEvent?: (
1522
dataset: Array<{}>,
16-
event: React.MouseEvent<HTMLCanvasElement>
23+
event: MouseEvent<HTMLCanvasElement>
1724
) => void;
1825
getElementAtEvent?: (
1926
element: InteractionItem[],
20-
event: React.MouseEvent<HTMLCanvasElement>
27+
event: MouseEvent<HTMLCanvasElement>
2128
) => void;
2229
getElementsAtEvent?: (
2330
elements: Array<{}>,
24-
event: React.MouseEvent<HTMLCanvasElement>
31+
event: MouseEvent<HTMLCanvasElement>
2532
) => void;
2633
}

tsconfig.test.json

-6
This file was deleted.

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -6543,7 +6543,7 @@ tslib@1.10.0:
65436543
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
65446544
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==
65456545

6546-
tslib@^1.11.1, tslib@^1.8.1:
6546+
tslib@^1.11.1, tslib@^1.8.1, tslib@^1.9.0:
65476547
version "1.14.1"
65486548
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
65496549
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

0 commit comments

Comments
 (0)