@progress/kendo-react-excel-export
Version:
KendoReact Excel Export package
171 lines • 6.96 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var PropTypes = require("prop-types");
var kendo_file_saver_1 = require("@progress/kendo-file-saver");
var workbook_1 = require("./ooxml/workbook");
var ExcelExport = /** @class */ (function (_super) {
__extends(ExcelExport, _super);
function ExcelExport(props) {
var _this = _super.call(this, props) || this;
_this.saveFile = function (dataURL) {
kendo_file_saver_1.saveAs(dataURL, _this.props.fileName, {
forceProxy: _this.props.forceProxy,
proxyURL: _this.props.proxyURL
});
};
_this.extractColumns = function (children, level) {
if (level === void 0) { level = 0; }
return Array.isArray(children)
? children.map(function (child) {
return _this.extractChild(child, level);
})
: [children, _this.extractChild(children, level)];
};
_this.extractChild = function (child, level) {
if (level === void 0) { level = 0; }
return React.isValidElement(child)
? __assign({}, child.props, { width: child.props.width && parseInt("" + child.props.width, 10), level: level, children: child.props.children
&& _this.extractColumns(child.props.children, level + 1) }) : __assign({}, child, { level: level, children: child.children && _this.extractColumns(child.children, level + 1) });
};
_this.getExportData = function (exportData) {
var result;
if (exportData) {
if (Array.isArray(exportData)) {
result = {
data: exportData
};
}
else {
result = exportData;
}
}
else {
result = {
data: _this.props.data,
group: _this.props.group
};
}
return result;
};
_this.save = _this.save.bind(_this);
_this.toDataURL = _this.toDataURL.bind(_this);
_this.workbookOptions = _this.workbookOptions.bind(_this);
return _this;
}
/**
* Saves the data to Excel.
*
* @param exportData - An optional parameter. Can be the data that will be exported or the [`WorkbookOptions`]({% slug api_excel-export_workbookoptions %}).
* @param columns - An optional parameter. If present, it will be used instead of the columns prop or the child column components.
*/
ExcelExport.prototype.save = function (exportData, columns) {
this.toDataURL(exportData, columns).then(this.saveFile);
};
/**
* Returns a promise which will be resolved with the file data URI.
*
* @param exportData - The optional data or the [`WorkbookOptions`]({% slug api_excel-export_workbookoptions %}) that will be used to generate the data URI.
* @param externalColumns - The optional columns that will be used.
* @returns {Promise<string>} - The promise that will be resolved by the file data URI.
*/
ExcelExport.prototype.toDataURL = function (exportData, columns) {
var options = workbook_1.isWorkbookOptions(exportData)
? exportData
: this.workbookOptions(exportData, columns);
return workbook_1.toDataURL(options);
};
/**
* Based on the specified columns and data, returns [`WorkbookOptions`]({% slug api_excel-export_workbookoptions %}).
*
* @param exportData - The optional data that will be exported.
* @param externalColumns - The optional columns that will be used.
* @returns {WorkbookOptions} - The workbook options.
*/
// tslint:enable:max-line-length
ExcelExport.prototype.workbookOptions = function (exportData, externalColumns) {
var currentData = this.getExportData(exportData);
var children = this.props.children;
var gridColumns = children && children.type &&
children.type.displayName === 'KendoReactGrid' &&
children.props && React.Children.toArray(children.props.children);
var columns = this.extractColumns(gridColumns ||
externalColumns ||
this.props.columns ||
React.Children.toArray(children));
var options = workbook_1.workbookOptions({
columns: columns,
data: currentData.data,
group: currentData.group,
filterable: this.props.filterable,
creator: this.props.creator,
date: this.props.date,
dir: this.props.dir,
hierarchy: this.props.hierarchy,
paddingCellOptions: this.props.paddingCellOptions,
headerPaddingCellOptions: this.props.headerPaddingCellOptions
});
return options;
};
/**
* @hidden
*/
ExcelExport.prototype.render = function () {
return this.props.children || null;
};
/**
* @hidden
*/
ExcelExport.propTypes = {
children: PropTypes.any,
columns: PropTypes.arrayOf(PropTypes.any),
creator: PropTypes.string,
data: PropTypes.any,
date: PropTypes.any,
filterable: PropTypes.bool,
fileName: PropTypes.string,
forceProxy: PropTypes.bool,
group: PropTypes.any,
headerPaddingCellOptions: PropTypes.any,
paddingCellOptions: PropTypes.any,
proxyURL: PropTypes.string,
dir: PropTypes.string,
hierarchy: PropTypes.bool,
collapsible: PropTypes.bool
};
/**
* @hidden
*/
ExcelExport.defaultProps = {
fileName: 'Export.xlsx',
forceProxy: false,
collapsible: false
};
return ExcelExport;
}(React.Component));
exports.default = ExcelExport;
//# sourceMappingURL=ExcelExport.js.map