Skip to content

Commit

Permalink
fix: allow to hide plugin loader by passing null by renderPluginLoader (
Browse files Browse the repository at this point in the history
#340)

fix: allow hide plugin loader by passing null through renderPluginLoader
  • Loading branch information
korvin89 authored Nov 13, 2023
1 parent 2e57d8e commit f7ca281
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/ChartKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ChartKitComponent = <T extends ChartKitType>(props: ChartKitComponentProps
);

return (
<React.Suspense fallback={renderPluginLoader?.() || <Loader />}>
<React.Suspense fallback={<Loader renderPluginLoader={renderPluginLoader} />}>
<div className={b({mobile: isMobile}, 'chartkit-theme_common')}>
<ChartComponent ref={widgetRef} id={id} lang={lang} {...restProps} />
</div>
Expand Down
12 changes: 10 additions & 2 deletions src/components/Loader/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import React from 'react';
import {Loader as BaseLoader, LoaderProps as BaseLoaderProps} from '@gravity-ui/uikit';
import {block} from '../../utils/cn';
import type {ChartKitRenderPluginLoader} from '../../types';

import './Loader.scss';

const b = block('loader');

type LoaderProps = BaseLoaderProps;
type LoaderProps = BaseLoaderProps & {renderPluginLoader?: ChartKitRenderPluginLoader};

export const Loader = ({renderPluginLoader, ...props}: LoaderProps) => {
const pluginLoader = renderPluginLoader?.();

// React.Suspense complains about possible undefined in "fallback" property
if (typeof pluginLoader !== 'undefined') {
return pluginLoader as React.JSX.Element;
}

export const Loader = (props: LoaderProps) => {
return (
<div className={b()}>
<BaseLoader {...props} />
Expand Down
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type ChartKitOnChartLoad<T extends ChartKitType> = {

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

export type ChartKitRenderPluginLoader = () => React.ReactNode;

export type ChartKitProps<T extends ChartKitType> = {
type: T;
data: ChartKitWidget[T]['data'];
Expand All @@ -48,7 +50,7 @@ export type ChartKitProps<T extends ChartKitType> = {
/** Used to render user's error component */
renderError?: RenderError;
/** Used to render user's plugin loader component */
renderPluginLoader?: () => React.ReactNode;
renderPluginLoader?: ChartKitRenderPluginLoader;
} & {
[key in keyof Omit<ChartKitWidget[T], 'data' | 'widget'>]: ChartKitWidget[T][key];
};
Expand Down

0 comments on commit f7ca281

Please sign in to comment.