UNPKG

@antv/s2-react-components

Version:

React components for S2

89 lines 4.66 kB
import { __awaiter, __rest } from "tslib"; import { MoreOutlined } from '@ant-design/icons'; import { CSV_SEPARATOR, S2_PREFIX_CLS, TAB_SEPARATOR, asyncGetAllData, asyncGetAllPlainData, copyToClipboard, download, i18n, } from '@antv/s2'; import { Button, Dropdown } from 'antd'; import cx from 'classnames'; import React from 'react'; export const Export = React.memo((props) => { const { className, children, async = true, copyOriginalText = i18n('复制原始数据'), copyFormatText = i18n('复制格式化数据'), downloadOriginalText = i18n('下载原始数据'), downloadFormatText = i18n('下载格式化数据'), sheetInstance, fileName = 'sheet', dropdown, customCopyMethod, onCopyError, onCopySuccess, onDownloadSuccess, onDownloadError } = props, restProps = __rest(props, ["className", "children", "async", "copyOriginalText", "copyFormatText", "downloadOriginalText", "downloadFormatText", "sheetInstance", "fileName", "dropdown", "customCopyMethod", "onCopyError", "onCopySuccess", "onDownloadSuccess", "onDownloadError"]); const PRE_CLASS = `${S2_PREFIX_CLS}-export`; const getData = (split, isFormat, method) => __awaiter(void 0, void 0, void 0, function* () { const params = { sheetInstance, split, formatOptions: isFormat, async, }; const data = yield ((customCopyMethod === null || customCopyMethod === void 0 ? void 0 : customCopyMethod(params)) || (method === null || method === void 0 ? void 0 : method(params))); return data; }); const getPlainData = (split, isFormat) => __awaiter(void 0, void 0, void 0, function* () { const result = yield getData(split, isFormat, asyncGetAllPlainData); return result; }); const getAllData = (split, isFormat) => __awaiter(void 0, void 0, void 0, function* () { const result = yield getData(split, isFormat, asyncGetAllData); return result; }); const copyData = (isFormat) => __awaiter(void 0, void 0, void 0, function* () { const data = yield getAllData(TAB_SEPARATOR, isFormat); copyToClipboard(data, async) .then(() => { onCopySuccess === null || onCopySuccess === void 0 ? void 0 : onCopySuccess(data); }) .catch((error) => { // eslint-disable-next-line no-console console.error('copy failed: ', error); onCopyError === null || onCopyError === void 0 ? void 0 : onCopyError(error); }); }); const downloadData = (isFormat) => __awaiter(void 0, void 0, void 0, function* () { // 导出的是 csv 格式, 复制时需要以逗号分割 https://github.com/antvis/S2/issues/2701 const data = yield getPlainData(CSV_SEPARATOR, isFormat); try { download(data, fileName); onDownloadSuccess === null || onDownloadSuccess === void 0 ? void 0 : onDownloadSuccess(data); } catch (error) { // eslint-disable-next-line no-console console.error('download failed: ', error); onDownloadError === null || onDownloadError === void 0 ? void 0 : onDownloadError(error); } }); return (React.createElement(React.Fragment, null, React.createElement(Dropdown, Object.assign({ menu: { items: [ { key: 'copyOriginal', label: copyOriginalText, onClick: () => { copyData(false); }, }, { key: 'copyFormat', label: copyFormatText, onClick: () => { copyData(true); }, }, { key: 'downloadOriginal', label: downloadOriginalText, onClick: () => { downloadData(false); }, }, { key: 'downloadFormat', label: downloadFormatText, onClick: () => { downloadData(true); }, }, ], }, trigger: ['click'], className: cx(PRE_CLASS, className) }, restProps, dropdown), children || (React.createElement(Button, { type: "text" }, React.createElement(MoreOutlined, null)))))); }); Export.displayName = 'Export'; //# sourceMappingURL=export.js.map