UNPKG

@antv/s2-react-components

Version:

React components for S2

92 lines 4.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Export = void 0; const tslib_1 = require("tslib"); const icons_1 = require("@ant-design/icons"); const s2_1 = require("@antv/s2"); const antd_1 = require("antd"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const react_1 = tslib_1.__importDefault(require("react")); exports.Export = react_1.default.memo((props) => { const { className, children, async = true, copyOriginalText = (0, s2_1.i18n)('复制原始数据'), copyFormatText = (0, s2_1.i18n)('复制格式化数据'), downloadOriginalText = (0, s2_1.i18n)('下载原始数据'), downloadFormatText = (0, s2_1.i18n)('下载格式化数据'), sheetInstance, fileName = 'sheet', dropdown, customCopyMethod, onCopyError, onCopySuccess, onDownloadSuccess, onDownloadError } = props, restProps = tslib_1.__rest(props, ["className", "children", "async", "copyOriginalText", "copyFormatText", "downloadOriginalText", "downloadFormatText", "sheetInstance", "fileName", "dropdown", "customCopyMethod", "onCopyError", "onCopySuccess", "onDownloadSuccess", "onDownloadError"]); const PRE_CLASS = `${s2_1.S2_PREFIX_CLS}-export`; const getData = (split, isFormat, method) => tslib_1.__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) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const result = yield getData(split, isFormat, s2_1.asyncGetAllPlainData); return result; }); const getAllData = (split, isFormat) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const result = yield getData(split, isFormat, s2_1.asyncGetAllData); return result; }); const copyData = (isFormat) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const data = yield getAllData(s2_1.TAB_SEPARATOR, isFormat); (0, s2_1.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) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { // 导出的是 csv 格式, 复制时需要以逗号分割 https://github.com/antvis/S2/issues/2701 const data = yield getPlainData(s2_1.CSV_SEPARATOR, isFormat); try { (0, s2_1.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_1.default.createElement(react_1.default.Fragment, null, react_1.default.createElement(antd_1.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: (0, classnames_1.default)(PRE_CLASS, className) }, restProps, dropdown), children || (react_1.default.createElement(antd_1.Button, { type: "text" }, react_1.default.createElement(icons_1.MoreOutlined, null)))))); }); exports.Export.displayName = 'Export'; //# sourceMappingURL=export.js.map