From de7bac0b1a80c5ffd29437b35404586ca8c264c5 Mon Sep 17 00:00:00 2001 From: susanta96 Date: Tue, 13 Jun 2023 11:40:21 +0530 Subject: [PATCH] fix(excelfile): changes to fix import #6 --- dist/ExcelPlugin/components/ExcelFile.js | 16 ++++--- dist/ExcelPlugin/elements/ExcelSheet.js | 5 +- dist/index.js | 10 ++-- src-js/ExcelPlugin/components/ExcelFile.js | 12 +++-- src-js/ExcelPlugin/elements/ExcelSheet.js | 2 +- src-js/index.js | 10 ++-- src/ExcelPlugin/components/ExcelFile.tsx | 53 +++++++++++----------- src/index.ts | 8 ++-- types/index.d.ts | 10 +++- 9 files changed, 73 insertions(+), 53 deletions(-) diff --git a/dist/ExcelPlugin/components/ExcelFile.js b/dist/ExcelPlugin/components/ExcelFile.js index 3439259..4620e1b 100644 --- a/dist/ExcelPlugin/components/ExcelFile.js +++ b/dist/ExcelPlugin/components/ExcelFile.js @@ -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; @@ -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", { @@ -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; \ No newline at end of file diff --git a/dist/ExcelPlugin/elements/ExcelSheet.js b/dist/ExcelPlugin/elements/ExcelSheet.js index d3a2f5a..6841c64 100644 --- a/dist/ExcelPlugin/elements/ExcelSheet.js +++ b/dist/ExcelPlugin/elements/ExcelSheet.js @@ -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(" can only have as children"); } return _this; diff --git a/dist/index.js b/dist/index.js index bf34838..2240d9f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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; \ No newline at end of file +var ReactExport = { + ExcelFile: ExcelFile_1.default +}; +exports.default = ReactExport; \ No newline at end of file diff --git a/src-js/ExcelPlugin/components/ExcelFile.js b/src-js/ExcelPlugin/components/ExcelFile.js index bb8f4d5..68576ee 100644 --- a/src-js/ExcelPlugin/components/ExcelFile.js +++ b/src-js/ExcelPlugin/components/ExcelFile.js @@ -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({ @@ -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; @@ -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 { diff --git a/src-js/ExcelPlugin/elements/ExcelSheet.js b/src-js/ExcelPlugin/elements/ExcelSheet.js index 3e648ef..3d4e61b 100644 --- a/src-js/ExcelPlugin/elements/ExcelSheet.js +++ b/src-js/ExcelPlugin/elements/ExcelSheet.js @@ -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(" can only have as children"); } } diff --git a/src-js/index.js b/src-js/index.js index 0c0959b..40aa639 100644 --- a/src-js/index.js +++ b/src-js/index.js @@ -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; diff --git a/src/ExcelPlugin/components/ExcelFile.tsx b/src/ExcelPlugin/components/ExcelFile.tsx index b381330..6f0586f 100644 --- a/src/ExcelPlugin/components/ExcelFile.tsx +++ b/src/ExcelPlugin/components/ExcelFile.tsx @@ -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[]; -} - class ExcelFile extends React.Component { 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) { @@ -35,9 +27,15 @@ class ExcelFile extends React.Component { fileExtension: this.props.fileExtension, }); } + + if (this.props.hideElement) { + this.setState({ + hideElement: this.props.hideElement, + }); + } } - createSheetData = (sheet: React.ReactElement) => { + createSheetData = (sheet: React.ReactElement) => { const columns = sheet.props.children; const sheetData = [ React.Children.map( @@ -71,20 +69,23 @@ class ExcelFile extends React.Component { 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>( + 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, @@ -141,9 +142,9 @@ class ExcelFile extends React.Component { }; render() { - const { hideElement, element } = this.props; + const { element } = this.props; - if (hideElement) { + if (this.state.hideElement === true) { return null; } else { return {element}; diff --git a/src/index.ts b/src/index.ts index 6bdd560..cfabfdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }; diff --git a/types/index.d.ts b/types/index.d.ts index db15c38..14e4ad8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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; // Array; + children: Array> | React.ReactElement; } export interface ExcelSheetProps { @@ -141,6 +142,11 @@ declare module 'react-xlsx-wrapper' { export class ExcelFile extends React.Component { } - + + export namespace ReactExport { + export class ExcelFile extends React.Component { + } + } + export default ReactExport }