diff --git a/src/useCSVDownloader.tsx b/src/useCSVDownloader.tsx index c98487c..c52a51f 100644 --- a/src/useCSVDownloader.tsx +++ b/src/useCSVDownloader.tsx @@ -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 = ({ @@ -27,6 +39,7 @@ function useCSVDownloaderComponent() { className = '', bom = false, config = {}, + component: Component, }: Props) => { const download = async () => { const bomCode = bom ? '\ufeff' : ''; @@ -61,6 +74,9 @@ function useCSVDownloaderComponent() { link.remove(); }; + if (Component) + return download()}>{children}; + return ( <> {type === Type.Button ? ( @@ -80,7 +96,7 @@ function useCSVDownloaderComponent() { ); }; - const CSVDownloader = React.useMemo(() => CSVDownloaderComponent, []) as any; + const CSVDownloader = React.useMemo(() => CSVDownloaderComponent, []); return CSVDownloader; }