UNPKG

@churchapps/apphelper

Version:

Library of helper functions for React and NextJS ChurchApps

66 lines 2.99 kB
"use client"; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { Button, Icon } from "@mui/material"; import { Suspense, lazy } from "react"; // Lazy load the CSVLink component const CSVLink = lazy(() => import("react-csv").then(module => ({ default: module.CSVLink }))); export const ExportLink = (props) => { const people = props.data ? [...props.data] : []; const getHeaders = () => { const result = []; if (people?.length > 0) { const names = getAllPropertyNames(); for (let i = 0; i < names.length; i++) { result.push({ label: names[i], key: names[i] }); } } return result; }; const getAllPropertyNames = () => { const result = []; for (let i = 0; i < people.length; i++) { const p = { ...people[i] }; p.birthDate = p.birthDate ? new Date(p.birthDate).toISOString() : null; p.anniversary = p.anniversary ? new Date(p.anniversary).toISOString() : null; people[i] = p; const propertyNames = getPropertyNames("", people[i]); for (let j = 0; j < propertyNames.length; j++) if (result.indexOf(propertyNames[j]) === -1) result.push(propertyNames[j]); } return result.sort(); }; const getPropertyNames = (prefix, obj) => { const result = []; const names = Object.getOwnPropertyNames(obj); for (let i = 0; i < names.length; i++) { const t = typeof obj[names[i]]; switch (t) { case "number": case "string": case "boolean": result.push(prefix + names[i]); break; case "object": if ((obj[names[i]] !== null)) { const children = getPropertyNames(prefix + names[i] + ".", obj[names[i]]); for (let j = 0; j < children.length; j++) result.push(children[j]); } } } return result; }; if (!people || people?.length === 0) return null; else { const items = []; if (props.spaceBefore) items.push(" "); items.push(_jsx(Suspense, { fallback: _jsxs(Button, { children: [_jsx(Icon, { sx: { marginRight: props.text ? 1 : 0 }, children: props.icon || "file_download" }), props.text || ""] }), children: _jsx(CSVLink, { data: people, headers: props.customHeaders || getHeaders(), filename: props.filename || "export.csv", children: _jsxs(Button, { children: [_jsx(Icon, { sx: { marginRight: props.text ? 1 : 0 }, children: props.icon || "file_download" }), props.text || ""] }) }) }, props.filename)); if (props.spaceAfter) items.push(" "); return (_jsx(_Fragment, { children: items })); } }; //# sourceMappingURL=ExportLink.js.map