UNPKG

@siamf/react-export

Version:

A React package that simplifies data exporting and clipboard management. It provides react component for printing documents, exporting data as PDF, Excel, and CSV, as well as copying text and structured data to the clipboard.

33 lines 1.17 kB
"use client"; import { jsx as _jsx } from "react/jsx-runtime"; import React from "react"; function convertToCSV(data) { const headers = Object.keys(data[0]).join(',') + '\n'; const rows = data.map(item => Object.values(item).join(',') + '\n'); return headers + rows.join(''); } const ExportAsCsv = ({ data, children, fileName = "reactExport", onError, onSuccess }) => { const csvData = convertToCSV(data); const downloadCSV = () => { try { const blob = new Blob([csvData], { type: 'text/csv' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${fileName}.csv`; a.click(); window.URL.revokeObjectURL(url); if (onSuccess) onSuccess(); } catch (error) { if (onError) onError(error); } }; return (_jsx(React.Fragment, { children: children === null || children === void 0 ? void 0 : children({ onClick: downloadCSV }) })); }; export default ExportAsCsv; //# sourceMappingURL=ExportAsCsv.js.map