Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: types improvements #67

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/components/ChartKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import block from 'bem-cn-lite';
import {i18n} from '../i18n';
import {CHARTKIT_ERROR_CODE, ChartKitError, settings} from '../libs';
import {getRandomCKId, typedMemo} from '../utils';
import type {ChartkitType, ChartKitRef, ChartKitWidgetRef, ChartKitProps} from '../types';
import type {ChartKitType, ChartKitRef, ChartKitWidgetRef, ChartKitProps} from '../types';
import {ErrorBoundary} from './ErrorBoundary/ErrorBoundary';
import {Loader} from './Loader/Loader';

Expand All @@ -12,11 +12,11 @@ import './ChartKit.scss';

const b = block('chartkit');

type ChartKitComponentProps<T extends ChartkitType> = Omit<ChartKitProps<T>, 'onError'> & {
type ChartKitComponentProps<T extends ChartKitType> = Omit<ChartKitProps<T>, 'onError'> & {
instanceRef?: React.ForwardedRef<ChartKitRef | undefined>;
};

const ChartKitComponent = <T extends ChartkitType>(props: ChartKitComponentProps<T>) => {
const ChartKitComponent = <T extends ChartKitType>(props: ChartKitComponentProps<T>) => {
const widgetRef = React.useRef<ChartKitWidgetRef>();
const {instanceRef, id = getRandomCKId(), type, data, onLoad, isMobile, ...restProps} = props;
const lang = settings.get('lang');
Expand Down Expand Up @@ -62,14 +62,14 @@ const ChartKitComponent = <T extends ChartkitType>(props: ChartKitComponentProps

const ChartKitComponentWithErrorBoundary = React.forwardRef<
ChartKitRef | undefined,
ChartKitProps<ChartkitType>
ChartKitProps<ChartKitType>
>(function ChartKitComponentWithErrorBoundary(props, ref) {
return (
<ErrorBoundary onError={props.onError}>
<ChartKitComponent instanceRef={ref} {...props} />
</ErrorBoundary>
);
}) /* https://stackoverflow.com/a/58473012 */ as <T extends ChartkitType>(
}) /* https://stackoverflow.com/a/58473012 */ as <T extends ChartKitType>(
props: ChartKitProps<T> & {ref?: React.ForwardedRef<ChartKitRef | undefined>},
) => ReturnType<typeof ChartKitComponent>;

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export type {
ChartKitProps,
ChartKitRef,
ChartKitWidgetRef,
ChartkitType,
ChartkitWidget,
ChartKitType,
ChartKitWidget,
} from './types';

export {settings};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/highcharts/renderer/helpers/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ChartKitHolidays} from '../../../../types';
import type {
Highcharts,
HighchartsWidgetData,
ChartKitComment,
HighchartsComment,
ExtendedHChart,
XAxisItem,
} from '../../types';
Expand All @@ -18,7 +18,7 @@ import './graph.scss';
type GetGraphArgs = {
options: ConfigOptions;
data: HighchartsWidgetData['data'];
comments?: ChartKitComment[];
comments?: HighchartsComment[];
holidays?: ChartKitHolidays;
isMobile?: boolean;
};
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/highcharts/types/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ interface CommentFlagX extends CommentBase {
};
}

export type ChartKitComment = CommentDotXY | CommentBandX | CommentLineX | CommentFlagX;
export type HighchartsComment = CommentDotXY | CommentBandX | CommentLineX | CommentFlagX;
2 changes: 1 addition & 1 deletion src/plugins/highcharts/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type {Highcharts} from './lib';
export type {HighchartsWidgetData} from './widget';
export type {DrillDownConfig, ExtendedHChart, StringParams, XAxisItem} from './misc';
export type {ChartKitComment} from './comments';
export type {HighchartsComment} from './comments';
8 changes: 4 additions & 4 deletions src/plugins/highcharts/types/misc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ChartKitComment} from './comments';
import type {HighchartsComment} from './comments';

export type StringParams = Record<string, string | string[]>;

Expand All @@ -14,9 +14,9 @@ export type XAxisItem = Highcharts.Axis & {

export type ExtendedHChart = Highcharts.Chart & {
userOptions: Highcharts.Options & {
_internalComments: ChartKitComment[];
_externalComments: ChartKitComment[];
_getComments: () => ChartKitComment[];
_internalComments: HighchartsComment[];
_externalComments: HighchartsComment[];
_getComments: () => HighchartsComment[];
};
xAxis: XAxisItem[];
navigator?: Highcharts.Options['navigator'];
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/highcharts/types/widget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Highcharts} from './lib';
import type {ChartKitComment} from './comments';
import type {HighchartsComment} from './comments';
import type {DrillDownConfig, StringParams} from './misc';

type GraphWidgetSeriesOptions = Highcharts.SeriesOptionsType & {
Expand All @@ -22,7 +22,7 @@ export type HighchartsWidgetData = {
| {
graphs: GraphWidgetSeriesOptions[];
}
) & {comments?: ChartKitComment[]};
) & {comments?: HighchartsComment[]};
config: {
hideComments?: boolean;
hideHolidaysBands?: boolean;
Expand All @@ -46,6 +46,6 @@ export type HighchartsWidgetData = {
};
libraryConfig: Highcharts.Options;
params?: StringParams;
comments?: ChartKitComment[];
comments?: HighchartsComment[];
sideMarkdown?: string;
};
2 changes: 1 addition & 1 deletion src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export type {Yagr, YagrWidgetData} from './yagr/types';
export {IndicatorPlugin} from './indicator';
export type {IndicatorWidgetData} from './indicator/types';
export {HighchartsPlugin} from './highcharts';
export type {Highcharts, HighchartsWidgetData} from './highcharts/types';
export type {Highcharts, HighchartsWidgetData, HighchartsComment} from './highcharts/types';
18 changes: 9 additions & 9 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {ChartkitWidget} from './widget';
import type {ChartKitWidget} from './widget';

export type {ChartKitHolidays} from './misc';

export type ChartKitLang = 'ru' | 'en';

export type ChartkitType = keyof ChartkitWidget;
export type ChartKitType = keyof ChartKitWidget;

export type ChartKitRef = {
reflow: (details?: unknown) => void;
Expand All @@ -14,25 +14,25 @@ export type ChartKitWidgetRef = {
reflow?: ChartKitRef['reflow'];
};

export type ChartKitOnLoadData<T extends ChartkitType> = {
widget?: ChartkitWidget[T]['widget'];
export type ChartKitOnLoadData<T extends ChartKitType> = {
widget?: ChartKitWidget[T]['widget'];
widgetRendering?: number;
};

export type ChartKitOnError = (data: {error: any}) => void;

export type ChartKitProps<T extends ChartkitType> = {
export type ChartKitProps<T extends ChartKitType> = {
type: T;
data: ChartkitWidget[T]['data'];
data: ChartKitWidget[T]['data'];
id?: string;
isMobile?: boolean;
onLoad?: (data?: ChartKitOnLoadData<T>) => void;
onError?: ChartKitOnError;
} & {[key in keyof Omit<ChartkitWidget[T], 'data' | 'widget'>]: ChartkitWidget[T][key]};
} & {[key in keyof Omit<ChartKitWidget[T], 'data' | 'widget'>]: ChartKitWidget[T][key]};

export type ChartKitPlugin = {
type: ChartkitType;
type: ChartKitType;
renderer: React.LazyExoticComponent<any>;
};

export type {ChartkitWidget};
export type {ChartKitWidget};
2 changes: 1 addition & 1 deletion src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {YagrWidgetData} from '../plugins/yagr/types';
import type {IndicatorWidgetData} from '../plugins/indicator/types';
import type {Highcharts, HighchartsWidgetData, StringParams} from '../plugins/highcharts/types';

export interface ChartkitWidget {
export interface ChartKitWidget {
yagr: {
data: YagrWidgetData;
widget: Yagr;
Expand Down