Skip to content

Commit

Permalink
fix: minor types fixes (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Oct 13, 2021
1 parent 028b33f commit fe6c00f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
"start": "microbundle-crl watch --no-compress --format modern,cjs",
"dev": "microbundle-crl --no-compress --no-sourcemap watch",
"prepublishOnly": "yarn build",
"test": "yarn test:unit && yarn test:lint && yarn test:build",
"test:build": "yarn build",
"test:lint": "eslint 'src/**/*.{ts,tsx}' 'test/**/*.{ts,tsx}'",
"test:unit": "jest -c jest.config.json",
"test:types": "tsc --skipLibCheck --noEmit",
"test:build": "yarn build",
"test": "yarn test:lint && yarn test:unit && yarn test:types && yarn test:build",
"format": "prettier --write src",
"predeploy": "cd example && yarn && yarn build",
"deploy": "gh-pages -d example/build"
Expand Down
11 changes: 7 additions & 4 deletions src/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import React, {
useMemo,
forwardRef,
} from 'react';
import { Props } from './types';

import Chart from 'chart.js/auto';
import type { ChartData } from 'chart.js';

import merge from 'lodash/merge';
import assign from 'lodash/assign';
import find from 'lodash/find';

import { Props } from './types';

const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
const {
id,
Expand All @@ -37,7 +36,11 @@ const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {

const computedData = useMemo<ChartData>(() => {
if (typeof data === 'function') {
return canvas.current ? data(canvas.current) : {};
return canvas.current
? data(canvas.current)
: {
datasets: [],
};
} else return merge({}, data);
}, [data, canvas.current]);

Expand Down
8 changes: 3 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { forwardRef } from 'react';
import Chart, { defaults } from 'chart.js/auto';

import { Props } from './types';
import ChartComponent from './chart';
import Chart from 'chart.js/auto';
import * as chartjs from 'chart.js';

export const Line = forwardRef<Chart | undefined, Omit<Props, 'type'>>(
(props, ref) => (
Expand Down Expand Up @@ -92,8 +92,6 @@ export const Scatter = forwardRef<Chart | undefined, Omit<Props, 'type'>>(
)
);

export const defaults = chartjs.defaults;

export { Chart };
export { Chart, defaults };

export default ChartComponent;
27 changes: 17 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import Chart from './index';
import type { CanvasHTMLAttributes, ReactNode, MouseEvent } from 'react';
import type {
ChartType,
ChartData,
ChartOptions,
Plugin,
InteractionItem,
} from 'chart.js';

export interface Props extends React.CanvasHTMLAttributes<HTMLCanvasElement> {
export interface Props extends CanvasHTMLAttributes<HTMLCanvasElement> {
id?: string;
className?: string;
height?: number;
width?: number;
redraw?: boolean;
type: Chart.ChartType;
data: Chart.ChartData | ((canvas: HTMLCanvasElement) => Chart.ChartData);
options?: Chart.ChartOptions;
fallbackContent?: React.ReactNode;
plugins?: Chart.PluginServiceRegistrationOptions[];
type: ChartType;
data: ChartData | ((canvas: HTMLCanvasElement) => ChartData);
options?: ChartOptions;
fallbackContent?: ReactNode;
plugins?: Plugin[];
getDatasetAtEvent?: (
dataset: Array<{}>,
event: React.MouseEvent<HTMLCanvasElement>
event: MouseEvent<HTMLCanvasElement>
) => void;
getElementAtEvent?: (
element: InteractionItem[],
event: React.MouseEvent<HTMLCanvasElement>
event: MouseEvent<HTMLCanvasElement>
) => void;
getElementsAtEvent?: (
elements: Array<{}>,
event: React.MouseEvent<HTMLCanvasElement>
event: MouseEvent<HTMLCanvasElement>
) => void;
}
6 changes: 0 additions & 6 deletions tsconfig.test.json

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6543,7 +6543,7 @@ tslib@1.10.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==

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

0 comments on commit fe6c00f

Please sign in to comment.