All files / src/stories EditableTableExportButton.tsx

20% Statements 2/10
0% Branches 0/2
25% Functions 1/4
12.5% Lines 1/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29            12x                                            
import React from 'react';
 
import { IconButton, Tooltip } from '@mui/material';
 
import Icon from '../components/Icon/Icon';
 
const EditableTableExportButton = ({ getRows }: { getRows: () => unknown[][] }) => (
  <Tooltip title='Export'>
    <IconButton
      aria-label='Export'
      onClick={() => {
        const csv = getRows()
          .map((row) => row.map((value) => `"${String(value ?? '').replace(/"/g, '""')}"`).join(','))
          .join('\r\n');
        const url = URL.createObjectURL(new Blob([csv], { type: 'text/csv;charset=utf-8;' }));
        const link = document.createElement('a');
        link.href = url;
        link.download = 'table.csv';
        link.click();
        URL.revokeObjectURL(url);
      }}
    >
      <Icon name='iconExport' size='medium' />
    </IconButton>
  </Tooltip>
);
 
export default EditableTableExportButton;