-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from uwblueprint/kathleen-megan/Export-As-CSV
Component-Export-as-csv
- Loading branch information
Showing
2 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from "react"; | ||
import { Button } from "@chakra-ui/react"; | ||
import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined"; | ||
import { TableData } from "./CommonTable"; | ||
|
||
type ExportProps = { | ||
exportData: TableData[]; | ||
}; | ||
|
||
// set exportData to be the list of objects needed to be exported | ||
const ExportToCSV = ({ exportData }: ExportProps): React.ReactElement => { | ||
const jsonToCSV = (jsonData: Array<TableData>) => { | ||
let csv = ""; | ||
const headers = Object.keys(jsonData[0]); | ||
csv += `${headers.join(",")}\n`; | ||
// Add the data | ||
jsonData.forEach((row: TableData) => { | ||
const data = headers | ||
.map((header) => JSON.stringify(row[header])) | ||
.join(","); | ||
csv += `${data}\n`; | ||
}); | ||
return csv; | ||
}; | ||
|
||
const downloadJsonAsCSV = () => { | ||
const csvData = jsonToCSV(exportData); | ||
// Create a CSV file and allow the user to download it | ||
const blob = new Blob([csvData], { type: "text/csv" }); | ||
const url = window.URL.createObjectURL(blob); | ||
const a = document.createElement("a"); | ||
a.href = url; | ||
a.download = "data.csv"; | ||
document.body.appendChild(a); | ||
a.click(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button | ||
alignItems="center" | ||
variant="secondary" | ||
onClick={downloadJsonAsCSV} | ||
gap={1} | ||
> | ||
<FileDownloadOutlinedIcon /> Export{" "} | ||
</Button> | ||
</> | ||
); | ||
}; | ||
|
||
export default ExportToCSV; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters