Skip to content

Commit

Permalink
fix(excelfile): changes to fix import
Browse files Browse the repository at this point in the history
  • Loading branch information
susanta96 committed Jun 13, 2023
1 parent 3cae870 commit de7bac0
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 53 deletions.
16 changes: 9 additions & 7 deletions dist/ExcelPlugin/components/ExcelFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var ExcelFile = /*#__PURE__*/function (_react_1$default$Comp) {
_this = _super.call.apply(_super, [this].concat(args));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
fileName: "Download",
fileExtension: "xlsx"
fileExtension: "xlsx",
hideElement: false
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "createSheetData", function (sheet) {
var columns = sheet.props.children;
Expand Down Expand Up @@ -126,14 +127,17 @@ var ExcelFile = /*#__PURE__*/function (_react_1$default$Comp) {
fileExtension: this.props.fileExtension
});
}
if (this.props.hideElement) {
this.setState({
hideElement: this.props.hideElement
});
}
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
hideElement = _this$props.hideElement,
element = _this$props.element;
if (hideElement) {
var element = this.props.element;
if (this.state.hideElement === true) {
return null;
} else {
return react_1.default.createElement("span", {
Expand All @@ -144,6 +148,4 @@ var ExcelFile = /*#__PURE__*/function (_react_1$default$Comp) {
}]);
return ExcelFile;
}(react_1.default.Component);
(0, _defineProperty2.default)(ExcelFile, "ExcelColumn", void 0);
(0, _defineProperty2.default)(ExcelFile, "ExcelSheet", void 0);
exports.default = ExcelFile;
5 changes: 3 additions & 2 deletions dist/ExcelPlugin/elements/ExcelSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ var ExcelSheet = /*#__PURE__*/function (_react_1$Component) {
(0, _inherits2.default)(ExcelSheet, _react_1$Component);
var _super = _createSuper(ExcelSheet);
function ExcelSheet(props) {
var _props$children;
var _this;
(0, _classCallCheck2.default)(this, ExcelSheet);
_this = _super.call(this, props);
if (!props.children.every(function (child) {
if (!((_props$children = props.children) !== null && _props$children !== void 0 && _props$children.every(function (child) {
return child.type === ExcelColumn_1.default;
})) {
}))) {
throw new Error("<ExcelSheet> can only have <ExcelColumn> as children");
}
return _this;
Expand Down
10 changes: 6 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ExcelColumn = exports.ExcelSheet = void 0;
exports.ExcelColumn = exports.ExcelSheet = exports.ExcelFile = void 0;
var ExcelFile_1 = __importDefault(require("./ExcelPlugin/components/ExcelFile"));
exports.ExcelFile = ExcelFile_1.default;
var ExcelSheet_1 = __importDefault(require("./ExcelPlugin/elements/ExcelSheet"));
exports.ExcelSheet = ExcelSheet_1.default;
var ExcelColumn_1 = __importDefault(require("./ExcelPlugin/elements/ExcelColumn"));
exports.ExcelColumn = ExcelColumn_1.default;
ExcelFile_1.default.ExcelSheet = ExcelSheet_1.default;
ExcelFile_1.default.ExcelColumn = ExcelColumn_1.default;
exports.default = ExcelFile_1.default;
var ReactExport = {
ExcelFile: ExcelFile_1.default
};
exports.default = ReactExport;
12 changes: 8 additions & 4 deletions src-js/ExcelPlugin/components/ExcelFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class ExcelFile extends react_1.default.Component {
state = {
fileName: "Download",
fileExtension: "xlsx",
hideElement: false,
};
static ExcelColumn;
static ExcelSheet;
componentDidMount() {
if (this.props.filename) {
this.setState({
Expand All @@ -24,6 +23,11 @@ class ExcelFile extends react_1.default.Component {
fileExtension: this.props.fileExtension,
});
}
if (this.props.hideElement) {
this.setState({
hideElement: this.props.hideElement,
});
}
}
createSheetData = (sheet) => {
const columns = sheet.props.children;
Expand Down Expand Up @@ -102,8 +106,8 @@ class ExcelFile extends react_1.default.Component {
this.download();
};
render() {
const { hideElement, element } = this.props;
if (hideElement) {
const { element } = this.props;
if (this.state.hideElement === true) {
return null;
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src-js/ExcelPlugin/elements/ExcelSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ExcelColumn_1 = __importDefault(require("./ExcelColumn"));
class ExcelSheet extends react_1.Component {
constructor(props) {
super(props);
if (!props.children.every((child) => child.type === ExcelColumn_1.default)) {
if (!props.children?.every((child) => child.type === ExcelColumn_1.default)) {
throw new Error("<ExcelSheet> can only have <ExcelColumn> as children");
}
}
Expand Down
10 changes: 6 additions & 4 deletions src-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExcelColumn = exports.ExcelSheet = void 0;
exports.ExcelColumn = exports.ExcelSheet = exports.ExcelFile = void 0;
const ExcelFile_1 = __importDefault(require("./ExcelPlugin/components/ExcelFile"));
exports.ExcelFile = ExcelFile_1.default;
const ExcelSheet_1 = __importDefault(require("./ExcelPlugin/elements/ExcelSheet"));
exports.ExcelSheet = ExcelSheet_1.default;
const ExcelColumn_1 = __importDefault(require("./ExcelPlugin/elements/ExcelColumn"));
exports.ExcelColumn = ExcelColumn_1.default;
ExcelFile_1.default.ExcelSheet = ExcelSheet_1.default;
ExcelFile_1.default.ExcelColumn = ExcelColumn_1.default;
exports.default = ExcelFile_1.default;
const ReactExport = {
ExcelFile: ExcelFile_1.default
};
exports.default = ReactExport;
53 changes: 27 additions & 26 deletions src/ExcelPlugin/components/ExcelFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@ import type { BookType, WorkSheet } from "xlsx-js-style";
import { excelSheetFromAoA, excelSheetFromDataSet } from "../utils/DataUtil";
import type {
ExcelColumnProps,
ExcelFileProps,
ExcelSheetProps,
ExcelValue,
} from "react-xlsx-wrapper";

interface ExcelFileProps {
hideElement?: boolean;
filename?: string;
fileExtension?: BookType;
element?: React.ReactNode;
children: React.ReactElement<ExcelSheetProps>[];
}

class ExcelFile extends React.Component<ExcelFileProps> {
state = {
fileName: "Download",
fileExtension: "xlsx" as BookType,
hideElement: false,
};
static ExcelColumn: typeof import("/Users/susanta96/Develop/OwnProject/AS-Devs/react-xlsx-wrapper/src/ExcelPlugin/elements/ExcelColumn").default;
static ExcelSheet: typeof import("/Users/susanta96/Develop/OwnProject/AS-Devs/react-xlsx-wrapper/src/ExcelPlugin/elements/ExcelSheet").default;

componentDidMount() {
if (this.props.filename) {
Expand All @@ -35,9 +27,15 @@ class ExcelFile extends React.Component<ExcelFileProps> {
fileExtension: this.props.fileExtension,
});
}

if (this.props.hideElement) {
this.setState({
hideElement: this.props.hideElement,
});
}
}

createSheetData = (sheet: React.ReactElement<ExcelSheetProps>) => {
createSheetData = (sheet: React.ReactElement<any>) => {
const columns = sheet.props.children;
const sheetData = [
React.Children.map(
Expand Down Expand Up @@ -71,20 +69,23 @@ class ExcelFile extends React.Component<ExcelFileProps> {
const fileName = this.getFileName();
const fileExtension: BookType = this.getFileExtension();

React.Children.forEach(this.props.children, (sheet) => {
let ws: WorkSheet = {};
const wsName = sheet.props.name || fileName.split(".")[0] || "Sheet1";
if (
typeof sheet.props.dataSet === "undefined" ||
sheet.props.dataSet.length === 0
) {
ws = excelSheetFromAoA(this.createSheetData(sheet));
} else {
ws = excelSheetFromDataSet(sheet.props.dataSet);
React.Children.forEach<React.ReactElement<ExcelSheetProps>>(
this.props.children,
(sheet) => {
let ws: WorkSheet = {};
const wsName = sheet.props.name || fileName.split(".")[0] || "Sheet1";
if (
typeof sheet.props.dataSet === "undefined" ||
sheet.props.dataSet.length === 0
) {
ws = excelSheetFromAoA(this.createSheetData(sheet));
} else {
ws = excelSheetFromDataSet(sheet.props.dataSet);
}
// add worksheet to workbook
utils.book_append_sheet(wb, ws, wsName);
}
// add worksheet to workbook
utils.book_append_sheet(wb, ws, wsName);
});
);

writeFile(wb, fileName, {
bookType: fileExtension,
Expand Down Expand Up @@ -141,9 +142,9 @@ class ExcelFile extends React.Component<ExcelFileProps> {
};

render() {
const { hideElement, element } = this.props;
const { element } = this.props;

if (hideElement) {
if (this.state.hideElement === true) {
return null;
} else {
return <span onClick={this.handleDownload}>{element}</span>;
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import ExcelFile from "./ExcelPlugin/components/ExcelFile";
import ExcelSheet from "./ExcelPlugin/elements/ExcelSheet";
import ExcelColumn from "./ExcelPlugin/elements/ExcelColumn";

ExcelFile.ExcelSheet = ExcelSheet;
ExcelFile.ExcelColumn = ExcelColumn;
const ReactExport = {
ExcelFile
};

export default ExcelFile;
export default ReactExport;

export {
ExcelFile,
ExcelSheet,
ExcelColumn
};
10 changes: 8 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ declare module 'react-xlsx-wrapper' {
import * as React from 'react';

export interface ExcelFileProps {
hideElement?: boolean; //Hide download element
filename?: string;
fileExtension?: string;
element?: React.ReactElement; //Download Element
children?: Array<React.ReactElement> | React.ReactElement; // Array<ExcelSheetProps>;
children: Array<React.ReactElement<ExcelSheetProps>> | React.ReactElement<ExcelSheetProps>;
}

export interface ExcelSheetProps {
Expand Down Expand Up @@ -141,6 +142,11 @@ declare module 'react-xlsx-wrapper' {

export class ExcelFile extends React.Component<ExcelFileProps, any> {
}


export namespace ReactExport {
export class ExcelFile extends React.Component<ExcelFileProps, any> {
}
}
export default ReactExport

}

0 comments on commit de7bac0

Please sign in to comment.