import classNames from 'classnames'; import React, { useEffect, useState } from 'react'; import { downloadAs, DownloadType } from '../../data-entry/utils'; import { Wrapper as MenuWrapper, Button, Menu, MenuItem } from 'react-aria-menubutton'; import { FaAngleDown } from 'react-icons/fa'; interface Props { id?: string; setProps?: (value: any) => any; className?: string; buttonClassName?: string; data: any; filename?: string; tooltip?: string; } export const DownloadDropdown: React.FC = ({ filename = 'export', ...otherProps }) => { const props = { filename, ...otherProps }; const [downloadType, setDownloadType] = useState(null); const [data, setData] = useState(props.data); const handleDownloadTypeChange = (type: DownloadType) => { setDownloadType(type); }; /** * Trigger download when download type changes * Running the download method inside an effect ensures * that props.data contains the latest data. * Setting downloadType to null after executon ensures * that the effect will be triggered on every selection. */ useEffect(() => { if (downloadType) { downloadAs[downloadType](data, props.filename); setDownloadType(null); } }, [downloadType]); useEffect(() => { setData(props.data); }, [props.data]); return (
  • JSON
  • CSV
  • {/*
  • Excel
  • */}
); };