Skip to content

add callback after the file is generated #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"workbench.colorCustomizations": {
"editorIndentGuide.activeBackground": "#20e20e9e"
}
}
9 changes: 9 additions & 0 deletions dist/ExcelPlugin/components/ExcelFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ var ExcelFile = function (_React$Component) {
value: function download() {
var _this2 = this;

if (this.props.onGenerationStart) {
this.props.onGenerationStart();
}
var wb = {
SheetNames: _react2.default.Children.map(this.props.children, function (sheet) {
return sheet.props.name;
Expand All @@ -104,6 +107,10 @@ var ExcelFile = function (_React$Component) {
var wbout = _tempaXlsx2.default.write(wb, { bookType: fileExtension, bookSST: true, type: 'binary' });

(0, _fileSaver.saveAs)(new Blob([(0, _DataUtil.strToArrBuffer)(wbout)], { type: "application/octet-stream" }), fileName);

if (this.props.onFileGenerated) {
this.props.onFileGenerated();
}
}
}, {
key: "getFileName",
Expand Down Expand Up @@ -165,6 +172,8 @@ ExcelFile.props = {
filename: _propTypes2.default.string,
fileExtension: _propTypes2.default.string,
element: _propTypes2.default.any,
onFileGenerated: _propTypes2.default.function,
onGenerationStart: _propTypes2.default.function,
children: function children(props, propName, componentName) {
_react2.default.Children.forEach(props[propName], function (child) {
if (child.type !== _ExcelSheet2.default) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-data-export",
"version": "0.6.0",
"version": "0.6.1",
"main": "dist/index.js",
"types": "types/index.d.ts",
"description": "A set of tools to export dataset from react to different formats.",
Expand Down Expand Up @@ -73,4 +73,4 @@
"@commitlint/config-conventional"
]
}
}
}
21 changes: 15 additions & 6 deletions src/ExcelPlugin/components/ExcelFile.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React from "react";
import PropTypes from "prop-types";
import {saveAs} from "file-saver";
import { saveAs } from "file-saver";
import XLSX from "tempa-xlsx";

import ExcelSheet from "../elements/ExcelSheet";
import {strToArrBuffer, excelSheetFromAoA, excelSheetFromDataSet} from "../utils/DataUtil";
import { strToArrBuffer, excelSheetFromAoA, excelSheetFromDataSet } from "../utils/DataUtil";

class ExcelFile extends React.Component {
fileExtensions = ['xlsx', 'xls', 'csv', 'txt', 'html'];
defaultFileExtension = 'xlsx';

static props = {
static props = {
hideElement: PropTypes.bool,
filename: PropTypes.string,
fileExtension: PropTypes.string,
element: PropTypes.any,
onFileGenerated: PropTypes.function,
onGenerationStart: PropTypes.function,
children: function (props, propName, componentName) {
React.Children.forEach(props[propName], child => {
if (child.type !== ExcelSheet) {
Expand Down Expand Up @@ -64,6 +66,9 @@ class ExcelFile extends React.Component {
}

download() {
if (this.props.onGenerationStart) {
this.props.onGenerationStart();
}
const wb = {
SheetNames: React.Children.map(this.props.children, sheet => sheet.props.name),
Sheets: {}
Expand All @@ -79,9 +84,13 @@ class ExcelFile extends React.Component {

const fileExtension = this.getFileExtension();
const fileName = this.getFileName();
const wbout = XLSX.write(wb, {bookType: fileExtension, bookSST: true, type: 'binary'});
const wbout = XLSX.write(wb, { bookType: fileExtension, bookSST: true, type: 'binary' });

saveAs(new Blob([strToArrBuffer(wbout)], { type: "application/octet-stream" }), fileName);

saveAs(new Blob([strToArrBuffer(wbout)], {type: "application/octet-stream"}), fileName);
if (this.props.onFileGenerated) {
this.props.onFileGenerated();
}
}

getFileName() {
Expand Down Expand Up @@ -121,7 +130,7 @@ class ExcelFile extends React.Component {
} else {
return (<span onClick={this.handleDownload}>{element}</span>);
}

}
}

Expand Down
59 changes: 18 additions & 41 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
/* index.d.ts (C) react-data-export */

// TypeScript Version: 2.2
declare module 'react-data-export' {
import * as React from 'react'

declare module "react-data-export" {
export interface ExcelFileProps {
filename?: string;
onFileGenerated?: () => void;
onGenerationStart?: () => void;
fileExtension?: string;
element?: any; //Download Element
children?: Array<React.ReactChild> | React.ReactChild; // Array<ExcelSheetProps>;
element?: any;
children?: any
}

export interface ExcelSheetProps {
name: string;
data?: Array<object>;
dataSet?: Array<ExcelSheetData>;
value?: Array<string> | Function;
children?: Array<React.ReactChild> | React.ReactChild; // Array<ExcelColumnProps>
data?: () => any | any[];
dataSet?: any;
value?: string[] | Function;
children?: any
}

export interface ExcelSheetData {
xSteps?: number;
ySteps?: number;
columns: Array<string>;
data: Array<ExcelCellData>;
data: any
}

export type ExcelCellData = ExcelValue | ExcelCell | Array<ExcelValue>;
export type ExcelCellData = ExcelValue | ExcelCell;
export type ExcelValue = string | number | Date | boolean;

export interface ExcelCell {
Expand Down Expand Up @@ -96,7 +93,7 @@ declare module 'react-data-export' {

export type ExcelTextRotation = 0 | 45 | 90 | 135 | 180 | 255;

export enum ExcelReadingOrder { LeftToRight = 1, RightToLeft}
enum ExcelReadingOrder { LeftToRight = 1, RightToLeft }

export type ExcelAlignmentType = "bottom" | "center" | "top";

Expand All @@ -106,32 +103,12 @@ declare module 'react-data-export' {
color: ExcelColorSpec;
}

export type ExcelBorderStyle =
"thin"
| "medium"
| "thick"
| "dotted"
| "hair"
| "dashed"
| "mediumDashed"
| "dashDot"
| "mediumDashDot"
| "dashDotDot"
| "mediumDashDotDot"
| "slantDashDot";

export class ExcelColumn extends React.Component<ExcelColumnProps, any> {
}

export class ExcelSheet extends React.Component<ExcelSheetProps, any> {
}

export class ExcelFile extends React.Component<ExcelFileProps, any> {
}
export type ExcelBorderStyle = "thin" | "medium" | "thick" | "dotted" | "hair" | "dashed" | "mediumDashed" | "dashDot" | "mediumDashDot" | "dashDotDot" | "mediumDashDotDot" | "slantDashDot";

export namespace ReactExport {
export class ExcelFile extends React.Component<ExcelFileProps, any> {
}
declare class ExcelFile extends React.Component<ExcelFileProps> {
static ExcelSheet: typeof ExcelSheet
static ExcelColumn: typeof ExcelColumn
}
export default ReactExport
declare class ExcelSheet extends React.Component<ExcelSheetProps> { }
declare class ExcelColumn extends React.Component<ExcelColumnProps> { }
}