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

add custom component #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/useCSVDownloader.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import PapaParse, { type UnparseConfig } from 'papaparse';
import React from 'react';
import PapaParse, { UnparseConfig } from 'papaparse';

const Type = {
Link: 'link',
Button: 'button',
} as const;

export interface Props {
children: React.ReactNode;
export type Props = {
children?: React.ReactNode;
data: any;
filename: string;
type?: 'link' | 'button';
style?: any;
className?: string;
bom?: boolean;
config?: UnparseConfig;
}
} & (
| {
style?: any;
className?: string;
type?: 'link' | 'button';
component?: never;
}
| {
style?: never;
className?: never;
type?: never;
component?: (
props: React.ComponentPropsWithoutRef<'button'>,
) => React.JSX.Element;
}
);

function useCSVDownloaderComponent() {
const CSVDownloaderComponent = ({
Expand All @@ -27,6 +39,7 @@ function useCSVDownloaderComponent() {
className = '',
bom = false,
config = {},
component: Component,
}: Props) => {
const download = async () => {
const bomCode = bom ? '\ufeff' : '';
Expand Down Expand Up @@ -61,6 +74,9 @@ function useCSVDownloaderComponent() {
link.remove();
};

if (Component)
return <Component onClick={() => download()}>{children}</Component>;

return (
<>
{type === Type.Button ? (
Expand All @@ -80,7 +96,7 @@ function useCSVDownloaderComponent() {
);
};

const CSVDownloader = React.useMemo(() => CSVDownloaderComponent, []) as any;
const CSVDownloader = React.useMemo(() => CSVDownloaderComponent, []);

return CSVDownloader;
}
Expand Down